Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
UpdateDocument
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
4 / 4
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getContentType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getContent
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
6
 addRecord
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * SOLR update document class.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Search
25 * @author   David Maus <maus@hab.de>
26 * @author   Demian Katz <demian.katz@villanova.edu>
27 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28 * @link     https://vufind.org
29 */
30
31namespace VuFindSearch\Backend\Solr\Document;
32
33use SplObjectStorage;
34use VuFindSearch\Backend\Solr\Record\SerializableRecordInterface;
35use XMLWriter;
36
37use function is_array;
38
39/**
40 * SOLR update document class.
41 *
42 * @category VuFind
43 * @package  Search
44 * @author   David Maus <maus@hab.de>
45 * @author   Demian Katz <demian.katz@villanova.edu>
46 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
47 * @link     https://vufind.org
48 */
49class UpdateDocument implements DocumentInterface
50{
51    /**
52     * Records and index attributes.
53     *
54     * @var SplObjectStorage
55     */
56    protected $records;
57
58    /**
59     * Constructor.
60     */
61    public function __construct()
62    {
63        $this->records = new SplObjectStorage();
64    }
65
66    /**
67     * Return content MIME type.
68     *
69     * @return string
70     */
71    public function getContentType(): string
72    {
73        return 'text/xml; charset=UTF-8';
74    }
75
76    /**
77     * Return serialized representation.
78     *
79     * @return string
80     */
81    public function getContent(): string
82    {
83        $writer = new XMLWriter();
84        $writer->openMemory();
85        $writer->startDocument();
86        $writer->startElement('add');
87        foreach ($this->records as $record) {
88            $writer->startElement('doc');
89            $indexAttr = $this->records->offsetGet($record);
90            foreach ($indexAttr as $name => $value) {
91                $writer->writeAttribute($name, $value);
92            }
93            foreach ($record->getFields() as $name => $values) {
94                $values = is_array($values) ? $values : [$values];
95                foreach ($values as $value) {
96                    $writer->startElement('field');
97                    $writer->writeAttribute('name', $name);
98                    $writer->text($value);
99                    $writer->endElement();
100                }
101            }
102            $writer->endElement();
103        }
104        $writer->endElement();
105        $writer->endDocument();
106        return $writer->flush();
107    }
108
109    /**
110     * Add record.
111     *
112     * @param SerializableRecordInterface $record    Record
113     * @param array                       $indexAttr Index attributes
114     *
115     * @return void
116     */
117    public function addRecord(
118        SerializableRecordInterface $record,
119        array $indexAttr = []
120    ): void {
121        $this->records->attach($record, $indexAttr);
122    }
123}