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