Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 18 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
MarcCollection | |
0.00% |
0 / 18 |
|
0.00% |
0 / 7 |
132 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
setData | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
rewind | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
current | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
key | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
next | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
valid | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | /** |
4 | * MARC collection class. |
5 | * |
6 | * PHP version 7 |
7 | * |
8 | * Copyright (C) The National Library of Finland 2021-2022. |
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 MARC |
25 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org/wiki/development Wiki |
28 | */ |
29 | |
30 | namespace VuFind\Marc; |
31 | |
32 | /** |
33 | * MARC collection class. |
34 | * |
35 | * @category VuFind |
36 | * @package MARC |
37 | * @author Ere Maijala <ere.maijala@helsinki.fi> |
38 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
39 | * @link https://vufind.org/wiki/development Wiki |
40 | */ |
41 | class MarcCollection implements \Iterator |
42 | { |
43 | /** |
44 | * Supported serialization formats |
45 | * |
46 | * @var array |
47 | */ |
48 | protected $serializations = [ |
49 | 'ISO2709' => Serialization\Iso2709::class, |
50 | 'MARCXML' => Serialization\MarcXml::class, |
51 | 'JSON' => Serialization\MarcInJson::class, |
52 | ]; |
53 | |
54 | /** |
55 | * Records in the collection |
56 | * |
57 | * @var array |
58 | */ |
59 | protected $records = []; |
60 | |
61 | /** |
62 | * Iteration position |
63 | * |
64 | * @var int |
65 | */ |
66 | protected $position = 0; |
67 | |
68 | /** |
69 | * Constructor |
70 | * |
71 | * @param string $data MARC record collection in MARCXML or ISO2709 format |
72 | */ |
73 | public function __construct(string $data = '') |
74 | { |
75 | $this->setData($data); |
76 | } |
77 | |
78 | /** |
79 | * Set MARC record data |
80 | * |
81 | * @param string $data MARC record in MARCXML or ISO2709 format |
82 | * |
83 | * @throws \Exception |
84 | * @return void |
85 | */ |
86 | public function setData(string $data): void |
87 | { |
88 | $this->position = 0; |
89 | $this->records = []; |
90 | if (!$data) { |
91 | return; |
92 | } |
93 | $valid = false; |
94 | foreach ($this->serializations as $serialization) { |
95 | if ($serialization::canParseCollection($data)) { |
96 | $this->records = $serialization::collectionFromString($data); |
97 | $valid = true; |
98 | break; |
99 | } |
100 | } |
101 | if (!$valid) { |
102 | throw new \Exception('MARC collection format not recognized'); |
103 | } |
104 | } |
105 | |
106 | /** |
107 | * Iterator: Rewind to the beginning. |
108 | * |
109 | * @return void |
110 | */ |
111 | public function rewind(): void |
112 | { |
113 | $this->position = 0; |
114 | } |
115 | |
116 | /** |
117 | * Iterator: Return current record. |
118 | * |
119 | * @return mixed |
120 | */ |
121 | #[\ReturnTypeWillChange] |
122 | public function current() |
123 | { |
124 | return new MarcReader($this->records[$this->position]); |
125 | } |
126 | |
127 | /** |
128 | * Iterator: Return current key. |
129 | * |
130 | * @return mixed |
131 | */ |
132 | #[\ReturnTypeWillChange] |
133 | public function key() |
134 | { |
135 | return $this->position; |
136 | } |
137 | |
138 | /** |
139 | * Iterator: Advance to the next record. |
140 | * |
141 | * @return void |
142 | */ |
143 | public function next(): void |
144 | { |
145 | ++$this->position; |
146 | } |
147 | |
148 | /** |
149 | * Iterator: Check if current position is valid. |
150 | * |
151 | * @return bool |
152 | */ |
153 | public function valid(): bool |
154 | { |
155 | return isset($this->records[$this->position]); |
156 | } |
157 | } |