Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.74% |
18 / 19 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
RecordCollectionFactory | |
94.74% |
18 / 19 |
|
50.00% |
1 / 2 |
7.01 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
factory | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
5.01 |
1 | <?php |
2 | |
3 | /** |
4 | * Simple JSON-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\Solr\Response\Json; |
31 | |
32 | use VuFindSearch\Exception\InvalidArgumentException; |
33 | use VuFindSearch\Response\RecordCollectionFactoryInterface; |
34 | |
35 | use function call_user_func; |
36 | use function gettype; |
37 | use function is_array; |
38 | |
39 | /** |
40 | * Simple JSON-based factory for record collection. |
41 | * |
42 | * @category VuFind |
43 | * @package Search |
44 | * @author David Maus <maus@hab.de> |
45 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
46 | * @link https://vufind.org |
47 | */ |
48 | class RecordCollectionFactory implements RecordCollectionFactoryInterface |
49 | { |
50 | /** |
51 | * Factory to turn data into a record object. |
52 | * |
53 | * @var callable |
54 | */ |
55 | protected $recordFactory; |
56 | |
57 | /** |
58 | * Class of collection. |
59 | * |
60 | * @var string |
61 | */ |
62 | protected $collectionClass; |
63 | |
64 | /** |
65 | * Constructor. |
66 | * |
67 | * @param callable $recordFactory Callback to construct records |
68 | * @param string $collectionClass Class of collection |
69 | * |
70 | * @return void |
71 | */ |
72 | public function __construct( |
73 | $recordFactory = null, |
74 | $collectionClass = RecordCollection::class |
75 | ) { |
76 | if (null === $recordFactory) { |
77 | $this->recordFactory = function ($data) { |
78 | return new Record($data); |
79 | }; |
80 | } else { |
81 | $this->recordFactory = $recordFactory; |
82 | } |
83 | $this->collectionClass = $collectionClass; |
84 | } |
85 | |
86 | /** |
87 | * Return record collection. |
88 | * |
89 | * @param array $response Deserialized JSON response |
90 | * |
91 | * @return RecordCollection |
92 | */ |
93 | public function factory($response) |
94 | { |
95 | if (!is_array($response)) { |
96 | throw new InvalidArgumentException( |
97 | sprintf( |
98 | 'Unexpected type of value: Expected array, got %s', |
99 | gettype($response) |
100 | ) |
101 | ); |
102 | } |
103 | $collection = new $this->collectionClass($response); |
104 | $hlDetails = $response['highlighting'] ?? []; |
105 | foreach ($response['response']['docs'] ?? [] as $doc) { |
106 | // If highlighting details were provided, merge them into the record for future use: |
107 | if (isset($doc['id']) && ($hl = $hlDetails[$doc['id']] ?? [])) { |
108 | $doc['__highlight_details'] = $hl; |
109 | } |
110 | $collection->add(call_user_func($this->recordFactory, $doc), false); |
111 | } |
112 | return $collection; |
113 | } |
114 | } |