1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scriptotek\Alma\Bibs; |
4
|
|
|
|
5
|
|
|
use Scriptotek\Alma\Client; |
6
|
|
|
use Scriptotek\Alma\Exception\NoLinkedNetworkZoneRecordException; |
7
|
|
|
use Scriptotek\Alma\Model\LazyResource; |
8
|
|
|
use Scriptotek\Marc\Record as MarcRecord; |
9
|
|
|
use Scriptotek\Sru\Record as SruRecord; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A single Bib resource. |
13
|
|
|
*/ |
14
|
|
|
class Bib extends LazyResource |
15
|
|
|
{ |
16
|
|
|
/** @var string */ |
17
|
|
|
public $mms_id; |
18
|
|
|
|
19
|
|
|
/* @var Holdings */ |
20
|
|
|
public $holdings; |
21
|
|
|
|
22
|
|
|
/* @var MarcRecord */ |
23
|
|
|
protected $_marc; |
24
|
|
|
|
25
|
|
|
public function __construct(Client $client = null, $mms_id = null) |
26
|
|
|
{ |
27
|
|
|
parent::__construct($client); |
|
|
|
|
28
|
|
|
$this->mms_id = $mms_id; |
29
|
|
|
$this->holdings = Holdings::make($this->client, $this); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Load MARC record onto this Bib object. Chainable method. |
34
|
|
|
* |
35
|
|
|
* @param string $xml |
36
|
|
|
* |
37
|
|
|
* @return Bib |
38
|
|
|
*/ |
39
|
|
|
public function setMarcRecord($xml) |
40
|
|
|
{ |
41
|
|
|
// Workaround for a long-standing API-bug |
42
|
|
|
$xml = str_replace('UTF-16', 'UTF-8', $xml); |
43
|
|
|
|
44
|
|
|
$this->_marc = MarcRecord::fromString($xml); |
|
|
|
|
45
|
|
|
// Note: do not marc as initialized, since we miss some other data from the Bib record. Oh, Alma :/ |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Initialize from SRU record without having to fetch the Bib record. |
52
|
|
|
* @param SruRecord $record |
53
|
|
|
* @param Client|null $client |
54
|
|
|
* @return Bib |
55
|
|
|
*/ |
56
|
|
|
public static function fromSruRecord(SruRecord $record, Client $client = null) |
57
|
|
|
{ |
58
|
|
|
$record->data->registerXPathNamespace('marc', 'http://www.loc.gov/MARC21/slim'); |
59
|
|
|
$mmsId = $record->data->text('.//controlfield[@tag="001"]'); |
60
|
|
|
|
61
|
|
|
return (new self($client, $mmsId)) |
62
|
|
|
->setMarcRecord($record->data->asXML()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getHoldings() |
66
|
|
|
{ |
67
|
|
|
return $this->holdings; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function save() |
71
|
|
|
{ |
72
|
|
|
// If initialized from an SRU record, we need to fetch the |
73
|
|
|
// remaining parts of the Bib record. |
74
|
|
|
$this->init(); |
75
|
|
|
|
76
|
|
|
// Serialize the MARC record |
77
|
|
|
$data = $this->_marc->toXML('UTF-8', false, false); |
78
|
|
|
|
79
|
|
|
// but wait, Alma hates namespaces, so we have to remove them... |
80
|
|
|
$data = str_replace(' xmlns="http://www.loc.gov/MARC21/slim"', '', $data); |
81
|
|
|
|
82
|
|
|
$this->data->anies = [$data]; |
83
|
|
|
|
84
|
|
|
return $this->client->putJSON('/bibs/' . $this->mms_id, $data); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the MMS ID of the linked record in network zone. |
89
|
|
|
*/ |
90
|
|
|
public function getNzMmsId() |
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
|
|
|
// @TODO: What if record is also linked to CZ? Probably an array is returned. |
97
|
|
|
$nz_mms_id = $this->data->linked_record_id->value; |
98
|
|
|
if (!$nz_mms_id) { |
99
|
|
|
throw new NoLinkedNetworkZoneRecordException("Record $this->mms_id is not linked to a network zone record."); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $nz_mms_id; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the Bib of the linked record in network zone. |
107
|
|
|
*/ |
108
|
|
|
public function getNzRecord() |
109
|
|
|
{ |
110
|
|
|
return $this->client->nz->bibs->get($this->getNzMmsId()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns the MARC record. Load it if we don't have it yet. |
115
|
|
|
*/ |
116
|
|
|
public function getRecord() |
117
|
|
|
{ |
118
|
|
|
if (is_null($this->_marc)) { |
119
|
|
|
$this->init(); |
120
|
|
|
} |
121
|
|
|
return $this->_marc; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Called when data is available to be processed. |
126
|
|
|
* |
127
|
|
|
* @param mixed $data |
128
|
|
|
*/ |
129
|
|
|
protected function onData($data) |
130
|
|
|
{ |
131
|
|
|
$this->setMarcRecord($data->anies[0]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Check if we have the full representation of our data object. |
136
|
|
|
* |
137
|
|
|
* @param \stdClass $data |
138
|
|
|
* @return boolean |
139
|
|
|
*/ |
140
|
|
|
protected function isInitialized($data) |
141
|
|
|
{ |
142
|
|
|
return isset($data->anies); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Generate the base URL for this resource. |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
protected function urlBase() |
151
|
|
|
{ |
152
|
|
|
return "/bibs/{$this->mms_id}"; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
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):