Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.85% covered (warning)
84.85%
28 / 33
84.62% covered (warning)
84.62%
11 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
Feed
84.85% covered (warning)
84.85%
28 / 33
84.62% covered (warning)
84.62%
11 / 13
19.13
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setEncoding
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getEncoding
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setOpensearchTotalResults
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getOpensearchTotalResults
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setOpensearchStartIndex
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getOpensearchStartIndex
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setOpensearchItemsPerPage
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getOpensearchItemsPerPage
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setOpensearchSearchTerms
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getOpensearchSearchTerms
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addOpensearchLink
73.33% covered (warning)
73.33%
11 / 15
0.00% covered (danger)
0.00%
0 / 1
6.68
 getOpensearchLinks
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Laminas\Feed\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;
31
32use Laminas\Feed\Uri;
33use Laminas\Feed\Writer\Exception;
34use Laminas\Feed\Writer\Extension\ITunes\Feed as ParentFeed;
35use Laminas\Stdlib\StringUtils;
36
37use function in_array;
38use function is_string;
39
40/**
41 * Laminas\Feed\Feed extension for Open Search
42 *
43 * Note: There doesn't seem to be a generic base class for this functionality,
44 * and creating a class with no parent blows up due to unexpected calls to
45 * Itunes-related functionality. To work around this, we are extending the
46 * equivalent Itunes plugin. This works fine, but perhaps in future there will
47 * be a more elegant way to achieve the same effect.
48 *
49 * @category VuFind
50 * @package  Feed_Plugins
51 * @author   Sebastian Cuy <sebastian.cuy@uni-koeln.de>
52 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
53 * @link     https://vufind.org/wiki/development Wiki
54 */
55class Feed extends ParentFeed
56{
57    /**
58     * Total results
59     *
60     * @var int
61     */
62    protected $totalResults = null;
63
64    /**
65     * Start index
66     *
67     * @var int
68     */
69    protected $startIndex = null;
70
71    /**
72     * Items per page
73     *
74     * @var int
75     */
76    protected $itemsPerPage = null;
77
78    /**
79     * Search terms
80     *
81     * @var string
82     */
83    protected $searchTerms = null;
84
85    /**
86     * Links
87     *
88     * @var array
89     */
90    protected $links = [];
91
92    /**
93     * Encoding of all text values
94     *
95     * @var string
96     */
97    protected $encoding = 'UTF-8';
98
99    /**
100     * The used string wrapper supporting encoding
101     *
102     * @var StringWrapperInterface
103     */
104    protected $stringWrapper;
105
106    /**
107     * Constructor
108     */
109    public function __construct()
110    {
111        $this->stringWrapper = StringUtils::getWrapper($this->encoding);
112    }
113
114    /**
115     * Set feed encoding
116     *
117     * @param string $enc encoding to set
118     *
119     * @return Feed
120     */
121    public function setEncoding($enc)
122    {
123        $this->stringWrapper = StringUtils::getWrapper($enc);
124        $this->encoding      = $enc;
125        return $this;
126    }
127
128    /**
129     * Get feed encoding
130     *
131     * @return string
132     */
133    public function getEncoding()
134    {
135        return $this->encoding;
136    }
137
138    /**
139     * Set total results
140     *
141     * @param int $totalResults number to set
142     *
143     * @return Feed
144     */
145    public function setOpensearchTotalResults($totalResults)
146    {
147        $this->totalResults = $totalResults;
148        return $this;
149    }
150
151    /**
152     * Get total results
153     *
154     * @return int
155     */
156    public function getOpensearchTotalResults()
157    {
158        return $this->totalResults;
159    }
160
161    /**
162     * Set start index
163     *
164     * @param int $startIndex index to set
165     *
166     * @return Feed
167     */
168    public function setOpensearchStartIndex($startIndex)
169    {
170        $this->startIndex = $startIndex;
171        return $this;
172    }
173
174    /**
175     * Get start index
176     *
177     * @return int
178     */
179    public function getOpensearchStartIndex()
180    {
181        return $this->startIndex;
182    }
183
184    /**
185     * Set items per page
186     *
187     * @param int $itemsPerPage number to set
188     *
189     * @return Feed
190     */
191    public function setOpensearchItemsPerPage($itemsPerPage)
192    {
193        $this->itemsPerPage = $itemsPerPage;
194        return $this;
195    }
196
197    /**
198     * Get items per page
199     *
200     * @return int
201     */
202    public function getOpensearchItemsPerPage()
203    {
204        return $this->itemsPerPage;
205    }
206
207    /**
208     * Set search terms
209     *
210     * @param string $searchTerms search terms
211     *
212     * @return Feed
213     */
214    public function setOpensearchSearchTerms($searchTerms)
215    {
216        $this->searchTerms = $searchTerms;
217        return $this;
218    }
219
220    /**
221     * Get search terms
222     *
223     * @return string
224     */
225    public function getOpensearchSearchTerms()
226    {
227        return $this->searchTerms;
228    }
229
230    /**
231     * Add a link
232     *
233     * @param string $url   the url of the link
234     * @param string $role  the role of the link
235     * @param string $type  the mime type of the link
236     * @param string $title Title for the link (optional)
237     *
238     * @return Feed
239     */
240    public function addOpensearchLink(
241        $url,
242        $role = null,
243        $type = null,
244        $title = null
245    ) {
246        if (empty($url) || !is_string($url) || !Uri::factory($url)->isValid()) {
247            throw new Exception\InvalidArgumentException(
248                'Invalid parameter: "url" must be '
249                . 'a non-empty string and valid URI/IRI'
250            );
251        }
252        if (!in_array(strtolower($type), ['rss', 'rdf', 'atom'])) {
253            throw new Exception\InvalidArgumentException(
254                'Invalid parameter: "type"; You must declare the type of '
255                . 'feed the link points to, i.e. RSS, RDF or Atom'
256            );
257        }
258        $link = compact('url', 'role', 'type');
259        if ($title) {
260            $link['title'] = $title;
261        }
262        $this->links[] = $link;
263        return $this;
264    }
265
266    /**
267     * Get the links
268     *
269     * @return string
270     */
271    public function getOpensearchLinks()
272    {
273        return $this->links;
274    }
275}