|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yep\Reflection; |
|
3
|
|
|
|
|
4
|
|
|
class ReflectionClass extends \ReflectionClass { |
|
5
|
|
|
/** @var object */ |
|
6
|
|
|
protected $object; |
|
7
|
|
|
|
|
8
|
7 |
|
public function __construct($class, $object = null) { |
|
9
|
7 |
|
if (is_object($class) && !is_object($object)) { |
|
10
|
2 |
|
$object = $class; |
|
11
|
|
|
} |
|
12
|
|
|
|
|
13
|
5 |
|
if (!is_object($object)) { |
|
14
|
5 |
|
throw new \InvalidArgumentException(sprintf('Expected "object", got "%s".', gettype($object))); |
|
15
|
5 |
|
} |
|
16
|
|
|
|
|
17
|
5 |
|
parent::__construct($class); |
|
18
|
5 |
|
$this->object = $object; |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
1 |
|
public function getObject() { |
|
22
|
1 |
|
return $this->object; |
|
23
|
1 |
|
} |
|
24
|
|
|
|
|
25
|
1 |
|
public static function from($class, $object = null) { |
|
26
|
|
|
return new static($class, $object); |
|
27
|
|
|
} |
|
28
|
1 |
|
|
|
29
|
1 |
|
public function invokeMethod($method, array $arguments = []) { |
|
30
|
1 |
|
$reflection = $this->getMethod($method); |
|
31
|
1 |
|
$reflection->setAccessible(true); |
|
32
|
1 |
|
|
|
33
|
|
|
return $reflection->invokeArgs($this->object, $arguments); |
|
34
|
1 |
|
} |
|
35
|
1 |
|
|
|
36
|
1 |
|
public function setPropertyValue($property, $value) { |
|
37
|
|
|
$reflection = $this->getProperty($property); |
|
38
|
1 |
|
$reflection->setAccessible(true); |
|
39
|
|
|
$reflection->setValue($this->object, $value); |
|
40
|
|
|
|
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getPropertyValue($property) { |
|
45
|
|
|
$reflection = $this->getProperty($property); |
|
46
|
|
|
$reflection->setAccessible(true); |
|
47
|
|
|
|
|
48
|
|
|
return $reflection->getValue($this->object); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getParent() { |
|
52
|
|
|
return self::from($this->getParentClass()->getName(), $this->object); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|