forked from Nchalenko/okta-sdk-php
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractResource.php
More file actions
387 lines (344 loc) · 10.4 KB
/
Copy pathAbstractResource.php
File metadata and controls
387 lines (344 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
/******************************************************************************
* Copyright 2017 Okta, Inc. *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
******************************************************************************/
namespace Okta\Resource;
use Carbon\Carbon;
use Okta\Client;
use Okta\DataStore\DefaultDataStore;
/**
* Class AbstractResource
* @package Okta\Resource
*/
abstract class AbstractResource
{
/**
* @var DefaultDataStore $dataStore The DataStore that will be used for requests.
*/
private $dataStore;
/**
* @var \StdClass $properties List of properties on the resource.
*/
private $properties;
/**
* @var \StdClass $dirtyProperties List of any properties that have been updated since get/save.
*/
private $dirtyProperties;
/**
* @var bool $materialized Has the resource been full retrieved from the API.
*/
private $materialized;
/**
* @var bool $dirty Are there dirty properties on the resource.
*/
private $dirty;
/**
* @var array $options Current options used on the resource
*/
private $options;
/**
* @var array|bool $methods List of methods on the resource.
*/
private $methods;
const ID = "id";
/**
* AbstractResource constructor.
*
* @param DefaultDataStore|NULL $dataStore DataStore to use for requests.
* @param \stdClass|NULL $properties Properties for the resource.
* @param array $options Options to use on the resource.
*/
public function __construct(?DefaultDataStore $dataStore = null, ?\stdClass $properties = null, array $options = [])
{
$this->dataStore = $dataStore ?: Client::getInstance()->getDataStore();
$this->setProperties($properties);
$this->options = $options;
$this->methods = array_flip(get_class_methods(get_class($this)));
}
/**
* Gets the href property.
*
* @return string
*/
public function getHref()
{
return $this->getLinkProperty('self.href');
}
/**
* Set properties on the resource.
*
* @param \stdClass|NULL $properties the properties to set.
* @return self
*/
public function setProperties(?\stdClass $properties = null): self
{
$this->dirty = false;
$this->properties = new \stdClass;
$this->dirtyProperties = new \stdClass;
if ($properties) {
foreach ($properties as $key => $value) {
$this->properties->{$key} = $value;
}
}
return $this;
}
/**
* Gets a property off the resource and allow you to cast it to a type.
*
* @param string $name Property to get off the resource.
* @param string|null $castTo Cast your item to this type.
* @return mixed|null
*/
public function getProperty($name, $castTo = null)
{
$property = $this->readProperty($name);
if (null !== $castTo) {
settype($property, $castTo);
}
return $property;
}
/**
* Get a HAL Linked property.
*
* @param string $key The property you want to get from the _links section.
* @return mixed|null
*/
public function getLinkProperty($key)
{
$target = $this->getProperty('_links');
$key = is_array($key) ? $key : explode('.', $key);
while (($segment = array_shift($key)) !== null) {
if (!property_exists($target, $segment)) {
throw new \InvalidArgumentException("Property {$segment} does not exists.");
}
$target = $target->{$segment};
}
return $target;
}
/**
* Returns an instance of Carbon for the date property.
*
* @param string $name Get this date property of the resource.
* @return Carbon|null
*/
public function getDateProperty($name)
{
$value = $this->readProperty($name);
if (null === $value) {
return null;
}
return new Carbon($value);
}
/**
* Gets the current property names for the resource.
*
* @param bool $retrieveDirtyProperties determines if you want to get only dirty properties with id.
* @return array
*/
public function getPropertyNames($retrieveDirtyProperties = false)
{
if ($retrieveDirtyProperties and $this->isDirty() and !$this->isNew()) {
return $this->getDirtyPropertyNames();
} else {
return array_keys((array) $this->properties);
}
}
/**
* Gets the property names that have been updated.
*
* @return array
*/
protected function getDirtyPropertyNames()
{
$names = array_keys((array) $this->dirtyProperties);
if (property_exists($this->properties, self::ID)) {
array_push($names, self::ID);
}
return $names;
}
/**
* Set the current options to use for the resource.
*
* @param array $options The options to set on the resource, typically query params.
*
* @return self
*/
public function setOptions($options): self
{
$this->options = $options;
return $this;
}
/**
* Clears all options on the resource.
*
* @return self
*/
public function clearOptions(): self
{
$this->options = null;
return $this;
}
/**
* Get the options currently set on the resource.
*
* @return array
*/
public function getOptions()
{
return $this->options;
}
/**
* Get a resource property from the resource.
*
* @param string $key The name of the resource property you want to get.
* @param string $className The class name you want to return as.
* @param array $options Additional options to pass, Typically query params.
*
* @return AbstractResource
*/
protected function getResourceProperty($key, $className, array $options = array()): AbstractResource
{
$this->options = array_replace($this->options, $options);
$value = $this->getProperty($key);
return $this->getDataStore()->instantiate($className, $value, $this->options);
}
/**
* Set a resource property on a resource.
*
* @param string $name Which property do you want to set.
* @param AbstractResource $resource What you want to set the property as.
*
* @return void
*/
protected function setResourceProperty($name, AbstractResource $resource)
{
$this->setProperty($name, $resource->properties);
}
/**
* Set a property on a resource.
*
* @param string $name Which property do you want to set.
* @param mixed $value What you want to set the property as.
*
* @return void
*/
protected function setProperty($name, $value)
{
$this->properties->$name = $value;
$this->dirtyProperties->$name = $value;
$this->dirty = true;
}
/**
* Get the current DataStore instance.
*
* @return DefaultDataStore
*/
protected function getDataStore()
{
return $this->dataStore;
}
/**
* Is the resource dirty.
*
* Has the resource been updated since you retrieved it from the API.
*
* @return bool
*/
protected function isDirty()
{
return $this->dirty;
}
/**
* Gets the ID property off the resource.
*
* @return string|null
*/
public function getId()
{
return $this->getProperty(self::ID);
}
/**
* Determines if the resource is new or not based on assigned id property.
*
* @return bool
*/
protected function isNew()
{
$prop = $this->readProperty(self::ID);
if ($prop) {
return false;
}
return true;
}
/**
* Will read the property from the resource if it exists or return null if it does not.
*
* @param string $name The property you want to read.
* @return mixed|null
*/
private function readProperty($name)
{
return property_exists($this->properties, $name) ? $this->properties->$name : null;
}
/**
* Magic "get" method
*
* @param string $property Property name
* @return mixed|null Property value if it exists, null if not
*/
public function __get($property)
{
$method = 'get' .ucfirst($property);
if (isset($this->methods[$method])) {
return $this->{$method}();
}
if (null === $this->readProperty($property)) {
return null;
}
return $this->properties->{$property};
}
/**
* Magic "set" method
*
* @param string $property Property name
* @param mixed $value Property value
*
* @return self
*/
public function __set($property, $value)
{
$method = 'set' .ucfirst($property);
if (isset($this->methods[$method])) {
return $this->{$method}($value);
}
$this->setProperty($property, $value);
// $this->properties->{$property} = $value;
return $this;
}
/**
* Returns a json string representation of the resource.
*
* @return string
*/
public function __toString()
{
$propertyNames = $this->getPropertyNames();
$properties = new \stdClass();
foreach ($propertyNames as $name) {
$properties->$name = $this->getProperty($name);
}
return json_encode($properties, true);
}
}