1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Scriptotek\Alma\Users; |
4
|
|
|
|
5
|
|
|
use Scriptotek\Alma\Client; |
6
|
|
|
use Scriptotek\Alma\GhostModel; |
7
|
|
|
|
8
|
|
|
class User extends GhostModel |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The primary id or some other id that can be used to fetch user information. |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
public $id; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var UserIdentifiers |
18
|
|
|
*/ |
19
|
|
|
protected $_identifiers; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* User constructor. |
23
|
|
|
* |
24
|
|
|
* @param Client $client |
25
|
|
|
* @param string $id |
26
|
|
|
*/ |
27
|
|
|
public function __construct(Client $client, $id) |
28
|
|
|
{ |
29
|
|
|
parent::__construct($client); |
30
|
|
|
$this->id = $id; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get the user id the object was constructed with. This might or might not be the primary id. |
35
|
|
|
* The only usefulness of this method over getPrimaryId() is that it will not trigger loading of the full object. |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public function getUserId() |
40
|
|
|
{ |
41
|
|
|
return $this->id; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the primary id. No need to load the full record for this. |
46
|
|
|
* |
47
|
|
|
* @return string|null |
48
|
|
|
*/ |
49
|
|
|
public function getPrimaryId() |
50
|
|
|
{ |
51
|
|
|
return $this->primary_id; |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the user identifiers. |
56
|
|
|
* |
57
|
|
|
* @return UserIdentifiers |
58
|
|
|
*/ |
59
|
|
|
public function getIdentifiers() |
60
|
|
|
{ |
61
|
|
|
return $this->init()->_identifiers; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Check if we have the full representation of our data object. |
66
|
|
|
* |
67
|
|
|
* @param \stdClass $data |
68
|
|
|
* @return boolean |
69
|
|
|
*/ |
70
|
|
|
protected function isInitialized($data) |
71
|
|
|
{ |
72
|
|
|
return isset($data->user_identifier); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Store data onto object. |
77
|
|
|
* |
78
|
|
|
* @param \stdClass $data |
79
|
|
|
*/ |
80
|
|
|
protected function setData(\stdClass $data) |
81
|
|
|
{ |
82
|
|
|
$this->_identifiers = UserIdentifiers::make($this->client, $data); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Generate the base URL for this resource. |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
protected function urlBase() |
92
|
|
|
{ |
93
|
|
|
return "/users/{$this->id}"; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function __get($key) |
97
|
|
|
{ |
98
|
|
|
// If there's a getter method, call it. |
99
|
|
|
$method = 'get' . ucfirst($key); |
100
|
|
|
if (method_exists($this, $method)) { |
101
|
|
|
return $this->$method(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// If the property is defined in our data object, return it. |
105
|
|
|
if (isset($this->data->{$key})) { |
106
|
|
|
return $this->data->{$key}; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// Load the full record if needed. |
110
|
|
|
$this->init(); |
111
|
|
|
|
112
|
|
|
// If there's a getter method on the UserIdentifiers object |
113
|
|
|
// (getBarcode, getPrimaryId), call it. |
114
|
|
|
if (method_exists($this->identifiers, $method)) { |
|
|
|
|
115
|
|
|
return $this->identifiers->$method(); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
// Re-check if there's a property on our data object |
119
|
|
|
if (isset($this->data->{$key})) { |
120
|
|
|
return $this->data->{$key}; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return null; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
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.