1 | <?php |
||
45 | class Elevator |
||
46 | { |
||
47 | /** |
||
48 | * Source object |
||
49 | * |
||
50 | * @var object |
||
51 | */ |
||
52 | protected $object; |
||
53 | /** |
||
54 | * Source object class reflection |
||
55 | * |
||
56 | * @var \ReflectionClass |
||
57 | */ |
||
58 | protected $class; |
||
59 | |||
60 | /** |
||
61 | * Constructor |
||
62 | * |
||
63 | * @param object $object Source object |
||
64 | * @throws UnexpectedValueException If the argument is not an object |
||
65 | */ |
||
66 | 7 | public function __construct($object) |
|
87 | |||
88 | /** |
||
89 | * Elevate the current object to a particular class |
||
90 | * |
||
91 | * @param \ReflectionClass $class Target class |
||
92 | * @param ElevationMap $map |
||
93 | * @return object Elevated target object |
||
94 | */ |
||
95 | 5 | public function elevate(\ReflectionClass $class, ElevationMap $map) |
|
96 | { |
||
97 | // If the target class doesn't extend the source object class |
||
98 | 5 | if (!$class->isSubclassOf($this->class->name)) { |
|
99 | 1 | throw new UnexpectedValueException( |
|
100 | sprintf( |
||
101 | 1 | UnexpectedValueException::INVALID_TARGET_CLASS_STR, |
|
102 | 1 | $class->name, |
|
103 | 1 | $this->class->name |
|
104 | ), |
||
105 | 1 | UnexpectedValueException::INVALID_TARGET_CLASS |
|
106 | ); |
||
107 | } |
||
108 | |||
109 | 4 | return $this->mergeMap($class->newInstanceWithoutConstructor(), $class, $map); |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * Recursively merge an elevation map into an elevated target object |
||
114 | * |
||
115 | * @param object $object Target object |
||
116 | * @param \ReflectionClass $class Current class |
||
117 | * @param ElevationMap $map Elevation map |
||
118 | * @return object Target object |
||
119 | */ |
||
120 | 4 | protected function mergeMap($object, \ReflectionClass $class, ElevationMap $map) |
|
137 | } |
||
138 |