|
1
|
|
|
<?php |
|
2
|
|
|
namespace MetaHydrator\Utils; |
|
3
|
|
|
|
|
4
|
|
|
use ReflectionClass; |
|
5
|
|
|
use ReflectionException; |
|
6
|
|
|
use ReflectionMethod; |
|
7
|
|
|
|
|
8
|
|
|
class ReflectionUtils |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @param $object |
|
12
|
|
|
* @param string $field |
|
13
|
|
|
* @return mixed |
|
14
|
|
|
* |
|
15
|
|
|
* @throws ReflectionException |
|
16
|
|
|
*/ |
|
17
|
|
|
static public function getFieldValue($object, $field) |
|
|
|
|
|
|
18
|
|
|
{ |
|
19
|
|
|
$reflectionClass = new ReflectionClass($object); |
|
20
|
|
|
|
|
21
|
|
View Code Duplication |
foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $reflectionMethod) { |
|
|
|
|
|
|
22
|
|
|
if (!$reflectionMethod->isStatic() |
|
23
|
|
|
&& $reflectionMethod->getNumberOfRequiredParameters() == 0 |
|
24
|
|
|
&& strcasecmp($reflectionMethod->getName(), "get$field") == 0) { |
|
|
|
|
|
|
25
|
|
|
return $reflectionMethod->invoke($object); |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
View Code Duplication |
if ($reflectionClass->hasProperty($field)) { |
|
|
|
|
|
|
29
|
|
|
$property = $reflectionClass->getProperty($field); |
|
30
|
|
|
if (!$property->isStatic()) { |
|
31
|
|
|
if (!$property->isPublic()) { |
|
32
|
|
|
$property->setAccessible(true); |
|
33
|
|
|
} |
|
34
|
|
|
return $property->getValue($object); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
if (is_array($object) || $object instanceof \ArrayAccess) { |
|
38
|
|
|
return $object[$field]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
throw new ReflectionException("could not extract field '$field' from object"); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param $object |
|
46
|
|
|
* @param string $field |
|
47
|
|
|
* @param mixed $value |
|
48
|
|
|
* |
|
49
|
|
|
* @throws ReflectionException |
|
50
|
|
|
*/ |
|
51
|
|
|
static public function setFieldValue(&$object, $field, $value) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
$reflectionClass = new ReflectionClass($object); |
|
54
|
|
|
|
|
55
|
|
View Code Duplication |
foreach ($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC) as $reflectionMethod) { |
|
|
|
|
|
|
56
|
|
|
if (!$reflectionMethod->isStatic() |
|
57
|
|
|
&& $reflectionMethod->getNumberOfRequiredParameters() == 1 |
|
58
|
|
|
&& strcasecmp($reflectionMethod->getName(), "set$field") == 0) { |
|
|
|
|
|
|
59
|
|
|
$reflectionMethod->invoke($object, $value); |
|
60
|
|
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
View Code Duplication |
if ($reflectionClass->hasProperty($field)) { |
|
|
|
|
|
|
64
|
|
|
$property = $reflectionClass->getProperty($field); |
|
65
|
|
|
if (!$property->isStatic()) { |
|
66
|
|
|
if (!$property->isPublic()) { |
|
67
|
|
|
$property->setAccessible(true); |
|
68
|
|
|
} |
|
69
|
|
|
$property->setValue($object, $value); |
|
70
|
|
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
if (is_array($object) || $object instanceof \ArrayAccess) { |
|
74
|
|
|
$object[$field] = $value; |
|
75
|
|
|
return; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
throw new ReflectionException("could not set field '$field' from object"); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|