1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scriptotek\Alma\Users; |
4
|
|
|
|
5
|
|
|
use Scriptotek\Alma\Client; |
6
|
|
|
use Scriptotek\Alma\Model\LazyResource; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* A single User resource. |
10
|
|
|
*/ |
11
|
|
|
class User extends LazyResource |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The primary id or some other id that can be used to fetch user information. |
15
|
|
|
* |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
public $id; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var UserIdentifiers |
22
|
|
|
*/ |
23
|
|
|
protected $_identifiers; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ContactInfo |
27
|
|
|
*/ |
28
|
|
|
protected $_contact_info; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var UserStatistics |
32
|
|
|
*/ |
33
|
|
|
protected $_user_statistics; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Loans |
37
|
|
|
*/ |
38
|
|
|
public $loans; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Fees |
42
|
|
|
*/ |
43
|
|
|
public $fees; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Requests |
47
|
|
|
*/ |
48
|
|
|
public $requests; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* User constructor. |
52
|
|
|
* |
53
|
|
|
* @param Client $client |
54
|
|
|
* @param string $id |
55
|
|
|
*/ |
56
|
|
|
public function __construct(Client $client, $id) |
57
|
|
|
{ |
58
|
|
|
parent::__construct($client); |
59
|
|
|
$this->id = $id; |
60
|
|
|
$this->loans = Loans::make($this->client, $this); |
61
|
|
|
$this->fees = Fees::make($this->client, $this); |
62
|
|
|
$this->requests = Requests::make($this->client, $this->url('/requests')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the user id the object was constructed with. This might or might not be the primary id. |
67
|
|
|
* The only usefulness of this method over getPrimaryId() is that it will not trigger loading of the full object. |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function getUserId() |
72
|
|
|
{ |
73
|
|
|
return $this->id; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the primary id. No need to load the full record for this. |
78
|
|
|
* |
79
|
|
|
* @return string|null |
80
|
|
|
*/ |
81
|
|
|
public function getPrimaryId() |
82
|
|
|
{ |
83
|
|
|
return $this->primary_id; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the user identifiers. |
88
|
|
|
* |
89
|
|
|
* @return UserIdentifiers |
90
|
|
|
*/ |
91
|
|
|
public function getIdentifiers() |
92
|
|
|
{ |
93
|
|
|
return $this->init()->_identifiers; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the user's contact info. |
98
|
|
|
*/ |
99
|
|
|
public function getContactInfo() |
100
|
|
|
{ |
101
|
|
|
return $this->init()->_contact_info; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the user's statistics info. |
106
|
|
|
*/ |
107
|
|
|
public function getStatistics() |
108
|
|
|
{ |
109
|
|
|
return $this->init()->_user_statistics; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Save the user |
114
|
|
|
* |
115
|
|
|
* @return string The API response body |
116
|
|
|
*/ |
117
|
|
|
public function save() |
118
|
|
|
{ |
119
|
|
|
$this->init(); |
120
|
|
|
return $this->client->put($this->url(), json_encode($this->jsonSerialize())); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Check if we have the full representation of our data object. |
125
|
|
|
* |
126
|
|
|
* @param \stdClass $data |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
|
protected function isInitialized($data) |
131
|
|
|
{ |
132
|
|
|
return isset($data->user_identifier); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Called when data is available to be processed. |
137
|
|
|
* |
138
|
|
|
* @param mixed $data |
139
|
|
|
*/ |
140
|
|
|
protected function onData($data) |
141
|
|
|
{ |
142
|
|
|
$this->_identifiers = UserIdentifiers::make($this->client, $data); |
143
|
|
|
$this->_contact_info = ContactInfo::make($this->client, $data->contact_info); |
144
|
|
|
$this->_user_statistics = UserStatistics::make($this->client, $data->user_statistic); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Generate the base URL for this resource. |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
protected function urlBase() |
153
|
|
|
{ |
154
|
|
|
return sprintf('/users/%s', rawurlencode($this->id)); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function __get($key) |
158
|
|
|
{ |
159
|
|
|
// If there's a getter method, call it. |
160
|
|
|
$method = 'get' . ucfirst($key); |
161
|
|
|
if (method_exists($this, $method)) { |
162
|
|
|
return $this->$method(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
// If the property is defined in our data object, return it. |
166
|
|
|
if (isset($this->data->{$key})) { |
167
|
|
|
return $this->data->{$key}; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// Load the full record if needed. |
171
|
|
|
$this->init(); |
172
|
|
|
|
173
|
|
|
// If there's a getter method on the UserIdentifiers object |
174
|
|
|
// (getBarcode, getPrimaryId), call it. |
175
|
|
|
if (method_exists($this->identifiers, $method)) { |
176
|
|
|
return $this->identifiers->$method(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// Re-check if there's a property on our data object |
180
|
|
|
if (isset($this->data->{$key})) { |
181
|
|
|
return $this->data->{$key}; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|