-
Notifications
You must be signed in to change notification settings - Fork 4
/
MarcInJson.php
232 lines (215 loc) · 6.15 KB
/
MarcInJson.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
/**
* MARC-in-JSON format support class.
*
* PHP version 7
*
* Copyright (C) The National Library of Finland 2022.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category VuFind
* @package MARC
* @author Ere Maijala <ere.maijala@helsinki.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:plugins:record_drivers Wiki
*/
namespace VuFind\Marc\Serialization;
use pcrov\JsonReader\JsonReader;
use function is_array;
use function strlen;
/**
* MARC-in-JSON format support class.
*
* @category VuFind
* @package MARC
* @author Ere Maijala <ere.maijala@helsinki.fi>
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License
* @link https://vufind.org/wiki/development:plugins:record_drivers Wiki
*/
class MarcInJson extends AbstractSerializationFile implements SerializationInterface
{
/**
* Current file
*
* @var string
*/
protected $fileName = '';
/**
* JSON Reader for current file
*
* @var JsonReader
*/
protected $reader = null;
/**
* Check if this class can parse the given MARC string
*
* @param string $marc MARC
*
* @return bool
*/
public static function canParse(string $marc): bool
{
// A pretty naïve check, but it's enough to tell the different formats apart
return substr(trim($marc), 0, 1) === '{';
}
/**
* Check if the serialization class can parse the given MARC collection string
*
* @param string $marc MARC
*
* @return bool
*/
public static function canParseCollection(string $marc): bool
{
// A pretty naïve check, but it's enough to tell the different formats apart
return substr(trim($marc), 0, 1) === '[';
}
/**
* Check if the serialization class can parse the given MARC collection file
*
* @param string $file File name
*
* @return bool
*/
public static function canParseCollectionFile(string $file): bool
{
if (false === ($f = fopen($file, 'rb'))) {
throw new \Exception("Cannot open file '$file' for reading");
}
$s = '';
do {
$s .= fgets($f, 10);
} while (strlen(ltrim($s)) < 5 && !feof($f));
fclose($f);
return self::canParseCollection($s);
}
/**
* Parse MARC collection from a string into an array
*
* @param string $collection MARC record collection in the format supported by
* the serialization class
*
* @throws \Exception
* @return array
*/
public static function collectionFromString(string $collection): array
{
return json_decode($collection, true);
}
/**
* Parse MARC-in-JSON
*
* @param string $marc JSON
*
* @throws \Exception
* @return array
*/
public static function fromString(string $marc): array
{
return json_decode($marc, true);
}
/**
* Convert record to ISO2709 string
*
* @param array $record Record data
*
* @return string
*/
public static function toString(array $record): string
{
return self::jsonEncode($record);
}
/**
* Open a collection file
*
* @param string $file File name
*
* @return void
*
* @throws \Exception
*/
public function openCollectionFile(string $file): void
{
$this->fileName = $file;
$this->reader = new JsonReader();
$this->reader->open($file);
// Move into the record array:
$this->reader->read();
}
/**
* Rewind the collection file
*
* @return void
*
* @throws \Exception
*/
public function rewind(): void
{
if ('' === $this->fileName) {
throw new \Exception('Collection file not open');
}
$this->openCollectionFile($this->fileName);
}
/**
* Get next record from the file or an empty string on EOF
*
* @return string
*
* @throws \Exception
*/
public function getNextRecord(): string
{
if (null === $this->reader) {
throw new \Exception('Collection file not open');
}
// We have to rely on the depth since the elements are anonymous:
if ($this->reader->depth() === 0) {
// Level 0 is the array enclosing the record objects, read into it:
$this->reader->read();
} else {
// Level 1 is an object, get the next one:
$this->reader->next();
}
$value = $this->reader->value();
return $value ? self::jsonEncode($value) : '';
}
/**
* Convert a record array to a JSON string
*
* @param array $record Record
*
* @return string
*/
protected static function jsonEncode(array $record): string
{
// We need to cast any subfield with '0' as key to an object; otherwise it
// would be encoded as a simple array instead of an object:
foreach ($record['fields'] as &$fieldData) {
$field = current($fieldData);
if (!is_array($field)) {
continue;
}
foreach ($field['subfields'] as &$subfield) {
if (key($subfield) == 0) {
$subfield = (object)$subfield;
}
}
unset($subfield);
$fieldData = [key($fieldData) => $field];
}
unset($fieldData);
return json_encode($record, JSON_UNESCAPED_UNICODE);
}
}