Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
DeleteDocument
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
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%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
 addKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addKeys
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addQuery
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * SOLR delete 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 XMLWriter;
34
35/**
36 * SOLR delete document class.
37 *
38 * @category VuFind
39 * @package  Search
40 * @author   David Maus <maus@hab.de>
41 * @author   Demian Katz <demian.katz@villanova.edu>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org
44 */
45class DeleteDocument implements DocumentInterface
46{
47    /**
48     * Unique keys to delete.
49     *
50     * @var string[]
51     */
52    protected $keys = [];
53
54    /**
55     * Delete queries.
56     *
57     * @var string[]
58     */
59    protected $queries = [];
60
61    /**
62     * Return content MIME type.
63     *
64     * @return string
65     */
66    public function getContentType(): string
67    {
68        return 'text/xml; charset=UTF-8';
69    }
70
71    /**
72     * Return serialized representation.
73     *
74     * @return string
75     */
76    public function getContent(): string
77    {
78        $writer = new XMLWriter();
79        $writer->openMemory();
80        $writer->startDocument();
81        $writer->startElement('delete');
82        foreach ($this->keys as $key) {
83            $writer->writeElement('id', $key);
84        }
85        foreach ($this->queries as $query) {
86            $writer->writeElement('query', $query);
87        }
88        $writer->endElement();
89        $writer->endDocument();
90        return $writer->flush();
91    }
92
93    /**
94     * Add unique key to delete.
95     *
96     * @param string $key Unique key
97     *
98     * @return void
99     */
100    public function addKey(string $key): void
101    {
102        $this->keys[] = $key;
103    }
104
105    /**
106     * Add array of unique keys to delete.
107     *
108     * @param string[] $keys Unique keys
109     *
110     * @return void
111     */
112    public function addKeys(array $keys): void
113    {
114        $this->keys = array_merge($this->keys, $keys);
115    }
116
117    /**
118     * Add delete query.
119     *
120     * @param string $query Delete query
121     *
122     * @return void
123     */
124    public function addQuery(string $query): void
125    {
126        $this->queries[] = $query;
127    }
128}