Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
MarcCollectionFile | |
0.00% |
0 / 26 |
|
0.00% |
0 / 7 |
210 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
setFile | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
rewind | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
current | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
key | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
next | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
valid | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | /** |
4 | * MARC collection class for streaming a file. |
5 | * |
6 | * PHP version 7 |
7 | * |
8 | * Copyright (C) The National Library of Finland 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 for streaming a file. |
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 MarcCollectionFile 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 | * Serialized format stream |
56 | * |
57 | * @var Serialization\SerializationFileInterface |
58 | */ |
59 | protected $stream; |
60 | |
61 | /** |
62 | * Iteration position |
63 | * |
64 | * @var int |
65 | */ |
66 | protected $position = 0; |
67 | |
68 | /** |
69 | * Current record |
70 | * |
71 | * @var string |
72 | */ |
73 | protected $record = ''; |
74 | |
75 | /** |
76 | * Message callback |
77 | * |
78 | * @var callable |
79 | */ |
80 | protected $messageCallback; |
81 | |
82 | /** |
83 | * Constructor |
84 | * |
85 | * @param string $file MARC record collection file in MARCXML or |
86 | * ISO2709 format |
87 | * @param callable $messageCallback Callback triggered for any messages with |
88 | * message string and error level (similar to |
89 | * https://www.php.net/manual/en/function.error-reporting.php) |
90 | */ |
91 | public function __construct(string $file = '', ?callable $messageCallback = null) |
92 | { |
93 | $this->messageCallback = $messageCallback; |
94 | $this->setFile($file); |
95 | } |
96 | |
97 | /** |
98 | * Set MARC record file |
99 | * |
100 | * @param string $file MARC record collection file in MARCXML or ISO2709 format |
101 | * |
102 | * @throws \Exception |
103 | * @return void |
104 | */ |
105 | public function setFile(string $file): void |
106 | { |
107 | if (!file_exists($file)) { |
108 | throw new \Exception("File '$file' does not exist"); |
109 | } |
110 | $this->position = 0; |
111 | $this->record = null; |
112 | foreach ($this->serializations as $serialization) { |
113 | if ($serialization::canParseCollectionFile($file)) { |
114 | $this->stream = new $serialization(); |
115 | if ( |
116 | $this->stream instanceof Serialization\MessageCallbackInterface |
117 | ) { |
118 | $this->stream->setMessageCallback($this->messageCallback); |
119 | } |
120 | $this->stream->openCollectionFile($file); |
121 | return; |
122 | } |
123 | } |
124 | throw new \Exception('MARC collection file format not recognized'); |
125 | } |
126 | |
127 | /** |
128 | * Iterator: Rewind to the beginning. |
129 | * |
130 | * @return void |
131 | */ |
132 | public function rewind(): void |
133 | { |
134 | $this->stream->rewind(); |
135 | $this->position = 0; |
136 | $this->record = null; |
137 | } |
138 | |
139 | /** |
140 | * Iterator: Return current record. |
141 | * |
142 | * @return mixed |
143 | */ |
144 | #[\ReturnTypeWillChange] |
145 | public function current() |
146 | { |
147 | if (null === $this->record) { |
148 | $this->record = $this->stream->getNextRecord(); |
149 | } |
150 | return $this->record ? new MarcReader($this->record) : null; |
151 | } |
152 | |
153 | /** |
154 | * Iterator: Return current key. |
155 | * |
156 | * @return mixed |
157 | */ |
158 | #[\ReturnTypeWillChange] |
159 | public function key() |
160 | { |
161 | return $this->position; |
162 | } |
163 | |
164 | /** |
165 | * Iterator: Advance to the next record. |
166 | * |
167 | * @return void |
168 | */ |
169 | public function next(): void |
170 | { |
171 | ++$this->position; |
172 | $this->record = null; |
173 | } |
174 | |
175 | /** |
176 | * Iterator: Check if current position is valid. |
177 | * |
178 | * @return bool |
179 | */ |
180 | public function valid(): bool |
181 | { |
182 | if (null === $this->record) { |
183 | $this->record = $this->stream->getNextRecord(); |
184 | } |
185 | return '' !== $this->record; |
186 | } |
187 | } |