Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
15 / 15 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
RecordCollectionFactory | |
100.00% |
15 / 15 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
factory | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | /** |
4 | * Simple XML-based factory for record collection. |
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 Julia Bauder <bauderj@grinnell.edu> |
26 | * @author David Maus <maus@hab.de> |
27 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
28 | * @link https://vufind.org |
29 | */ |
30 | |
31 | namespace VuFindSearch\Backend\EIT\Response\XML; |
32 | |
33 | use VuFindSearch\Exception\InvalidArgumentException; |
34 | use VuFindSearch\Response\RecordCollectionFactoryInterface; |
35 | |
36 | use function call_user_func; |
37 | use function gettype; |
38 | use function is_array; |
39 | use function is_callable; |
40 | |
41 | /** |
42 | * Simple XML-based factory for record collection. |
43 | * Largely copied from the WorldCat XML-based factory |
44 | * |
45 | * @category VuFind |
46 | * @package Search |
47 | * @author Julia Bauder <bauderj@grinnell.edu> |
48 | * @author David Maus <maus@hab.de> |
49 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
50 | * @link https://vufind.org |
51 | */ |
52 | class RecordCollectionFactory implements RecordCollectionFactoryInterface |
53 | { |
54 | /** |
55 | * Factory to turn data into a record object. |
56 | * |
57 | * @var callable |
58 | */ |
59 | protected $recordFactory; |
60 | |
61 | /** |
62 | * Class of collection. |
63 | * |
64 | * @var string |
65 | */ |
66 | protected $collectionClass; |
67 | |
68 | /** |
69 | * Constructor. |
70 | * |
71 | * @param callable $recordFactory Record factory function |
72 | * @param string $collectionClass Class of collection |
73 | * |
74 | * @return void |
75 | */ |
76 | public function __construct($recordFactory = null, $collectionClass = null) |
77 | { |
78 | if (!is_callable($recordFactory)) { |
79 | throw new InvalidArgumentException('Record factory must be callable.'); |
80 | } |
81 | $this->recordFactory = $recordFactory; |
82 | $this->collectionClass = $collectionClass ?? RecordCollection::class; |
83 | } |
84 | |
85 | /** |
86 | * Return record collection. |
87 | * |
88 | * @param array $response Collection of XML documents |
89 | * |
90 | * @return RecordCollection |
91 | */ |
92 | public function factory($response) |
93 | { |
94 | if (!is_array($response)) { |
95 | throw new InvalidArgumentException( |
96 | sprintf( |
97 | 'Unexpected type of value: Expected array, got %s', |
98 | gettype($response) |
99 | ) |
100 | ); |
101 | } |
102 | $collection = new $this->collectionClass($response); |
103 | foreach ($response['docs'] as $doc) { |
104 | $collection->add(call_user_func($this->recordFactory, $doc), false); |
105 | } |
106 | return $collection; |
107 | } |
108 | } |