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