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