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