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