1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scriptotek\Alma\Bibs; |
4
|
|
|
|
5
|
|
|
use Scriptotek\Alma\Client; |
6
|
|
|
use Scriptotek\Alma\Conf\Library; |
7
|
|
|
use Scriptotek\Alma\Model\LazyResource; |
8
|
|
|
use Scriptotek\Alma\Users\Loan; |
9
|
|
|
use Scriptotek\Alma\Users\User; |
10
|
|
|
|
11
|
|
|
class Item extends LazyResource |
12
|
|
|
{ |
13
|
|
|
/** @var Bib */ |
14
|
|
|
public $bib; |
15
|
|
|
|
16
|
|
|
/** @var Holding */ |
17
|
|
|
public $holding; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
protected $item_id; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Item constructor. |
24
|
|
|
* |
25
|
|
|
* @param Client $client |
26
|
|
|
* @param Bib $bib |
27
|
|
|
* @param Holding $holding |
28
|
|
|
* @param $item_id |
29
|
|
|
*/ |
30
|
|
|
public function __construct(Client $client, Bib $bib, Holding $holding, $item_id) |
31
|
|
|
{ |
32
|
|
|
parent::__construct($client); |
33
|
|
|
$this->bib = $bib; |
34
|
|
|
$this->holding = $holding; |
35
|
|
|
$this->item_id = $item_id; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Generate the base URL for this resource. |
40
|
|
|
* |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
protected function urlBase() |
44
|
|
|
{ |
45
|
|
|
return "/bibs/{$this->bib->mms_id}/holdings/{$this->holding->holding_id}/items/{$this->item_id}"; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Check if we have the full representation of our data object. |
50
|
|
|
* |
51
|
|
|
* @param \stdClass $data |
52
|
|
|
* @return boolean |
53
|
|
|
*/ |
54
|
|
|
protected function isInitialized($data) |
55
|
|
|
{ |
56
|
|
|
return isset($data->item_data); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Called when data is available to be processed. |
61
|
|
|
* |
62
|
|
|
* @param mixed $data |
63
|
|
|
*/ |
64
|
|
|
protected function onData($data) |
65
|
|
|
{ |
66
|
|
|
if (isset($this->bib_data)) { |
67
|
|
|
$this->bib->init($this->bib_data); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
if (isset($this->holding_data)) { |
70
|
|
|
$this->holding->init($this->holding_data); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Create a new loan. |
76
|
|
|
* |
77
|
|
|
* @param User $user |
78
|
|
|
* @param Library $library |
79
|
|
|
* @param string $circ_desk |
80
|
|
|
* @return Loan |
81
|
|
|
* @throws \Scriptotek\Alma\Exception\RequestFailed |
82
|
|
|
*/ |
83
|
|
|
public function checkOut(User $user, Library $library, $circ_desk = 'DEFAULT_CIRC_DESK') |
84
|
|
|
{ |
85
|
|
|
$postData = [ |
86
|
|
|
'library' => ['value' => $library->code], |
87
|
|
|
'circ_desk' => ['value' => $circ_desk], |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
$data = $this->client->postJSON( |
91
|
|
|
$this->url('/loans', ['user_id' => $user->id]), |
92
|
|
|
$postData |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
return Loan::make($this->client, $user, $data->loan_id) |
96
|
|
|
->init($data); |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Perform scan-in on item. |
101
|
|
|
* |
102
|
|
|
* @param Library $library |
103
|
|
|
* @param string $circ_desk |
104
|
|
|
* @param array $params |
105
|
|
|
* @return ScanInResponse |
106
|
|
|
* @throws \Scriptotek\Alma\Exception\RequestFailed |
107
|
|
|
*/ |
108
|
|
|
public function scanIn(Library $library, $circ_desk = 'DEFAULT_CIRC_DESK', $params = []) |
109
|
|
|
{ |
110
|
|
|
$params['op'] = 'scan'; |
111
|
|
|
$params['library'] = $library->code; |
112
|
|
|
$params['circ_desk'] = $circ_desk; |
113
|
|
|
|
114
|
|
|
$data = $this->client->postJSON($this->url('', $params)); |
115
|
|
|
|
116
|
|
|
return ScanInResponse::make($this->client, $data); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the current loan as a Loan object, or null if the item is not loaned out. |
121
|
|
|
* |
122
|
|
|
* @returns Loan|null |
123
|
|
|
*/ |
124
|
|
|
public function getLoan() |
125
|
|
|
{ |
126
|
|
|
$data = $this->client->getJSON($this->url('/loans')); |
127
|
|
|
|
128
|
|
|
if ($data->total_record_count == 1) { |
129
|
|
|
return Loan::make( |
130
|
|
|
$this->client, |
131
|
|
|
User::make($this->client, $data->item_loan[0]->user_id), |
132
|
|
|
$data->item_loan[0]->loan_id |
133
|
|
|
)->init($data->item_loan[0]); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return null; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function __get($key) |
140
|
|
|
{ |
141
|
|
|
if ($key == 'loan') { |
142
|
|
|
return $this->getLoan(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$this->init(); |
146
|
|
|
|
147
|
|
|
if (isset($this->data->item_data->{$key})) { |
148
|
|
|
return $this->data->item_data->{$key}; |
149
|
|
|
} |
150
|
|
|
if (isset($this->data->holding_data->{$key})) { |
151
|
|
|
return $this->data->holding_data->{$key}; |
152
|
|
|
} |
153
|
|
|
if (isset($this->data->bib_data->{$key})) { |
154
|
|
|
return $this->data->bib_data->{$key}; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return parent::__get($key); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.