1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace W2w\Lib\ApieObjectAccessNormalizer\ObjectAccess; |
5
|
|
|
|
6
|
|
|
use ReflectionClass; |
7
|
|
|
|
8
|
|
|
class GroupedObjectAccess implements ObjectAccessSupportedInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var ObjectAccessInterface |
12
|
|
|
*/ |
13
|
|
|
private $fallback; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var ObjectAccessInterface[] |
17
|
|
|
*/ |
18
|
|
|
private $specificObjectAccess; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param ObjectAccessInterface $fallback |
22
|
|
|
* @param ObjectAccessInterface[] $specificObjectAccess |
23
|
|
|
*/ |
24
|
|
|
public function __construct(ObjectAccessInterface $fallback, array $specificObjectAccess) |
25
|
|
|
{ |
26
|
|
|
$this->fallback = $fallback; |
27
|
|
|
$this->specificObjectAccess = $specificObjectAccess; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Returns the correct ObjectAccessInterface instance. |
32
|
|
|
* |
33
|
|
|
* @param ReflectionClass $reflectionClass |
34
|
|
|
* @return ObjectAccessInterface |
35
|
|
|
*/ |
36
|
|
|
private function findObjectAccessForClass(ReflectionClass $reflectionClass): ObjectAccessInterface |
37
|
|
|
{ |
38
|
|
|
if (isset($this->specificObjectAccess[$reflectionClass->name])) { |
39
|
|
|
return $this->specificObjectAccess[$reflectionClass->name]; |
40
|
|
|
} |
41
|
|
|
foreach ($this->specificObjectAccess as $key => $objectAccess) { |
42
|
|
|
if ($objectAccess instanceof ObjectAccessSupportedInterface && $objectAccess->isSupported($reflectionClass)) { |
43
|
|
|
return $objectAccess; |
44
|
|
|
} |
45
|
|
|
if (is_a($reflectionClass->name, $key, true)) { |
46
|
|
|
return $objectAccess; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
return $this->fallback; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
*/ |
55
|
|
|
public function getGetterFields(ReflectionClass $reflectionClass): array |
56
|
|
|
{ |
57
|
|
|
return $this->findObjectAccessForClass($reflectionClass)->getGetterFields($reflectionClass); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritDoc} |
62
|
|
|
*/ |
63
|
|
|
public function getSetterFields(ReflectionClass $reflectionClass): array |
64
|
|
|
{ |
65
|
|
|
return $this->findObjectAccessForClass($reflectionClass)->getSetterFields($reflectionClass); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritDoc} |
70
|
|
|
*/ |
71
|
|
|
public function getGetterTypes(ReflectionClass $reflectionClass, string $fieldName): array |
72
|
|
|
{ |
73
|
|
|
return $this->findObjectAccessForClass($reflectionClass)->getGetterTypes($reflectionClass, $fieldName); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritDoc} |
78
|
|
|
*/ |
79
|
|
|
public function getSetterTypes(ReflectionClass $reflectionClass, string $fieldName): array |
80
|
|
|
{ |
81
|
|
|
return $this->findObjectAccessForClass($reflectionClass)->getSetterTypes($reflectionClass, $fieldName); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritDoc} |
86
|
|
|
*/ |
87
|
|
|
public function getConstructorArguments(ReflectionClass $reflectionClass): array |
88
|
|
|
{ |
89
|
|
|
return $this->findObjectAccessForClass($reflectionClass)->getConstructorArguments($reflectionClass); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
|
|
public function getValue(object $instance, string $fieldName) |
96
|
|
|
{ |
97
|
|
|
return $this->findObjectAccessForClass(new ReflectionClass($instance))->getValue($instance, $fieldName); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritDoc} |
102
|
|
|
*/ |
103
|
|
|
public function setValue(object $instance, string $fieldName, $value) |
104
|
|
|
{ |
105
|
|
|
return $this->findObjectAccessForClass(new ReflectionClass($instance))->setValue($instance, $fieldName, $value); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritDoc} |
110
|
|
|
*/ |
111
|
|
|
public function instantiate(ReflectionClass $reflectionClass, array $constructorArgs): object |
112
|
|
|
{ |
113
|
|
|
return $this->findObjectAccessForClass($reflectionClass)->instantiate($reflectionClass, $constructorArgs); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritDoc} |
118
|
|
|
*/ |
119
|
|
|
public function getDescription(ReflectionClass $reflectionClass, string $fieldName, bool $preferGetters): ?string |
120
|
|
|
{ |
121
|
|
|
return $this->findObjectAccessForClass($reflectionClass)->getDescription($reflectionClass, $fieldName, $preferGetters); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritDoc} |
126
|
|
|
*/ |
127
|
|
|
public function isSupported(ReflectionClass $reflectionClass): bool |
128
|
|
|
{ |
129
|
|
|
$instance = $this->findObjectAccessForClass($reflectionClass); |
130
|
|
|
if ($instance instanceof ObjectAccessSupportedInterface) { |
131
|
|
|
return $instance->isSupported($reflectionClass); |
132
|
|
|
} |
133
|
|
|
return true; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|