-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFile_MARC_Reference_Cache.php
More file actions
281 lines (258 loc) · 7.61 KB
/
File_MARC_Reference_Cache.php
File metadata and controls
281 lines (258 loc) · 7.61 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
class File_MARC_Reference_Cache implements ArrayAccess
{
/**
* @var array Array of fields or subfields
*/
protected $data = [];
/**
* @var array Associative Array of specs
*/
protected $spec = [];
/**
* @var array Associative Array of validation results
*/
protected $valid = [];
/**
* Cache a spec.
*
* This is a cache control method for specs
*
* @param string|MARCspecInterface|SubSpecInterface $spec The spec to cache
* @param bool|null $value Validation result for a subspec
*
* @return mixed Returns either a spec or a validation result
*/
public function spec($spec, $value = null)
{
$key = "$spec";
if (array_key_exists($key, $this->spec)) {
return $this->spec[$key]; // return since spec is already cached
}
// cache a spec
if (!is_null($value)) {
return $this->spec[$key] = $value;
}
if (is_string($spec)) {
$spec = new CK\MARCspec\MARCspec($spec);
}
$this->spec[$key] = $spec;
$cmp = $this->spec[$key]->__toString();
if ($cmp !== $key) {
$this->spec[$cmp] = $this->spec[$key];
}
return $this->spec[$key];
}
/**
* Set or get subspec validation.
*
* @param SubSpecInterface|string $subSpec The validated subspec
* @param bool|null $value Validation result true or false
*
* @return bool|null If subspec was already validated return true or false.
* Returns null, if subspec was not already validated.
*/
public function validation($subSpec, $value = null)
{
$key = "$subSpec";
if (is_null($value)) {
if (array_key_exists($key, $this->valid)) {
return $this->valid[$key];
} else {
return;
}
}
$this->valid[$key] = $value;
}
/**
* Set data.
*
* @param FieldInterface|SubfieldInterface|string $key Datas spec
* @param bool|null $value The data
*/
protected function setData($key, $value)
{
$this->data[$key] = (is_array($value)) ? $value : [$value];
}
/**
* Get the data.
*
* Extract data from $this->data depending on provided specs
*
* @param FieldInterface $fieldspec Datas field spec
* @param SubfieldInterface $subfieldspec Datas subfield spec
*
* @return array[File_MARC_Field|File_MARC_Subfield] Array of data
*/
public function getData(CK\MARCspec\FieldInterface $fieldspec, CK\MARCspec\SubfieldInterface $subfieldspec = null)
{
if (!$this->data) {
return [];
}
$fieldBase = $fieldspec->getBaseSpec();
if (!is_null($subfieldspec)) {
$key = $fieldBase.$subfieldspec->getBaseSpec();
if (array_key_exists($key, $this->data)) {
return $this->data[$key];
}
$key = $fieldBase.'$'.$subfieldspec->getTag();
if (array_key_exists($key, $this->data)) {
return array_values($this->sliceData($key, $subfieldspec));
}
return [];
}
// subfieldspec is null
if (array_key_exists($fieldBase, $this->data)) {
return $this->data[$fieldBase];
}
$key = $fieldspec->getTag();
if (array_key_exists($key, $this->data)) {
return array_values($this->sliceData($key, $fieldspec));
}
return [];
}
/**
* Get a slice from data.
*
* @param string $key The datas key
* @param FieldInterface|SubfieldInterface $spec Spec with slice information
*
* @return array[File_MARC_Field|File_MARC_Subfield]
*/
private function sliceData($key, $spec)
{
$start = $spec->getIndexStart();
if ('#' === $start) { // reverse index order
$end = count($this->data[$key]) - 1;
$start = $spec->getIndexEnd();
if ('#' === $start) {
return [$this->data[$key][$end]];
}
$length = $end - $start;
return array_slice($this->data[$key], $start, $length);
}
$end = $spec->getIndexEnd();
if ('#' === $end) {
$end = count($this->data[$key]) - 1;
}
$length = $end - $start + 1;
return array_slice($this->data[$key], $start, $length);
}
/**
* Get referenced data content.
*
* @param FieldInterface|SubfieldInterface $spec The corresponding spec
* @param array[File_MARC_Field|File_MARC_Subfield] $value The value to reference
*
* @return array[string] Array of referenced data content
*/
public function getContents($spec, array $value = [])
{
if (!$value) {
$value = $this->getData("$spec");
}
$substring = false;
$charStart = null;
$length = null;
if ($spec->offsetExists('charStart')) {
$substring = true;
$charStart = $spec['charStart'];
$length = $spec['charLength'] * 1;
if ('#' === $charStart) {
// negative index
$charStart = $length * -1;
}
}
array_walk(
$value,
function (&$val, $key) use ($substring, $charStart, $length) {
/*
* Convert to string
*/
// leader
if (is_string($val)) {
// value stays untouched
} elseif ($val instanceof File_MARC_Data_Field) {
$val = $val->getContents();
} else {
$val = $val->getData();
}
/*
* Get substring
*/
if ($substring) {
$val = substr($val, $charStart, $length);
}
}
);
return $value;
}
/**
* Assigns a value to the specified offset.
*
* @param string $key The offset to assign the value to
* @param File_MARC_Field|File_MARC_Subfield $value The value to set
*
*
* @abstracting ArrayAccess
*/
public function offsetSet($key, $value)
{
$this->setData("$key", $value);
}
/**
* Whether or not an offset exists.
*
* @param string $key An offset to check for
*
* @return bool
*
* @abstracting ArrayAccess
*/
public function offsetExists($key)
{
return array_key_exists("$key", $this->data);
}
/**
* Unsets an offset.
*
* @param string $key The offset to unset
*
*
* @abstracting ArrayAccess
*/
public function offsetUnset($key)
{
unset($this->data["$key"]);
}
/**
* Returns the value at specified offset.
*
* @param string $key The offset to retrieve
*
* @return array[File_MARC_Field|File_MARC_Subfield]
*
* @abstracting ArrayAccess
*/
public function offsetGet($key)
{
$key = "$key";
if (array_key_exists($key, $this->data)) {
return $this->data[$key];
}
return [];
}
/**
* Get protected attributes.
*
* @param string $name The property name
*
* @return array[File_MARC_Field|File_MARC_Subfield]
*/
public function __get($name)
{
if ('data' == $name) {
return $this->data;
}
}
}