|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Scriptotek\Alma\Users; |
|
4
|
|
|
|
|
5
|
|
|
use Scriptotek\Alma\Client; |
|
6
|
|
|
|
|
7
|
|
|
class User |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* This class is a ghost object that lazy loads the full record only when needed. |
|
11
|
|
|
* If $initialized is false, it means we haven't yet loaded the full record. |
|
12
|
|
|
* We can still have incomplete data from a search response. |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $initialized = false; |
|
15
|
|
|
|
|
16
|
|
|
protected $client; |
|
17
|
|
|
|
|
18
|
|
|
protected $id; |
|
19
|
|
|
|
|
20
|
|
|
protected $_identifiers; |
|
21
|
|
|
|
|
22
|
|
|
/* @var \stdClass */ |
|
23
|
|
|
protected $data; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Create a user from a search response containing an incomplete user record. |
|
27
|
|
|
* |
|
28
|
|
|
* @param Client $client |
|
29
|
|
|
* @param \stdClass $data |
|
30
|
|
|
* |
|
31
|
|
|
* @return User |
|
32
|
|
|
*/ |
|
33
|
|
|
public static function fromSearchResponse(Client $client, \stdClass $data) |
|
34
|
|
|
{ |
|
35
|
|
|
return (new self($client, $data->primary_id)) |
|
36
|
|
|
->init($data); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* User constructor. |
|
41
|
|
|
* |
|
42
|
|
|
* @param Client $client |
|
43
|
|
|
* @param string $id |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(Client $client, $id) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->client = $client; |
|
48
|
|
|
$this->id = $id; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Load data on this User object. Chainable method. |
|
53
|
|
|
* |
|
54
|
|
|
* @param \stdClass $data |
|
55
|
|
|
* |
|
56
|
|
|
* @return User |
|
57
|
|
|
*/ |
|
58
|
|
|
public function init($data = null) |
|
59
|
|
|
{ |
|
60
|
|
|
if ($this->initialized) { |
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
if (is_null($data)) { |
|
65
|
|
|
$data = $this->client->getJSON('/users/' . $this->id); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (isset($data->user_identifier)) { |
|
69
|
|
|
$this->_identifiers = new UserIdentifiers($data->primary_id, $data->user_identifier); |
|
70
|
|
|
$this->initialized = true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$this->data = $data; |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get the complete user record. |
|
80
|
|
|
* |
|
81
|
|
|
* @return \stdClass |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getData() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->init()->data; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get the primary id. No need to load the full record for this. |
|
90
|
|
|
* |
|
91
|
|
|
* @return string|null |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getPrimaryId() |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->primary_id; |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get the user identifiers. |
|
100
|
|
|
* |
|
101
|
|
|
* @return UserIdentifier[] |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getIdentifiers() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->init()->_identifiers; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Magic! |
|
110
|
|
|
*/ |
|
111
|
|
|
public function __get($key) |
|
112
|
|
|
{ |
|
113
|
|
|
// If there's a getter method, call it. |
|
114
|
|
|
$method = 'get' . ucfirst($key); |
|
115
|
|
|
if (method_exists($this, $method)) { |
|
116
|
|
|
return $this->$method(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// If the property is defined in our data object, return it. |
|
120
|
|
|
if (isset($this->data->{$key})) { |
|
121
|
|
|
return $this->data->{$key}; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
// Load the full record if needed. |
|
125
|
|
|
$this->init(); |
|
126
|
|
|
|
|
127
|
|
|
// If there's a getter method on the UserIdentifiers object |
|
128
|
|
|
// (getBarcode, getPrimaryId), call it. |
|
129
|
|
|
if (method_exists($this->identifiers, $method)) { |
|
|
|
|
|
|
130
|
|
|
return $this->identifiers->$method(); |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
// Re-check if there's a property on our data object |
|
134
|
|
|
if (isset($this->data->{$key})) { |
|
135
|
|
|
return $this->data->{$key}; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
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@propertyannotation 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.