Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
19 / 19 |
|
100.00% |
10 / 10 |
CRAP | |
100.00% |
1 / 1 |
NamedList | |
100.00% |
19 / 19 |
|
100.00% |
10 / 10 |
15 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
toArray | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
count | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
current | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
key | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
next | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
valid | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
rewind | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
removeKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
removeKeys | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | /** |
4 | * SOLR NamedList with parameter json.nl=arrarr. |
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 Countable; |
33 | use Iterator; |
34 | |
35 | use function count; |
36 | use function in_array; |
37 | use function is_array; |
38 | |
39 | /** |
40 | * SOLR NamedList with parameter json.nl=arrarr. |
41 | * |
42 | * A NamedList arrarr represent a NamedList as an array of two element arrays |
43 | * [[name1,val1], [name2, val2], [name3,val3]]. |
44 | * |
45 | * @category VuFind |
46 | * @package Search |
47 | * @author David Maus <maus@hab.de> |
48 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
49 | * @link https://vufind.org |
50 | * @see http://wiki.apache.org/solr/SolJSON |
51 | */ |
52 | class NamedList implements Countable, Iterator |
53 | { |
54 | /** |
55 | * The named list. |
56 | * |
57 | * @var array |
58 | */ |
59 | protected $list; |
60 | |
61 | /** |
62 | * The current position |
63 | * |
64 | * @var array |
65 | */ |
66 | protected $current = null; |
67 | |
68 | /** |
69 | * Constructor. |
70 | * |
71 | * @param array $list Named list |
72 | * |
73 | * @return void |
74 | */ |
75 | public function __construct(array $list) |
76 | { |
77 | $this->list = $list; |
78 | } |
79 | |
80 | /** |
81 | * Convert the named list into a standard associative array. |
82 | * |
83 | * @return array |
84 | */ |
85 | public function toArray() |
86 | { |
87 | $arr = []; |
88 | foreach ($this as $k => $v) { |
89 | $arr[$k] = $v; |
90 | } |
91 | return $arr; |
92 | } |
93 | |
94 | /// Countable |
95 | |
96 | /** |
97 | * Return count of elements. |
98 | * |
99 | * @return int |
100 | */ |
101 | public function count(): int |
102 | { |
103 | return count($this->list); |
104 | } |
105 | |
106 | /// Iterator |
107 | |
108 | /** |
109 | * Return current element value. |
110 | * |
111 | * @return mixed |
112 | */ |
113 | #[\ReturnTypeWillChange] |
114 | public function current() |
115 | { |
116 | return $this->valid() ? $this->current[1] : null; |
117 | } |
118 | |
119 | /** |
120 | * Return current element name. |
121 | * |
122 | * @return string |
123 | */ |
124 | #[\ReturnTypeWillChange] |
125 | public function key() |
126 | { |
127 | return $this->valid() ? $this->current[0] : null; |
128 | } |
129 | |
130 | /** |
131 | * Move to next element. |
132 | * |
133 | * @return void |
134 | */ |
135 | public function next(): void |
136 | { |
137 | $this->current = next($this->list); |
138 | } |
139 | |
140 | /** |
141 | * Return true if the iterator is at a valid position. |
142 | * |
143 | * @return bool |
144 | */ |
145 | public function valid(): bool |
146 | { |
147 | return is_array($this->current); |
148 | } |
149 | |
150 | /** |
151 | * Rewind iterator. |
152 | * |
153 | * @return void |
154 | */ |
155 | public function rewind(): void |
156 | { |
157 | reset($this->list); |
158 | $this->current = current($this->list); |
159 | } |
160 | |
161 | /** |
162 | * Remove single element from list. |
163 | * |
164 | * @param string $key Key to remove |
165 | * |
166 | * @return void |
167 | */ |
168 | public function removeKey($key) |
169 | { |
170 | $this->removeKeys([$key]); |
171 | } |
172 | |
173 | /** |
174 | * Remove elements from list. |
175 | * |
176 | * @param array $keys Keys to remove |
177 | * |
178 | * @return void |
179 | */ |
180 | public function removeKeys(array $keys) |
181 | { |
182 | $newList = []; |
183 | foreach ($this->list as $current) { |
184 | if (!in_array($current[0], $keys)) { |
185 | $newList[] = $current; |
186 | } |
187 | } |
188 | $this->list = $newList; |
189 | $this->rewind(); |
190 | } |
191 | } |