|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Scriptotek\Alma\Bibs; |
|
4
|
|
|
|
|
5
|
|
|
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement; |
|
6
|
|
|
use Scriptotek\Alma\Client; |
|
7
|
|
|
use Scriptotek\Alma\Exception\NoLinkedNetworkZoneRecordException; |
|
8
|
|
|
use Scriptotek\Alma\Model\LazyResource; |
|
9
|
|
|
use Scriptotek\Marc\Record as MarcRecord; |
|
10
|
|
|
use Scriptotek\Sru\Record as SruRecord; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* A single Bib resource. |
|
14
|
|
|
*/ |
|
15
|
|
|
class Bib extends LazyResource |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
public $mms_id; |
|
19
|
|
|
|
|
20
|
|
|
/* @var Holdings */ |
|
21
|
|
|
public $holdings; |
|
22
|
|
|
|
|
23
|
|
|
/* @var MarcRecord */ |
|
24
|
|
|
protected $_marc; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(Client $client = null, $mms_id = null) |
|
27
|
|
|
{ |
|
28
|
|
|
parent::__construct($client); |
|
|
|
|
|
|
29
|
|
|
$this->mms_id = $mms_id; |
|
30
|
|
|
$this->holdings = Holdings::make($this->client, $this); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Get the model data. This API does not support JSON for editing, |
|
35
|
|
|
* so we fetch XML instead. |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function fetchData() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->client->getXML($this->url()); |
|
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Load MARC record onto this Bib object. Chainable method. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $xml |
|
46
|
|
|
* |
|
47
|
|
|
* @return Bib |
|
48
|
|
|
*/ |
|
49
|
|
|
public function setMarcRecord($xml) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->_marc = MarcRecord::fromString($xml); |
|
|
|
|
|
|
52
|
|
|
// Note: do not marc as initialized, since we miss some other data from the Bib record. Oh, Alma :/ |
|
53
|
|
|
|
|
54
|
|
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Initialize from SRU record without having to fetch the Bib record. |
|
59
|
|
|
* @param SruRecord $record |
|
60
|
|
|
* @param Client|null $client |
|
61
|
|
|
* @return Bib |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function fromSruRecord(SruRecord $record, Client $client = null) |
|
64
|
|
|
{ |
|
65
|
|
|
$record->data->registerXPathNamespace('marc', 'http://www.loc.gov/MARC21/slim'); |
|
66
|
|
|
$mmsId = $record->data->text('.//controlfield[@tag="001"]'); |
|
67
|
|
|
|
|
68
|
|
|
return (new self($client, $mmsId)) |
|
69
|
|
|
->setMarcRecord($record->data->asXML()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* For backwards-compability. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getHoldings() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->holdings; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Save the MARC record. |
|
82
|
|
|
*/ |
|
83
|
|
|
public function save() |
|
84
|
|
|
{ |
|
85
|
|
|
// If initialized from an SRU record, we need to fetch the |
|
86
|
|
|
// remaining parts of the Bib record. |
|
87
|
|
|
$this->init(); |
|
88
|
|
|
|
|
89
|
|
|
// Inject the MARC record |
|
90
|
|
|
$marcXml = new QuiteSimpleXMLElement($this->_marc->toXML('UTF-8', false, false)); |
|
91
|
|
|
$this->data->first('record')->replace($marcXml); |
|
92
|
|
|
|
|
93
|
|
|
// Serialize |
|
94
|
|
|
$newData = $this->data->asXML(); |
|
95
|
|
|
|
|
96
|
|
|
// Alma doesn't like namespaces |
|
97
|
|
|
$newData = str_replace(' xmlns="http://www.loc.gov/MARC21/slim"', '', $newData); |
|
98
|
|
|
|
|
99
|
|
|
return $this->client->putXML($this->url(), $newData); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get the MMS ID of the linked record in network zone. |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getNzMmsId() |
|
106
|
|
|
{ |
|
107
|
|
|
// If initialized from an SRU record, we need to fetch the |
|
108
|
|
|
// remaining parts of the Bib record. |
|
109
|
|
|
$this->init(); |
|
110
|
|
|
|
|
111
|
|
|
$nz_mms_id = $this->data->text("linked_record_id[@type='NZ']"); |
|
112
|
|
|
if (!$nz_mms_id) { |
|
113
|
|
|
throw new NoLinkedNetworkZoneRecordException("Record $this->mms_id is not linked to a network zone record."); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return $nz_mms_id; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Get the Bib of the linked record in network zone. |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getNzRecord() |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->client->nz->bibs->get($this->getNzMmsId()); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Returns the MARC record. Load it if we don't have it yet. |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getRecord() |
|
131
|
|
|
{ |
|
132
|
|
|
if (is_null($this->_marc)) { |
|
133
|
|
|
$this->init(); |
|
134
|
|
|
} |
|
135
|
|
|
return $this->_marc; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Called when data is available to be processed. |
|
140
|
|
|
* |
|
141
|
|
|
* @param mixed $data |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function onData($data) |
|
144
|
|
|
{ |
|
145
|
|
|
$this->setMarcRecord($data->first('record')->asXML()); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Check if we have the full representation of our data object. |
|
150
|
|
|
* |
|
151
|
|
|
* @param $data |
|
152
|
|
|
* @return boolean |
|
153
|
|
|
*/ |
|
154
|
|
|
protected function isInitialized($data) |
|
155
|
|
|
{ |
|
156
|
|
|
return $data->has('record'); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Generate the base URL for this resource. |
|
161
|
|
|
* |
|
162
|
|
|
* @return string |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function urlBase() |
|
165
|
|
|
{ |
|
166
|
|
|
return "/bibs/{$this->mms_id}"; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Magic! |
|
171
|
|
|
* @param string $key |
|
172
|
|
|
* @return mixed |
|
173
|
|
|
*/ |
|
174
|
|
|
public function __get($key) |
|
175
|
|
|
{ |
|
176
|
|
|
// If there's a getter method, call it. |
|
177
|
|
|
if ($key == 'record') { |
|
178
|
|
|
return $this->getRecord(); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
$this->init(); |
|
182
|
|
|
|
|
183
|
|
|
// If the property is defined in our data object, return it. |
|
184
|
|
|
return $this->data->text($key); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):