Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
55 / 55
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
Feed
100.00% covered (success)
100.00%
55 / 55
100.00% covered (success)
100.00%
7 / 7
17
100.00% covered (success)
100.00%
1 / 1
 render
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 _appendNamespaces
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 setTotalResults
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 setStartIndex
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 setItemsPerPage
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 setQuery
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
3
 setLinks
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3/**
4 * Laminas\Feed\Renderer\Feed extension for Open Search
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Deutsches Archäologisches Institut 2015.
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  Feed_Plugins
25 * @author   Sebastian Cuy <sebastian.cuy@uni-koeln.de>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFind\Feed\Writer\Extension\OpenSearch\Renderer;
31
32use DOMDocument;
33use DOMElement;
34use Laminas\Feed\Writer\Extension\AbstractRenderer;
35
36/**
37 * Laminas\Feed\Renderer\Feed extension for Open Search
38 *
39 * @category VuFind
40 * @package  Feed_Plugins
41 * @author   Sebastian Cuy <sebastian.cuy@uni-koeln.de>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org/wiki/development Wiki
44 */
45class Feed extends AbstractRenderer
46{
47    /**
48     * Set to TRUE if a rendering method actually renders something. This
49     * is used to prevent premature appending of a XML namespace declaration
50     * until an element which requires it is actually appended.
51     *
52     * @var bool
53     */
54    protected $called = false;
55
56    /**
57     * Render feed
58     *
59     * @return void
60     */
61    public function render()
62    {
63        $this->setTotalResults($this->dom, $this->base);
64        $this->setStartIndex($this->dom, $this->base);
65        $this->setItemsPerPage($this->dom, $this->base);
66        $this->setQuery($this->dom, $this->base);
67        $this->setLinks($this->dom, $this->base);
68        if ($this->called) {
69            $this->_appendNamespaces();
70        }
71    }
72
73    /**
74     * Append feed namespaces
75     *
76     * @return void
77     */
78    // @codingStandardsIgnoreStart
79    protected function _appendNamespaces()
80    {
81        // @codingStandardsIgnoreEnd
82        // (We have to ignore coding standards here because the method name has
83        // to have an underscore for compatibility w/ parent class)
84        $this->getRootElement()->setAttribute(
85            'xmlns:opensearch',
86            'http://a9.com/-/spec/opensearch/1.1/'
87        );
88    }
89
90    /**
91     * Set total results
92     *
93     * @param DOMDocument $dom  the dom document
94     * @param DOMElement  $root the root element
95     *
96     * @return void
97     */
98    protected function setTotalResults(DOMDocument $dom, DOMElement $root)
99    {
100        $totalResults = $this->getDataContainer()->getOpensearchTotalResults();
101        if ($totalResults !== null) {
102            $elem = $dom->createElement('opensearch:totalResults');
103            $text = $dom->createTextNode($totalResults);
104            $elem->appendChild($text);
105            $root->appendChild($elem);
106            $this->called = true;
107        }
108    }
109
110    /**
111     * Set start index
112     *
113     * @param DOMDocument $dom  the dom document
114     * @param DOMElement  $root the root element
115     *
116     * @return void
117     */
118    protected function setStartIndex(DOMDocument $dom, DOMElement $root)
119    {
120        $startIndex = $this->getDataContainer()->getOpensearchStartIndex();
121        if ($startIndex !== null) {
122            $elem = $dom->createElement('opensearch:startIndex');
123            $text = $dom->createTextNode($startIndex);
124            $elem->appendChild($text);
125            $root->appendChild($elem);
126            $this->called = true;
127        }
128    }
129
130    /**
131     * Set items per page
132     *
133     * @param DOMDocument $dom  the dom document
134     * @param DOMElement  $root the root element
135     *
136     * @return void
137     */
138    protected function setItemsPerPage(DOMDocument $dom, DOMElement $root)
139    {
140        $itemsPerPage = $this->getDataContainer()->getOpensearchItemsPerPage();
141        if ($itemsPerPage !== null) {
142            $elem = $dom->createElement('opensearch:itemsPerPage');
143            $text = $dom->createTextNode($itemsPerPage);
144            $elem->appendChild($text);
145            $root->appendChild($elem);
146            $this->called = true;
147        }
148    }
149
150    /**
151     * Set the query element
152     *
153     * @param DOMDocument $dom  the dom document
154     * @param DOMElement  $root the root element
155     *
156     * @return void
157     */
158    protected function setQuery(DOMDocument $dom, DOMElement $root)
159    {
160        $searchTerms = $this->getDataContainer()->getOpensearchSearchTerms();
161        $startIndex = $this->getDataContainer()->getOpensearchStartIndex();
162        if (!empty($searchTerms)) {
163            $elem = $dom->createElement('opensearch:Query');
164            $elem->setAttribute('role', 'request');
165            $elem->setAttribute('searchTerms', rawurlencode($searchTerms));
166            if ($startIndex !== null) {
167                $elem->setAttribute('startIndex', $startIndex);
168            }
169            $root->appendChild($elem);
170            $this->called = true;
171        }
172    }
173
174    /**
175     * Set links
176     *
177     * @param DOMDocument $dom  the dom document
178     * @param DOMElement  $root the root element
179     *
180     * @return void
181     */
182    protected function setLinks(DOMDocument $dom, DOMElement $root)
183    {
184        $links = $this->getDataContainer()->getOpensearchLinks();
185        foreach ($links as $link) {
186            $elem = $dom->createElement('atom:link');
187            if ($link['role'] != null) {
188                $elem->setAttribute('rel', $link['role']);
189            }
190            if ($link['type'] != null) {
191                $mime = 'application/' . strtolower($link['type']) . '+xml';
192                $elem->setAttribute('type', $mime);
193            }
194            if ($link['title'] ?? null) {
195                $elem->setAttribute('title', $link['title']);
196            }
197            $elem->setAttribute('href', $link['url']);
198            $root->appendChild($elem);
199            $this->called = true;
200        }
201    }
202}