-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathciobject_helper.php
59 lines (54 loc) · 1.87 KB
/
ciobject_helper.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
<?php
/**
* Coppyright (C) 2016 Marcos Zuriaga Miguel
* all rights reserved
*/
namespace AQ {
/**
* Interfacing methods to autofill the class public vars from any class
* or array of stdClass (or any other valid class) instances
*
* @author wolfi
*/
class CIObject {
/**
* Returns an array with instances of this object with the properties set
* from the properties on the array objects
* @param \stdClass[] $array
* @return self[]
*/
static function fromSTDArray($array){
$ret = [];
foreach ($array as $value) {
$ret[] = static::fromObject($value);
}
return $ret;
}
/**
* Creates an instance of the class, setting all it's public properties
* to the ones set on the object (json_decode or codeigniter result)
* @param \stdClass $object
* @return \self A new instance of the object
* @throws Exception In case an unexpected type is found
*/
static function fromObject($object){
if (!is_object($object)) throw new \Exception ("Expected an object, got: " . gettype ($object));
$self = static::class;
$reflect = new \ReflectionClass($self);
$props = $reflect->getProperties(\ReflectionProperty::IS_PUBLIC);
$ret = new $self();
foreach ($props as $prop) {
if (isset($object->{$prop->name})){
$ret->{$prop->name} = $object->{$prop->name};
}
}
$ret->_post_init_process();
return $ret;
}
/**
* Executed after creating the object using the fromSTDObject method
*/
protected function _post_init_process(){
}
}
}