ElevationMap::getPropertyValues()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
/**
4
 * elevator
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Rdfalite
8
 * @subpackage Jkphl\Elevator\Domain
9
 * @author Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Jkphl\Elevator\Domain;
38
39
/**
40
 * Elevation property map
41
 *
42
 * @package Jkphl\Elevator
43
 * @subpackage Jkphl\Elevator\Domain
44
 */
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()
108
    {
109 3
        return $this->map;
110
    }
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)
119
    {
120 5
        $className = $class->name;
121 5
        return isset($this->map[$className]) ? $this->map[$className] : [];
122
    }
123
}
124