-
Notifications
You must be signed in to change notification settings - Fork 0
/
Object.php
608 lines (543 loc) · 17.3 KB
/
Object.php
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
<?php
/**
* ELSWAK Object
*
* @author Errol Sayre
*/
//!Related Exceptions
/**
* Generic exception for ELSWAK Objects.
*
* @package ELSWAK
*/
class ELSWAK_Object_Exception extends ELSWAK_Exception {}
/**
* Exception for attempting to access a non-existent ELSWAK Object property.
*
* @package ELSWAK
*/
class ELSWAK_Object_NonexistentProperty_Exception extends ELSWAK_Object_Exception {}
/**
* Exception for attempting to access an ELSWAK Object property protected by non-public accessors.
*
* @package ELSWAK
*/
class ELSWAK_Object_ProtectedProperty_Exception extends ELSWAK_Object_Exception {}
/**
* Exception for attempting to access a non-public method of an ELSWAK Object.
*
* @package ELSWAK
*/
class ELSWAK_Object_ProtectedMethod_Exception extends ELSWAK_Object_Exception {}
//!Functionality Stubs
// Place a stub for backward compatibility with PHP < 5.4.0
if (!interface_exists('JsonSerializable')) {
require_once 'JsonSerializeableInterface.php';
}
//!Class Definition
/**
* Basic building block for objects with automatic accessors.
*
* Provides a replacement for ELSWAK Settable as a more basic building
* block for building more structured classes with default getters and
* setters.
*
* @package ELSWAK
*/
abstract class ELSWAK_Object
implements JsonSerializable, ArrayAccess, ELSWAK_Gettable {
//!Class constants
//!— Comparisons
/**
* Left and right equal
* @type integer
*/
const COMPARISON_EQUAL = 0;
/**
* Left operand smaller/lesser/earlier
*
* I define these seemingly duplicate constants to allow the developer
* to pick values that make the most sense to him while fitting the
* algorithm of the comparison.
*
* @type integer
*/
const COMPARISON_LEFT_LESSER = -1;
/**
* Left operand larger/greater/later
* @type integer
*/
const COMPARISON_LEFT_GREATER = 1;
/**
* Right operand smaller/lesser/earlier
* @type integer
*/
const COMPARISON_RIGHT_LESSER = 1;
/**
* Right operand larger/greater/later
* @type integer
*/
const COMPARISON_RIGHT_GREATER = -1;
//!Static properties
/**
* Memoized getter collection
*
* Collect all of the discovered and automagic setter methods segregated by class such that the result is only stored (and determined) once per class and is accessible to all instances.
*/
private static $_getters = array();
/**
* Memoized setter collection
*
* Collect all of the discovered and automagic setter methods segregated by class such that the result is only stored (and determined) once per class and is accessible to all instances.
*/
private static $_setters = array();
/**
* Memoized caller collection
*
* Collect all of the discovered and automagic methods segregated by class such that the result is only stored (and determined) once per class and is accessible to all instances.
*/
private static $_callers = array();
/**
* Memoized method collection
*
* Collect all of the discovered methods segregated by class such that the result is only stored (and determined) once per class and is accessible to all instances.
*/
private static $_methods = array();
//!Instance methods
/**
* Default the constructor to include property import.
*
* All objects can natively support importing values from an array or iterable object by key/property. This generic method in-turn means constructors are purely optional to implement.
*
* @param array|object $import Array or iterable object.
*/
public function __construct($import = null) {
if ($import) {
$this->_import($import);
}
}
/**
* Import values from any iterable collection.
*
* @param array|object $import Array or iterable object.
*
* @return ELSWAK_Object self
*/
public function _import($import) {
if (is_array($import) || is_object($import)) {
foreach ($import as $property => $value) {
try { $this->__set($property, $value); } catch (Exception $e) {}
}
}
return $this;
}
/**
* Export properties of this object as an array.
*
* @return array
*/
public function _export() {
$export = array();
$keys = array_keys(get_object_vars($this));
foreach ($keys as $property) {
if ($this->{$property} instanceof ELSWAK_Object) {
$export[$property] = $this->{$property}->_export();
}
else {
// avoid the use of lazy-inits, which can cause infinite recursion
$initial = $this->{$property};
try {
$value = $this->__get($property);
// determine if the value is an object and was initially null
if (is_object($value) && !$initial) {
// this is most likely a lazy-init, omit it from the export and restore the initial value
$this->{$property} = $initial;
}
else {
// include the value
$export[$property] = $value;
}
}
catch (Exception $e) {
// the property should not be included in the export
}
}
}
return $export;
}
//!JSONSerializable methods
/**
* Provide the JSON encoder with an easy to handle array.
*
* @return array
*/
public function jsonSerialize() {
return $this->_export();
}
public function toJSON() {
return json_encode( $this->jsonSerialize() );
}
/**
* Default to JSON representation like Objective-C's describe method.
*
* @return string
*/
public function __toString() {
return json_encode($this);
}
//!Object Comparison methods
/**
* Compare two objects
*
* Since this is a generic method that is intended to be overriden by
* subclasses, for now we'll rely upon PHP’s built-in comparison.
*
* @param mixed $that
* @return integer
*/
public function compare($that) {
if ($this == $that) {
return self::COMPARISON_EQUAL;
} elseif ($this < $that) {
return self::COMPARISON_LEFT_LESSER;
}
return self::COMPARISON_LEFT_GREATER;
}
/**
* Short-cut for comparison
*
* Each of these short-cut methods is designed to operate within the
* context of the results of the compare method. This means that
* subclasses can override one method and still have these three
* shortcuts as long as they maintain the same convention i.e:
* - this less than that yields -1
* - this equal to that yields 0
* - this greater than that yields 1
*
* @param mixed $that
* @return boolean
*/
public function isEqualTo($that) {
if ($this->compare($that) == self::COMPARISON_EQUAL) {
return true;
}
return false;
}
/**
* Short-cut for comparison
*
* @param mixed $that
* @return boolean
*/
public function isLessThan($that) {
if ($this->compare($that) == self::COMPARISON_LEFT_LESSER) {
return true;
}
return false;
}
/**
* Short-cut for comparison
*
* @param mixed $that
* @return boolean
*/
public function isGreaterThan($that) {
if ($this->compare($that) == self::COMPARISON_LEFT_GREATER) {
return true;
}
return false;
}
//!ArrayAccess methods
/**
* Set a property via array notation.
* @return mixed|ELSWAK_Object self
*/
public function offsetSet($offset, $value) {
return $this->__set($offset, $value);
}
/**
* Determine if the offset is "gettable" as a real or virtual property.
* @return boolean
*/
public function offsetExists($offset) {
try {
$this->__get($offset);
return true;
} catch (ELSWAK_Object_Exception $e) {}
return false;
}
/**
* Restore the property to a null state.
*
* This will only work if the property is able to be set to null, which may or may not be allowed based upon the extending class definition.
* @return mixed|ELSWAK_Object self
*/
public function offsetUnset($offset) {
try {
return $this->__set($offset, null);
} catch (ELSWAK_Object_Exception $e) {}
return $this;
}
/**
* Get a property via array notation.
* @return mixed
*/
public function offsetGet($offset) {
try {
return $this->__get($offset);
} catch (ELSWAK_Object_Exception $e) {}
return null;
}
//!Gettable — Instance methods
/**
* Provide a gentle getter
*
* Allow the caller to ask for a real or virtual property without
* complaining.
*
* @param string $property
* @return mixed|null
*/
public function get($property) {
try {
return $this->__get($property);
} catch (ELSWAK_Object_Exception $e) {}
return null;
}
//!Magic Method Default Getter/Setter methods
/**
* Utilize the __set "magic" method to provide all properties a default setter.
*
* @param string $property The property to set.
* @param mixed $value The value to set the given property.
* @return ELSWAK_Object self
*/
public function __set($property, $value) {
// determine if this class has been examined before
$className = get_class($this);
if (!isset(self::$_setters[$className])) {
self::$_setters[$className] = array();
}
$method = 'set'.ucfirst($property);
// determine if this property has been examined before
if (!isset(self::$_setters[$className][$property])) {
// determine if this property can be set or not
if (ELSWAK_Object_Helper::methodExistsForClass($method, $this)) {
// the property has a public setter method, set the value using the method
self::$_setters[$className][$property] = 2;
$this->_registerMethod($method);
} else if (method_exists($this, $method)) {
// the property has a protected setter method, protect the property
self::$_setters[$className][$property] = -1;
$this->_registerMethod($method);
} else if (property_exists($this, $property)) {
// the property has no setter method, set the value directly
self::$_setters[$className][$property] = 1;
} else {
// the property is not defined in the class, protect the class definition
self::$_setters[$className][$property] = -2;
}
}
// perform the determined operation
if (self::$_setters[$className][$property] == 1) {
$this->{$property} = $value;
} else if (self::$_setters[$className][$property] == 2) {
$this->{$method}($value);
} else if (self::$_setters[$className][$property] == -1) {
throw new ELSWAK_Object_ProtectedProperty_Exception('Unable to set property "'.$property.'". Property is protected and has no publicly accessible setter method.');
} else {
throw new ELSWAK_Object_NonexistentProperty_Exception('Unable to set property "'.$property.'". Property is not defined within the class "'.$className.'".');
}
return $this;
}
/**
* Return the appropriate value from a matching property.
*
* @param string $property The property to get.
*
* @return mixed
*/
public function __get($property) {
// determine if this class has been examined before
$className = get_class($this);
if (!isset(self::$_getters[$className])) {
self::$_getters[$className] = array();
}
// determine if this property has been examined before
if (!isset(self::$_getters[$className][$property])) {
// determine if this property can be accessed
// search for getter methods that include the "get" prefix or not.
$method = 'get'.$property;
if (ELSWAK_Object_Helper::methodExistsForClass($method, $this)) {
// the property has a public getter method, return the value using the method
self::$_getters[$className][$property] = 2;
$this->_registerMethod($method);
} else if (method_exists($this, $method)) {
// the property has a protected getter method, protect the property
self::$_getters[$className][$property] = -1;
$this->_registerMethod($method);
} else if (ELSWAK_Object_Helper::methodExistsForClass($property, $this)) {
// the property has a public getter method named as the property, return the value using the method
self::$_getters[$className][$property] = 3;
$this->_registerMethod($method);
} else if (method_exists($this, $property)) {
// the property has a protected getter method named as the property, protect the property
self::$_getters[$className][$property] = -1;
$this->_registerMethod($method);
} else if (property_exists($this, $property)) {
// the property is has no getter method, return the value directly
self::$_getters[$className][$property] = 1;
} else {
// the property is not defined in the class, protect the class definition
self::$_getters[$className][$property] = -2;
}
}
// perform the determined operation
if (self::$_getters[$className][$property] == 1) {
return $this->{$property};
} else if (self::$_getters[$className][$property] == 2) {
return $this->{'get'.$property}();
} else if (self::$_getters[$className][$property] == 3) {
return $this->{$property}();
} else if (self::$_getters[$className][$property] == -1) {
throw new ELSWAK_Object_ProtectedProperty_Exception('Unable to get property "'.$property.'". Property is protected and has no publically accessible getter method.');
} else {
throw new ELSWAK_Object_NonexistentProperty_Exception('Unable to get property "'.$property.'". Property is not defined within the class "'.$className.'".');
}
return $this;
}
/**
* Call the matching method or pseudo-method.
*
* @param string $method The method to call.
* @param mixed|array $arguments The arguments to pass to the called method.
*
* @return mixed|void
*/
public function __call($method, $arguments) {
// determine if this class has been examined before
$className = get_class($this);
if (!isset(self::$_callers[$className])) {
self::$_callers[$className] = array();
}
// determine if this method has been examined before
if (!array_key_exists($method, self::$_callers[$className]) || !is_array(self::$_callers[$className][$method])) {
// set the default
self::$_callers[$className][$method] = array(
'type' => 0,
'property' => null
);
// determine if this method is a protected internal method or a property
if (method_exists($this, $method)) {
// the method exists but is inaccessible (otherwise the __call method would not have been called)
// protect the method
self::$_callers[$className][$method]['type'] = -1;
$this->_registerMethod($method);
} else if ((stripos($method, 'set') === 0)) {
self::$_callers[$className][$method]['type'] = 1;
self::$_callers[$className][$method]['property'] = strtolower(substr($method, 3, 1)).substr($method, 4);
} else if ((stripos($method, 'get') === 0)) {
self::$_callers[$className][$method]['type'] = 1;
self::$_callers[$className][$method]['property'] = strtolower(substr($method, 3, 1)).substr($method, 4);
} else {
self::$_callers[$className][$method]['type'] = 1;
self::$_callers[$className][$method]['property'] = $method;
}
}
// perform the determined operation
if (self::$_callers[$className][$method]['type'] == -1) {
throw new ELSWAK_Object_ProtectedMethod_Exception('Unable to call method "'.$method.'". Method is protected.');
} else if (
(self::$_callers[$className][$method]['type'] == 1) &&
(count($arguments) == 1)
) {
// attempt to set the property with the provided argument, allowing the __set method to throw any appropriate exceptions
return $this->__set(self::$_callers[$className][$method]['property'], $arguments[0]);
}
// since no matching method exists and there is not an appropriate number of arguments for a set operation, attempt to get the property, allowing the __get method to throw any appropriate exceptions
return $this->__get(self::$_callers[$className][$method]['property']);
}
//!Magic Method assistance methods
/**
* Register a located method.
*
* When a method is determined to exist, add it to the listing to avoid performing another lookup.
*
* @param string $method The method to register/memoize.
*
* @return ELSWAK_Object self
*/
protected function _registerMethod($method) {
$className = get_class($this);
if (!array_key_exists($className, self::$_methods)) {
self::$_methods[$className] = array();
}
self::$_methods[$className][strtolower($method)] = true;
return $this;
}
/**
* Determine if a method exists and wether it is publicly accessible.
*
* @param string $method The method to check.
*
* @return boolean
*/
public function _methodExists($method) {
$className = get_class($this);
$compareMethod = strtolower($method);
// ensure the appropriate slot is available
if (!array_key_exists($className, self::$_methods)) {
self::$_methods[$className] = array();
}
// check if this method has been seen before
if (array_key_exists($compareMethod, self::$_methods[$className])) {
if (self::$_methods[$className][$compareMethod] === true) {
return true;
}
} elseif (method_exists($this, $method)) {
$this->_registerMethod($method);
return true;
}
return false;
}
}
//!Related classes
/**
* Provide an external vantage point for determining the visibility of methods.
*
* @package ELSWAK
*/
class ELSWAK_Object_Helper {
/**
* Get the methods from the other class visible to this class.
*
* @param string|object $class The class to examine.
*
* return array
*/
public static function methodsForClass($class) {
return get_class_methods($class);
}
/**
* Determine if a method exists for a given class.
*
* @param string $method The method name to check for.
* @param string|object $class The class to check within.
*
* return boolean
*/
public static function methodExistsForClass($method, $class) {
// ensure we're inspecting an object rather than getting the output of __toString()
if (is_object($class)) {
$class = get_class($class);
}
$method = strtolower($method);
$methods = self::methodsForClass($class);
foreach ($methods as $name) {
if (strtolower($name) == $method) {
return true;
}
}
return false;
}
}