1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace W2w\Lib\ApieObjectAccessNormalizer\ObjectAccess; |
5
|
|
|
|
6
|
|
|
use ReflectionClass; |
7
|
|
|
use ReflectionMethod; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* ObjectAccess instance that only works on objects that implement SelfObjectAccessInterface |
11
|
|
|
* |
12
|
|
|
* @see SelfObjectAccessInterface |
13
|
|
|
*/ |
14
|
|
|
class SelfObjectAccess implements ObjectAccessSupportedInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* {@inheritDoc} |
18
|
|
|
*/ |
19
|
|
|
public function getGetterFields(ReflectionClass $reflectionClass): array |
20
|
|
|
{ |
21
|
|
|
$method = new ReflectionMethod($reflectionClass->getName(), __FUNCTION__); |
22
|
|
|
return $method->invoke(null); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritDoc} |
27
|
|
|
*/ |
28
|
|
|
public function getSetterFields(ReflectionClass $reflectionClass): array |
29
|
|
|
{ |
30
|
|
|
$method = new ReflectionMethod($reflectionClass->getName(), __FUNCTION__); |
31
|
|
|
return $method->invoke(null); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritDoc} |
36
|
|
|
*/ |
37
|
|
|
public function getGetterTypes(ReflectionClass $reflectionClass, string $fieldName): array |
38
|
|
|
{ |
39
|
|
|
$method = new ReflectionMethod($reflectionClass->getName(), __FUNCTION__); |
40
|
|
|
return $method->invoke(null, $fieldName); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritDoc} |
45
|
|
|
*/ |
46
|
|
|
public function getSetterTypes(ReflectionClass $reflectionClass, string $fieldName): array |
47
|
|
|
{ |
48
|
|
|
$method = new ReflectionMethod($reflectionClass->getName(), __FUNCTION__); |
49
|
|
|
return $method->invoke(null, $fieldName); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
*/ |
55
|
|
|
public function getConstructorArguments(ReflectionClass $reflectionClass): array |
56
|
|
|
{ |
57
|
|
|
$method = new ReflectionMethod($reflectionClass->getName(), __FUNCTION__); |
58
|
|
|
return $method->invoke(null); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
*/ |
64
|
|
|
public function getMethodArguments(ReflectionMethod $method, ?ReflectionClass $reflectionClass = null): array |
65
|
|
|
{ |
66
|
|
|
return (new ObjectAccess())->getMethodArguments($method, $reflectionClass); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritDoc} |
71
|
|
|
*/ |
72
|
|
|
public function getValue(object $instance, string $fieldName) |
73
|
|
|
{ |
74
|
|
|
$method = new ReflectionMethod($instance, 'getFieldNameValue'); |
75
|
|
|
return $method->invoke($instance, $fieldName); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
|
|
public function setValue(object $instance, string $fieldName, $value) |
82
|
|
|
{ |
83
|
|
|
$method = new ReflectionMethod($instance, 'setFieldNameValue'); |
84
|
|
|
return $method->invoke($instance, $fieldName, $value); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritDoc} |
89
|
|
|
*/ |
90
|
|
|
public function instantiate(ReflectionClass $reflectionClass, array $constructorArgs): object |
91
|
|
|
{ |
92
|
|
|
$method = new ReflectionMethod($reflectionClass->getName(), __FUNCTION__); |
93
|
|
|
return $method->invoke(null, $constructorArgs); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritDoc} |
98
|
|
|
*/ |
99
|
|
|
public function getDescription(ReflectionClass $reflectionClass, string $fieldName, bool $preferGetters): ?string |
100
|
|
|
{ |
101
|
|
|
$method = new ReflectionMethod($reflectionClass->getName(), __FUNCTION__); |
102
|
|
|
return $method->invoke(null, $fieldName, $preferGetters); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns true if class is supported for this ObjectAccess instance. |
107
|
|
|
* |
108
|
|
|
* @param ReflectionClass $reflectionClass |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
public function isSupported(ReflectionClass $reflectionClass): bool |
112
|
|
|
{ |
113
|
|
|
return $reflectionClass->implementsInterface(SelfObjectAccessInterface::class); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|