1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scriptotek\SimpleMarcParser; |
4
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
6
|
|
|
use Danmichaelo\QuiteSimpleXmlElement\QuiteSimpleXmlElement; |
7
|
|
|
|
8
|
|
|
class Record |
9
|
|
|
{ |
10
|
|
|
protected $data; |
11
|
|
|
|
12
|
|
|
public static $subfieldSeparator = '--'; |
13
|
|
|
|
14
|
|
|
public function __get($name) |
15
|
|
|
{ |
16
|
|
|
if (isset($this->data[$name])) { |
17
|
|
|
return $this->data[$name]; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
return; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function __set($name, $value) |
24
|
|
|
{ |
25
|
|
|
if (is_null($value)) { |
26
|
|
|
unset($this->data[$name]); |
27
|
|
|
} elseif (is_string($value) && empty($value)) { |
28
|
|
|
unset($this->data[$name]); |
29
|
|
|
} else { |
30
|
|
|
$this->data[$name] = $value; |
31
|
|
|
} |
32
|
|
|
// } |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function __isset($name) |
36
|
|
|
{ |
37
|
|
|
return isset($this->data[$name]); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function __unset($name) |
41
|
|
|
{ |
42
|
|
|
unset($this->data[$name]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Convert the object to its JSON representation. |
47
|
|
|
* |
48
|
|
|
* @param int $options |
49
|
|
|
* |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function toJson($options = 0) |
53
|
|
|
{ |
54
|
|
|
return json_encode($this->toArray(), $options); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Convert the model instance to an array. |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public function toArray() |
63
|
|
|
{ |
64
|
|
|
return $this->data; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Parse a *Representation of Dates and Times* (ISO 8601). |
69
|
|
|
* The date requires 8 numeric characters in the pattern yyyymmdd. |
70
|
|
|
* The time requires 8 numeric characters in the pattern hhmmss.f, |
71
|
|
|
* expressed in terms of the 24-hour (00-23) clock. |
72
|
|
|
* |
73
|
|
|
* @param string $value |
74
|
|
|
* |
75
|
|
|
* @return Carbon|null |
76
|
|
|
*/ |
77
|
|
|
protected function parseDateTime($value) |
78
|
|
|
{ |
79
|
|
|
if (strlen($value) == 6) { |
80
|
|
|
return Carbon::createFromFormat('ymdHis', $value . '000000'); |
81
|
|
|
} |
82
|
|
|
if (strlen($value) == 8) { |
83
|
|
|
return Carbon::createFromFormat('YmdHis', $value . '000000'); |
84
|
|
|
} |
85
|
|
|
if (strlen($value) == 16) { |
86
|
|
|
return Carbon::createFromFormat('YmdHis', substr($value, 0, 14)); |
87
|
|
|
} // skip decimal fraction |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Parse a "name node", personal or corporate, main or added, that |
92
|
|
|
* might have authority information encapsulated. |
93
|
|
|
* |
94
|
|
|
* @param string $authority |
95
|
|
|
* @param array &$out |
96
|
|
|
*/ |
97
|
|
|
protected function parseAuthority($authority, &$out) |
98
|
|
|
{ |
99
|
|
|
if (!empty($authority)) { |
100
|
|
|
$out['id'] = $authority; |
101
|
|
|
if (preg_match('/\((.*?)\)(.*)/', $authority, $matches)) { |
102
|
|
|
// As used by at least OCLC and Bibsys |
103
|
|
|
$out['vocabulary'] = $matches[1]; |
104
|
|
|
$out['id'] = $matches[2]; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Parse a "name node", personal or corporate, main or added, that |
111
|
|
|
* might have relators encapsulated. |
112
|
|
|
* |
113
|
|
|
* @param QuiteSimpleXmlElement &$node |
114
|
|
|
* @param array &$out |
115
|
|
|
* @param string $default |
116
|
|
|
*/ |
117
|
|
|
protected function parseRelator(&$node, &$out, $default = null) |
118
|
|
|
{ |
119
|
|
|
$relterm = $node->text('marc:subfield[@code="e"]'); |
120
|
|
|
$relcode = $node->text('marc:subfield[@code="4"]'); |
121
|
|
|
if (!empty($relcode)) { |
122
|
|
|
$out['role'] = $relcode; |
123
|
|
|
} elseif (!empty($relterm)) { |
124
|
|
|
$out['role'] = $relterm; |
125
|
|
|
} elseif (!is_null($default)) { |
126
|
|
|
$out['role'] = $default; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Parse a "relationship node", one that have links to other records encapsulated. |
132
|
|
|
* |
133
|
|
|
* @param QuiteSimpleXmlElement $node |
134
|
|
|
* |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
protected function parseRelationship($node) |
138
|
|
|
{ |
139
|
|
|
$rel = array(); |
140
|
|
|
|
141
|
|
|
$x = preg_replace('/\(.*?\)/', '', $node->text('marc:subfield[@code="w"]')); |
142
|
|
|
if (!empty($x)) { |
143
|
|
|
$rel['id'] = $x; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$x = $node->text('marc:subfield[@code="t"]'); |
147
|
|
|
if (!empty($x)) { |
148
|
|
|
$rel['title'] = $x; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$x = $node->text('marc:subfield[@code="g"]'); |
152
|
|
|
if (!empty($x)) { |
153
|
|
|
$rel['parts'] = $x; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$x = $node->text('marc:subfield[@code="x"]'); |
157
|
|
|
if (!empty($x)) { |
158
|
|
|
$rel['issn'] = $x; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
$x = $node->text('marc:subfield[@code="z"]'); |
162
|
|
|
if (!empty($x)) { |
163
|
|
|
$rel['isbn'] = $x; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $rel; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|