1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\DevToolkit\Helpers; |
4
|
|
|
|
5
|
|
|
use ReflectionObject; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
|
8
|
|
|
class DevUtils |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param object $obj |
12
|
|
|
* @param string $prop |
13
|
|
|
* @param mixed $val |
14
|
|
|
* @return void |
15
|
|
|
*/ |
16
|
|
|
public static function updateProp(object $obj, string $prop, $val): void |
17
|
|
|
{ |
18
|
|
|
$refObject = new ReflectionObject($obj); |
19
|
|
|
$refProperty = $refObject->getProperty($prop); |
20
|
|
|
$refProperty->setAccessible(true); |
21
|
|
|
$refProperty->setValue($obj, $val); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param object $obj |
26
|
|
|
* @param string $prop |
27
|
|
|
* @param callable $cb |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public static function updatePropCb(object $obj, string $prop, callable $cb): void |
31
|
|
|
{ |
32
|
|
|
$refObject = new ReflectionObject($obj); |
33
|
|
|
$refProperty = $refObject->getProperty($prop); |
34
|
|
|
$refProperty->setAccessible(true); |
35
|
|
|
$refProperty->setValue($obj, $cb($refProperty->getValue($obj))); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param object $obj |
40
|
|
|
* @param string $prop |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
public static function getProp(object $obj, string $prop) |
44
|
|
|
{ |
45
|
|
|
$refObject = new ReflectionObject($obj); |
46
|
|
|
$refProperty = $refObject->getProperty($prop); |
47
|
|
|
$refProperty->setAccessible(true); |
48
|
|
|
return $refProperty->getValue($obj); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $class |
53
|
|
|
* @param string $prop |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
|
|
public static function getStaticProp(string $class, string $prop) |
57
|
|
|
{ |
58
|
|
|
$refClass = new ReflectionClass($class); |
59
|
|
|
return $refClass->getStaticPropertyValue($prop); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $class |
64
|
|
|
* @param string $prop |
65
|
|
|
* @param mixed $val |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
|
|
public static function updateStaticProp(string $class, string $prop, $val) |
69
|
|
|
{ |
70
|
|
|
$refClass = new ReflectionClass($class); |
71
|
|
|
$refClass->setStaticPropertyValue($prop, $val); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|