1 | <?php |
||
45 | class ElevationMap |
||
46 | { |
||
47 | /** |
||
48 | * Source object |
||
49 | * |
||
50 | * @var object |
||
51 | */ |
||
52 | protected $object; |
||
53 | /** |
||
54 | * Elevation property map |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $map = []; |
||
59 | |||
60 | /** |
||
61 | * Elevation map constructor |
||
62 | * |
||
63 | * @param object $object Source object |
||
64 | */ |
||
65 | 8 | public function __construct($object) |
|
66 | { |
||
67 | 8 | $this->object = $object; |
|
68 | 8 | $class = new \ReflectionClass($this->object); |
|
69 | 8 | if (!$class->isInternal()) { |
|
70 | 6 | $this->reflectProperties($class); |
|
71 | } |
||
72 | 8 | } |
|
73 | |||
74 | /** |
||
75 | * Recursively reflect the object properties for a particular class |
||
76 | * |
||
77 | * @param \ReflectionClass $class Class reflection |
||
78 | */ |
||
79 | 6 | protected function reflectProperties(\ReflectionClass $class) |
|
80 | { |
||
81 | 6 | $classMap = []; |
|
82 | |||
83 | // Run through all reflection properties |
||
84 | 6 | foreach ($class->getProperties() as $property) { |
|
85 | // If the property is defined by the current class |
||
86 | 6 | if ($property->getDeclaringClass()->name === $class->name) { |
|
87 | 6 | $property->setAccessible(true); |
|
88 | 6 | $classMap[$property->name] = $property->getValue($this->object); |
|
89 | 6 | $property->setAccessible(false); |
|
90 | } |
||
91 | } |
||
92 | |||
93 | 6 | $this->map[$class->name] = $classMap; |
|
94 | |||
95 | // Recurse if necessary |
||
96 | 6 | $parentClass = $class->getParentClass(); |
|
97 | 6 | if (($parentClass instanceof \ReflectionClass) && !$parentClass->isInternal()) { |
|
98 | 5 | $this->reflectProperties($parentClass); |
|
99 | } |
||
100 | 6 | } |
|
101 | |||
102 | /** |
||
103 | * Return the elevation map |
||
104 | * |
||
105 | * @return array Elevation map |
||
106 | */ |
||
107 | 3 | public function getMap() |
|
111 | |||
112 | /** |
||
113 | * Return the property values for a particular class |
||
114 | * |
||
115 | * @param \ReflectionClass $class Class reflection |
||
116 | * @return array Property values |
||
117 | */ |
||
118 | 5 | public function getPropertyValues(\ReflectionClass $class) |
|
123 | } |
||
124 |