@@ -16,7 +16,7 @@ |
||
16 | 16 | */ |
17 | 17 | trait AccessorCamelTrait |
18 | 18 | { |
19 | - use AccessorTrait, FormatAsCamel { |
|
20 | - FormatAsCamel::accessor_format insteadof AccessorTrait; |
|
21 | - } |
|
19 | + use AccessorTrait, FormatAsCamel { |
|
20 | + FormatAsCamel::accessor_format insteadof AccessorTrait; |
|
21 | + } |
|
22 | 22 | } |
@@ -21,60 +21,60 @@ |
||
21 | 21 | */ |
22 | 22 | interface HasAccessor |
23 | 23 | { |
24 | - public const ACCESSOR_TYPE_GETTER = "get"; |
|
25 | - public const ACCESSOR_TYPE_SETTER = "set"; |
|
26 | - public const ACCESSOR_IS_LAZY = 'lazy'; |
|
27 | - public const ACCESSOR_IS_NOT_LAZY = ''; |
|
24 | + public const ACCESSOR_TYPE_GETTER = "get"; |
|
25 | + public const ACCESSOR_TYPE_SETTER = "set"; |
|
26 | + public const ACCESSOR_IS_LAZY = 'lazy'; |
|
27 | + public const ACCESSOR_IS_NOT_LAZY = ''; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Formats an accessor method name. |
|
31 | - * |
|
32 | - * @param string $type One of {@link ACCESSOR_TYPE_GETTER} and {@link ACCESSOR_TYPE_SETTER}. |
|
33 | - * @param string $lazy One of {@link ACCESSOR_IS_NOT_LAZY} and {@link ACCESSOR_IS_LAZY}. |
|
34 | - * Defaults to {@link ACCESSOR_IS_NOT_LAZY}. |
|
35 | - */ |
|
36 | - public static function accessor_format( |
|
37 | - string $property, |
|
38 | - string $type, |
|
39 | - string $lazy = self::ACCESSOR_IS_NOT_LAZY |
|
40 | - ): string; |
|
29 | + /** |
|
30 | + * Formats an accessor method name. |
|
31 | + * |
|
32 | + * @param string $type One of {@link ACCESSOR_TYPE_GETTER} and {@link ACCESSOR_TYPE_SETTER}. |
|
33 | + * @param string $lazy One of {@link ACCESSOR_IS_NOT_LAZY} and {@link ACCESSOR_IS_LAZY}. |
|
34 | + * Defaults to {@link ACCESSOR_IS_NOT_LAZY}. |
|
35 | + */ |
|
36 | + public static function accessor_format( |
|
37 | + string $property, |
|
38 | + string $type, |
|
39 | + string $lazy = self::ACCESSOR_IS_NOT_LAZY |
|
40 | + ): string; |
|
41 | 41 | |
42 | - /** |
|
43 | - * Returns the value of a property. |
|
44 | - * |
|
45 | - * @param string $property |
|
46 | - * |
|
47 | - * @return mixed |
|
48 | - * |
|
49 | - * @throws PropertyNotDefined when the property is not defined. |
|
50 | - * @throws PropertyNotReadable when the property is not accessible or is write-only |
|
51 | - * (the property is not defined and only a setter is available). |
|
52 | - * @throws ReflectionException |
|
53 | - */ |
|
54 | - public function __get($property); |
|
42 | + /** |
|
43 | + * Returns the value of a property. |
|
44 | + * |
|
45 | + * @param string $property |
|
46 | + * |
|
47 | + * @return mixed |
|
48 | + * |
|
49 | + * @throws PropertyNotDefined when the property is not defined. |
|
50 | + * @throws PropertyNotReadable when the property is not accessible or is write-only |
|
51 | + * (the property is not defined and only a setter is available). |
|
52 | + * @throws ReflectionException |
|
53 | + */ |
|
54 | + public function __get($property); |
|
55 | 55 | |
56 | - /** |
|
57 | - * Sets the value of a property. |
|
58 | - * |
|
59 | - * @param string $property |
|
60 | - * @param mixed $value |
|
61 | - * |
|
62 | - * @return void |
|
63 | - * |
|
64 | - * @throws PropertyNotWritable when the property doesn't exists, has no lazy |
|
65 | - * getter and is not public; or when only a getter is implemented. |
|
66 | - */ |
|
67 | - public function __set($property, $value); |
|
56 | + /** |
|
57 | + * Sets the value of a property. |
|
58 | + * |
|
59 | + * @param string $property |
|
60 | + * @param mixed $value |
|
61 | + * |
|
62 | + * @return void |
|
63 | + * |
|
64 | + * @throws PropertyNotWritable when the property doesn't exists, has no lazy |
|
65 | + * getter and is not public; or when only a getter is implemented. |
|
66 | + */ |
|
67 | + public function __set($property, $value); |
|
68 | 68 | |
69 | - /** |
|
70 | - * Whether an object has a property. |
|
71 | - * |
|
72 | - * The property can be defined by the class or handled by a getter or setter, or both. |
|
73 | - */ |
|
74 | - public function has_property(string $property): bool; |
|
69 | + /** |
|
70 | + * Whether an object has a property. |
|
71 | + * |
|
72 | + * The property can be defined by the class or handled by a getter or setter, or both. |
|
73 | + */ |
|
74 | + public function has_property(string $property): bool; |
|
75 | 75 | |
76 | - /** |
|
77 | - * Whether an object has a method. |
|
78 | - */ |
|
79 | - public function has_method(string $method): bool; |
|
76 | + /** |
|
77 | + * Whether an object has a method. |
|
78 | + */ |
|
79 | + public function has_method(string $method): bool; |
|
80 | 80 | } |
@@ -31,225 +31,225 @@ |
||
31 | 31 | */ |
32 | 32 | trait AccessorTrait |
33 | 33 | { |
34 | - use FormatAsSnake; |
|
35 | - |
|
36 | - public function __get($property) |
|
37 | - { |
|
38 | - return $this->accessor_get($property); |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * @throws ReflectionException |
|
43 | - */ |
|
44 | - public function __set($property, $value) |
|
45 | - { |
|
46 | - $this->accessor_set($property, $value); |
|
47 | - } |
|
48 | - |
|
49 | - public function has_property(string $property): bool |
|
50 | - { |
|
51 | - return property_exists($this, $property) |
|
52 | - || $this->has_method( |
|
53 | - static::accessor_format( |
|
54 | - $property, |
|
55 | - HasAccessor::ACCESSOR_TYPE_GETTER |
|
56 | - ) |
|
57 | - ) |
|
58 | - || $this->has_method( |
|
59 | - static::accessor_format( |
|
60 | - $property, |
|
61 | - HasAccessor::ACCESSOR_TYPE_GETTER, |
|
62 | - HasAccessor::ACCESSOR_IS_LAZY |
|
63 | - ) |
|
64 | - ) |
|
65 | - || $this->has_method( |
|
66 | - static::accessor_format( |
|
67 | - $property, |
|
68 | - HasAccessor::ACCESSOR_TYPE_SETTER |
|
69 | - ) |
|
70 | - ) |
|
71 | - || $this->has_method( |
|
72 | - static::accessor_format( |
|
73 | - $property, |
|
74 | - HasAccessor::ACCESSOR_TYPE_SETTER, |
|
75 | - HasAccessor::ACCESSOR_IS_LAZY |
|
76 | - ) |
|
77 | - ); |
|
78 | - } |
|
79 | - |
|
80 | - public function has_method(string $method): bool |
|
81 | - { |
|
82 | - return method_exists($this, $method); |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Returns the value of an inaccessible property. |
|
87 | - * |
|
88 | - * The method tries to get the property using the getter and lazy getter methods. |
|
89 | - * |
|
90 | - * @return mixed |
|
91 | - */ |
|
92 | - private function accessor_get(string $property) |
|
93 | - { |
|
94 | - $method = static::accessor_format( |
|
95 | - $property, |
|
96 | - HasAccessor::ACCESSOR_TYPE_GETTER |
|
97 | - ); |
|
98 | - |
|
99 | - if ($this->has_method($method)) { |
|
100 | - return $this->$method(); |
|
101 | - } |
|
102 | - |
|
103 | - $method = static::accessor_format( |
|
104 | - $property, |
|
105 | - HasAccessor::ACCESSOR_TYPE_GETTER, |
|
106 | - HasAccessor::ACCESSOR_IS_LAZY |
|
107 | - ); |
|
108 | - |
|
109 | - if ($this->has_method($method)) { |
|
110 | - return $this->$property = $this->$method(); |
|
111 | - } |
|
112 | - |
|
113 | - $this->assert_property_is_readable($property); |
|
114 | - } //@codeCoverageIgnore |
|
115 | - |
|
116 | - /** |
|
117 | - * Sets the value of an inaccessible property. |
|
118 | - * |
|
119 | - * The method is called because the property does not exist, its visibility is |
|
120 | - * _protected_ or _private_, or because although it is visible it was unset and is no |
|
121 | - * longer accessible. |
|
122 | - * |
|
123 | - * A `set_<property>` method can be used the handle virtual properties, for instance a |
|
124 | - * `minute` property that would alter a `second` property. |
|
125 | - * |
|
126 | - * A `lazy_set_<property>` method can be used to set properties that are protected or |
|
127 | - * private, which can be used to make properties write-only for example. |
|
128 | - * |
|
129 | - * @param mixed $value |
|
130 | - * |
|
131 | - * @throws ReflectionException |
|
132 | - */ |
|
133 | - private function accessor_set(string $property, $value): void |
|
134 | - { |
|
135 | - $method = static::accessor_format( |
|
136 | - $property, |
|
137 | - HasAccessor::ACCESSOR_TYPE_SETTER |
|
138 | - ); |
|
139 | - |
|
140 | - if ($this->has_method($method)) { |
|
141 | - $this->$method($value); |
|
142 | - |
|
143 | - return; |
|
144 | - } |
|
145 | - |
|
146 | - $method = static::accessor_format( |
|
147 | - $property, |
|
148 | - HasAccessor::ACCESSOR_TYPE_SETTER, |
|
149 | - HasAccessor::ACCESSOR_IS_LAZY |
|
150 | - ); |
|
151 | - |
|
152 | - if ($this->has_method($method)) { |
|
153 | - $this->$property = $this->$method($value); |
|
154 | - |
|
155 | - return; |
|
156 | - } |
|
157 | - |
|
158 | - $this->assert_property_is_writable($property); |
|
159 | - |
|
160 | - $this->$property = $value; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Asserts that a property is readable. |
|
165 | - * |
|
166 | - * @throws PropertyNotDefined when the property is not defined. |
|
167 | - * @throws PropertyNotReadable when the property is not accessible or is write-only |
|
168 | - * (the property is not defined and only a setter is available). |
|
169 | - */ |
|
170 | - private function assert_property_is_readable(string $property): void |
|
171 | - { |
|
172 | - try { |
|
173 | - $reflexion_class = new ReflectionClass($this); |
|
174 | - $reflexion_property = $reflexion_class->getProperty($property); |
|
175 | - |
|
176 | - if (!$reflexion_property->isPublic()) { |
|
177 | - throw new PropertyNotReadable([$property, $this]); |
|
178 | - } |
|
179 | - } catch (ReflectionException $e) { |
|
180 | - # |
|
181 | - # An exception may occur if the property is not defined, we don't care about that. |
|
182 | - # |
|
183 | - } |
|
184 | - |
|
185 | - $this->assert_no_accessor( |
|
186 | - $property, |
|
187 | - HasAccessor::ACCESSOR_TYPE_SETTER, |
|
188 | - PropertyNotReadable::class |
|
189 | - ); |
|
190 | - |
|
191 | - $properties = array_keys(get_object_vars($this)); |
|
192 | - |
|
193 | - if ($properties) { |
|
194 | - throw new PropertyNotDefined( |
|
195 | - sprintf( |
|
196 | - 'Unknown or inaccessible property "%s"' |
|
197 | - . ' for object of class "%s" (available properties: %s).', |
|
198 | - $property, |
|
199 | - get_class($this), |
|
200 | - implode(', ', $properties) |
|
201 | - ) |
|
202 | - ); |
|
203 | - } |
|
204 | - |
|
205 | - throw new PropertyNotDefined([$property, $this]); |
|
206 | - } |
|
207 | - |
|
208 | - /** |
|
209 | - * Asserts that a property is writable. |
|
210 | - * |
|
211 | - * @throws PropertyNotWritable|ReflectionException when the property doesn't exist, has no lazy getter and is |
|
212 | - * not public; or when only a getter is implemented. |
|
213 | - */ |
|
214 | - private function assert_property_is_writable(string $property): void |
|
215 | - { |
|
216 | - if ( |
|
217 | - property_exists($this, $property) && |
|
218 | - !$this->has_method( |
|
219 | - static::accessor_format( |
|
220 | - $property, |
|
221 | - HasAccessor::ACCESSOR_TYPE_GETTER, |
|
222 | - HasAccessor::ACCESSOR_IS_LAZY |
|
223 | - ) |
|
224 | - ) |
|
225 | - ) { |
|
226 | - $reflection = new ReflectionObject($this); |
|
227 | - $property_reflection = $reflection->getProperty($property); |
|
228 | - |
|
229 | - if (!$property_reflection->isPublic()) { |
|
230 | - throw new PropertyNotWritable([$property, $this]); |
|
231 | - } |
|
232 | - |
|
233 | - return; |
|
234 | - } |
|
235 | - |
|
236 | - $this->assert_no_accessor( |
|
237 | - $property, |
|
238 | - HasAccessor::ACCESSOR_TYPE_GETTER, |
|
239 | - PropertyNotWritable::class |
|
240 | - ); |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Asserts that an accessor is not implemented. |
|
245 | - * |
|
246 | - * @param string $type One of {@link HasAccessor::ACCESSOR_TYPE_GETTER} |
|
247 | - * and {@link HasAccessor::ACCESSOR_TYPE_SETTER}. |
|
248 | - */ |
|
249 | - private function assert_no_accessor(string $property, string $type, string $exception_class): void |
|
250 | - { |
|
251 | - if ($this->has_method(static::accessor_format($property, $type))) { |
|
252 | - throw new $exception_class([$property, $this]); |
|
253 | - } |
|
254 | - } |
|
34 | + use FormatAsSnake; |
|
35 | + |
|
36 | + public function __get($property) |
|
37 | + { |
|
38 | + return $this->accessor_get($property); |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * @throws ReflectionException |
|
43 | + */ |
|
44 | + public function __set($property, $value) |
|
45 | + { |
|
46 | + $this->accessor_set($property, $value); |
|
47 | + } |
|
48 | + |
|
49 | + public function has_property(string $property): bool |
|
50 | + { |
|
51 | + return property_exists($this, $property) |
|
52 | + || $this->has_method( |
|
53 | + static::accessor_format( |
|
54 | + $property, |
|
55 | + HasAccessor::ACCESSOR_TYPE_GETTER |
|
56 | + ) |
|
57 | + ) |
|
58 | + || $this->has_method( |
|
59 | + static::accessor_format( |
|
60 | + $property, |
|
61 | + HasAccessor::ACCESSOR_TYPE_GETTER, |
|
62 | + HasAccessor::ACCESSOR_IS_LAZY |
|
63 | + ) |
|
64 | + ) |
|
65 | + || $this->has_method( |
|
66 | + static::accessor_format( |
|
67 | + $property, |
|
68 | + HasAccessor::ACCESSOR_TYPE_SETTER |
|
69 | + ) |
|
70 | + ) |
|
71 | + || $this->has_method( |
|
72 | + static::accessor_format( |
|
73 | + $property, |
|
74 | + HasAccessor::ACCESSOR_TYPE_SETTER, |
|
75 | + HasAccessor::ACCESSOR_IS_LAZY |
|
76 | + ) |
|
77 | + ); |
|
78 | + } |
|
79 | + |
|
80 | + public function has_method(string $method): bool |
|
81 | + { |
|
82 | + return method_exists($this, $method); |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Returns the value of an inaccessible property. |
|
87 | + * |
|
88 | + * The method tries to get the property using the getter and lazy getter methods. |
|
89 | + * |
|
90 | + * @return mixed |
|
91 | + */ |
|
92 | + private function accessor_get(string $property) |
|
93 | + { |
|
94 | + $method = static::accessor_format( |
|
95 | + $property, |
|
96 | + HasAccessor::ACCESSOR_TYPE_GETTER |
|
97 | + ); |
|
98 | + |
|
99 | + if ($this->has_method($method)) { |
|
100 | + return $this->$method(); |
|
101 | + } |
|
102 | + |
|
103 | + $method = static::accessor_format( |
|
104 | + $property, |
|
105 | + HasAccessor::ACCESSOR_TYPE_GETTER, |
|
106 | + HasAccessor::ACCESSOR_IS_LAZY |
|
107 | + ); |
|
108 | + |
|
109 | + if ($this->has_method($method)) { |
|
110 | + return $this->$property = $this->$method(); |
|
111 | + } |
|
112 | + |
|
113 | + $this->assert_property_is_readable($property); |
|
114 | + } //@codeCoverageIgnore |
|
115 | + |
|
116 | + /** |
|
117 | + * Sets the value of an inaccessible property. |
|
118 | + * |
|
119 | + * The method is called because the property does not exist, its visibility is |
|
120 | + * _protected_ or _private_, or because although it is visible it was unset and is no |
|
121 | + * longer accessible. |
|
122 | + * |
|
123 | + * A `set_<property>` method can be used the handle virtual properties, for instance a |
|
124 | + * `minute` property that would alter a `second` property. |
|
125 | + * |
|
126 | + * A `lazy_set_<property>` method can be used to set properties that are protected or |
|
127 | + * private, which can be used to make properties write-only for example. |
|
128 | + * |
|
129 | + * @param mixed $value |
|
130 | + * |
|
131 | + * @throws ReflectionException |
|
132 | + */ |
|
133 | + private function accessor_set(string $property, $value): void |
|
134 | + { |
|
135 | + $method = static::accessor_format( |
|
136 | + $property, |
|
137 | + HasAccessor::ACCESSOR_TYPE_SETTER |
|
138 | + ); |
|
139 | + |
|
140 | + if ($this->has_method($method)) { |
|
141 | + $this->$method($value); |
|
142 | + |
|
143 | + return; |
|
144 | + } |
|
145 | + |
|
146 | + $method = static::accessor_format( |
|
147 | + $property, |
|
148 | + HasAccessor::ACCESSOR_TYPE_SETTER, |
|
149 | + HasAccessor::ACCESSOR_IS_LAZY |
|
150 | + ); |
|
151 | + |
|
152 | + if ($this->has_method($method)) { |
|
153 | + $this->$property = $this->$method($value); |
|
154 | + |
|
155 | + return; |
|
156 | + } |
|
157 | + |
|
158 | + $this->assert_property_is_writable($property); |
|
159 | + |
|
160 | + $this->$property = $value; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Asserts that a property is readable. |
|
165 | + * |
|
166 | + * @throws PropertyNotDefined when the property is not defined. |
|
167 | + * @throws PropertyNotReadable when the property is not accessible or is write-only |
|
168 | + * (the property is not defined and only a setter is available). |
|
169 | + */ |
|
170 | + private function assert_property_is_readable(string $property): void |
|
171 | + { |
|
172 | + try { |
|
173 | + $reflexion_class = new ReflectionClass($this); |
|
174 | + $reflexion_property = $reflexion_class->getProperty($property); |
|
175 | + |
|
176 | + if (!$reflexion_property->isPublic()) { |
|
177 | + throw new PropertyNotReadable([$property, $this]); |
|
178 | + } |
|
179 | + } catch (ReflectionException $e) { |
|
180 | + # |
|
181 | + # An exception may occur if the property is not defined, we don't care about that. |
|
182 | + # |
|
183 | + } |
|
184 | + |
|
185 | + $this->assert_no_accessor( |
|
186 | + $property, |
|
187 | + HasAccessor::ACCESSOR_TYPE_SETTER, |
|
188 | + PropertyNotReadable::class |
|
189 | + ); |
|
190 | + |
|
191 | + $properties = array_keys(get_object_vars($this)); |
|
192 | + |
|
193 | + if ($properties) { |
|
194 | + throw new PropertyNotDefined( |
|
195 | + sprintf( |
|
196 | + 'Unknown or inaccessible property "%s"' |
|
197 | + . ' for object of class "%s" (available properties: %s).', |
|
198 | + $property, |
|
199 | + get_class($this), |
|
200 | + implode(', ', $properties) |
|
201 | + ) |
|
202 | + ); |
|
203 | + } |
|
204 | + |
|
205 | + throw new PropertyNotDefined([$property, $this]); |
|
206 | + } |
|
207 | + |
|
208 | + /** |
|
209 | + * Asserts that a property is writable. |
|
210 | + * |
|
211 | + * @throws PropertyNotWritable|ReflectionException when the property doesn't exist, has no lazy getter and is |
|
212 | + * not public; or when only a getter is implemented. |
|
213 | + */ |
|
214 | + private function assert_property_is_writable(string $property): void |
|
215 | + { |
|
216 | + if ( |
|
217 | + property_exists($this, $property) && |
|
218 | + !$this->has_method( |
|
219 | + static::accessor_format( |
|
220 | + $property, |
|
221 | + HasAccessor::ACCESSOR_TYPE_GETTER, |
|
222 | + HasAccessor::ACCESSOR_IS_LAZY |
|
223 | + ) |
|
224 | + ) |
|
225 | + ) { |
|
226 | + $reflection = new ReflectionObject($this); |
|
227 | + $property_reflection = $reflection->getProperty($property); |
|
228 | + |
|
229 | + if (!$property_reflection->isPublic()) { |
|
230 | + throw new PropertyNotWritable([$property, $this]); |
|
231 | + } |
|
232 | + |
|
233 | + return; |
|
234 | + } |
|
235 | + |
|
236 | + $this->assert_no_accessor( |
|
237 | + $property, |
|
238 | + HasAccessor::ACCESSOR_TYPE_GETTER, |
|
239 | + PropertyNotWritable::class |
|
240 | + ); |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Asserts that an accessor is not implemented. |
|
245 | + * |
|
246 | + * @param string $type One of {@link HasAccessor::ACCESSOR_TYPE_GETTER} |
|
247 | + * and {@link HasAccessor::ACCESSOR_TYPE_SETTER}. |
|
248 | + */ |
|
249 | + private function assert_no_accessor(string $property, string $type, string $exception_class): void |
|
250 | + { |
|
251 | + if ($this->has_method(static::accessor_format($property, $type))) { |
|
252 | + throw new $exception_class([$property, $this]); |
|
253 | + } |
|
254 | + } |
|
255 | 255 | } |
@@ -25,96 +25,96 @@ |
||
25 | 25 | */ |
26 | 26 | final class AccessorReflection |
27 | 27 | { |
28 | - /** |
|
29 | - * @var array<class-string, ReflectionProperty[]> |
|
30 | - */ |
|
31 | - private static $private_properties_cache = []; |
|
32 | - |
|
33 | - /** |
|
34 | - * @var array<class-string, ReflectionProperty[]> |
|
35 | - */ |
|
36 | - private static $facade_properties_cache = []; |
|
37 | - |
|
38 | - /** |
|
39 | - * @param class-string|object $reference |
|
40 | - * |
|
41 | - * @return class-string |
|
42 | - */ |
|
43 | - private static function resolve_reference($reference): string |
|
44 | - { |
|
45 | - if (is_object($reference)) { |
|
46 | - return get_class($reference); |
|
47 | - } |
|
48 | - |
|
49 | - return $reference; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Returns the private properties defined by the reference, this includes the private |
|
54 | - * properties defined by the whole class inheritance. |
|
55 | - * |
|
56 | - * @param class-string|object $reference Class name or instance. |
|
57 | - * |
|
58 | - * @return ReflectionProperty[] |
|
59 | - * |
|
60 | - * @throws ReflectionException |
|
61 | - */ |
|
62 | - public static function resolve_private_properties($reference): array |
|
63 | - { |
|
64 | - $reference = self::resolve_reference($reference); |
|
65 | - $cached = &self::$private_properties_cache[$reference]; |
|
66 | - |
|
67 | - if ($cached) { |
|
68 | - return $cached; |
|
69 | - } |
|
70 | - |
|
71 | - $private_properties = []; |
|
72 | - $class_reflection = new ReflectionClass($reference); |
|
73 | - |
|
74 | - while ($class_reflection) { |
|
75 | - $private_properties[] = $class_reflection->getProperties(ReflectionProperty::IS_PRIVATE); |
|
76 | - $class_reflection = $class_reflection->getParentClass(); |
|
77 | - } |
|
78 | - |
|
79 | - return $cached = $private_properties ? array_merge(...$private_properties) : []; // @phpstan-ignore-line |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Returns the façade properties implemented by the specified reference. |
|
84 | - * |
|
85 | - * A façade property is a combination of a private property with the corresponding volatile |
|
86 | - * getter and setter. |
|
87 | - * |
|
88 | - * @param class-string|HasAccessor $reference Class name or instance implementing {@link HasAccessor}. |
|
89 | - * |
|
90 | - * @return ReflectionProperty[] |
|
91 | - * |
|
92 | - * @throws ReflectionException |
|
93 | - */ |
|
94 | - public static function resolve_facade_properties($reference): array |
|
95 | - { |
|
96 | - $reference = self::resolve_reference($reference); |
|
97 | - $facade_properties = &self::$facade_properties_cache[$reference]; |
|
98 | - |
|
99 | - if ($facade_properties) { |
|
100 | - return $facade_properties; |
|
101 | - } |
|
102 | - |
|
103 | - $facade_properties = []; |
|
104 | - |
|
105 | - foreach (self::resolve_private_properties($reference) as $property) { |
|
106 | - $name = $property->name; |
|
107 | - |
|
108 | - if ( |
|
109 | - !method_exists($reference, $reference::accessor_format($name, HasAccessor::ACCESSOR_TYPE_GETTER)) |
|
110 | - || !method_exists($reference, $reference::accessor_format($name, HasAccessor::ACCESSOR_TYPE_SETTER)) |
|
111 | - ) { |
|
112 | - continue; |
|
113 | - } |
|
114 | - |
|
115 | - $facade_properties[$name] = $property; |
|
116 | - } |
|
117 | - |
|
118 | - return $facade_properties; |
|
119 | - } |
|
28 | + /** |
|
29 | + * @var array<class-string, ReflectionProperty[]> |
|
30 | + */ |
|
31 | + private static $private_properties_cache = []; |
|
32 | + |
|
33 | + /** |
|
34 | + * @var array<class-string, ReflectionProperty[]> |
|
35 | + */ |
|
36 | + private static $facade_properties_cache = []; |
|
37 | + |
|
38 | + /** |
|
39 | + * @param class-string|object $reference |
|
40 | + * |
|
41 | + * @return class-string |
|
42 | + */ |
|
43 | + private static function resolve_reference($reference): string |
|
44 | + { |
|
45 | + if (is_object($reference)) { |
|
46 | + return get_class($reference); |
|
47 | + } |
|
48 | + |
|
49 | + return $reference; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * Returns the private properties defined by the reference, this includes the private |
|
54 | + * properties defined by the whole class inheritance. |
|
55 | + * |
|
56 | + * @param class-string|object $reference Class name or instance. |
|
57 | + * |
|
58 | + * @return ReflectionProperty[] |
|
59 | + * |
|
60 | + * @throws ReflectionException |
|
61 | + */ |
|
62 | + public static function resolve_private_properties($reference): array |
|
63 | + { |
|
64 | + $reference = self::resolve_reference($reference); |
|
65 | + $cached = &self::$private_properties_cache[$reference]; |
|
66 | + |
|
67 | + if ($cached) { |
|
68 | + return $cached; |
|
69 | + } |
|
70 | + |
|
71 | + $private_properties = []; |
|
72 | + $class_reflection = new ReflectionClass($reference); |
|
73 | + |
|
74 | + while ($class_reflection) { |
|
75 | + $private_properties[] = $class_reflection->getProperties(ReflectionProperty::IS_PRIVATE); |
|
76 | + $class_reflection = $class_reflection->getParentClass(); |
|
77 | + } |
|
78 | + |
|
79 | + return $cached = $private_properties ? array_merge(...$private_properties) : []; // @phpstan-ignore-line |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Returns the façade properties implemented by the specified reference. |
|
84 | + * |
|
85 | + * A façade property is a combination of a private property with the corresponding volatile |
|
86 | + * getter and setter. |
|
87 | + * |
|
88 | + * @param class-string|HasAccessor $reference Class name or instance implementing {@link HasAccessor}. |
|
89 | + * |
|
90 | + * @return ReflectionProperty[] |
|
91 | + * |
|
92 | + * @throws ReflectionException |
|
93 | + */ |
|
94 | + public static function resolve_facade_properties($reference): array |
|
95 | + { |
|
96 | + $reference = self::resolve_reference($reference); |
|
97 | + $facade_properties = &self::$facade_properties_cache[$reference]; |
|
98 | + |
|
99 | + if ($facade_properties) { |
|
100 | + return $facade_properties; |
|
101 | + } |
|
102 | + |
|
103 | + $facade_properties = []; |
|
104 | + |
|
105 | + foreach (self::resolve_private_properties($reference) as $property) { |
|
106 | + $name = $property->name; |
|
107 | + |
|
108 | + if ( |
|
109 | + !method_exists($reference, $reference::accessor_format($name, HasAccessor::ACCESSOR_TYPE_GETTER)) |
|
110 | + || !method_exists($reference, $reference::accessor_format($name, HasAccessor::ACCESSOR_TYPE_SETTER)) |
|
111 | + ) { |
|
112 | + continue; |
|
113 | + } |
|
114 | + |
|
115 | + $facade_properties[$name] = $property; |
|
116 | + } |
|
117 | + |
|
118 | + return $facade_properties; |
|
119 | + } |
|
120 | 120 | } |
@@ -25,75 +25,75 @@ |
||
25 | 25 | */ |
26 | 26 | trait SerializableTrait |
27 | 27 | { |
28 | - /** |
|
29 | - * @throws ReflectionException |
|
30 | - */ |
|
31 | - public function __sleep() |
|
32 | - { |
|
33 | - return $this->accessor_sleep(); |
|
34 | - } |
|
28 | + /** |
|
29 | + * @throws ReflectionException |
|
30 | + */ |
|
31 | + public function __sleep() |
|
32 | + { |
|
33 | + return $this->accessor_sleep(); |
|
34 | + } |
|
35 | 35 | |
36 | - public function __wakeup() |
|
37 | - { |
|
38 | - $this->accessor_wakeup(); |
|
39 | - } |
|
36 | + public function __wakeup() |
|
37 | + { |
|
38 | + $this->accessor_wakeup(); |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * The method returns an array of key/key pairs. |
|
43 | - * |
|
44 | - * Properties for which a lazy getter is defined are discarded. For instance, if the property |
|
45 | - * `next` is defined and the class of the instance defines the getter `lazy_get_next()`, the |
|
46 | - * property is discarded. |
|
47 | - * |
|
48 | - * Note that façade properties are also included. |
|
49 | - * |
|
50 | - * @throws ReflectionException |
|
51 | - */ |
|
52 | - private function accessor_sleep(): array |
|
53 | - { |
|
54 | - $properties = array_keys(get_object_vars($this)); |
|
41 | + /** |
|
42 | + * The method returns an array of key/key pairs. |
|
43 | + * |
|
44 | + * Properties for which a lazy getter is defined are discarded. For instance, if the property |
|
45 | + * `next` is defined and the class of the instance defines the getter `lazy_get_next()`, the |
|
46 | + * property is discarded. |
|
47 | + * |
|
48 | + * Note that façade properties are also included. |
|
49 | + * |
|
50 | + * @throws ReflectionException |
|
51 | + */ |
|
52 | + private function accessor_sleep(): array |
|
53 | + { |
|
54 | + $properties = array_keys(get_object_vars($this)); |
|
55 | 55 | |
56 | - if ($properties) { |
|
57 | - $properties = array_combine($properties, $properties); |
|
56 | + if ($properties) { |
|
57 | + $properties = array_combine($properties, $properties); |
|
58 | 58 | |
59 | - foreach ($properties as $property) { |
|
60 | - if ( |
|
61 | - $this->has_method(static::accessor_format( |
|
62 | - $property, |
|
63 | - HasAccessor::ACCESSOR_TYPE_GETTER, |
|
64 | - HasAccessor::ACCESSOR_IS_LAZY |
|
65 | - )) |
|
66 | - ) { |
|
67 | - unset($properties[$property]); |
|
68 | - } |
|
69 | - } |
|
70 | - } |
|
59 | + foreach ($properties as $property) { |
|
60 | + if ( |
|
61 | + $this->has_method(static::accessor_format( |
|
62 | + $property, |
|
63 | + HasAccessor::ACCESSOR_TYPE_GETTER, |
|
64 | + HasAccessor::ACCESSOR_IS_LAZY |
|
65 | + )) |
|
66 | + ) { |
|
67 | + unset($properties[$property]); |
|
68 | + } |
|
69 | + } |
|
70 | + } |
|
71 | 71 | |
72 | - foreach (AccessorReflection::resolve_facade_properties($this) as $name => $property) { |
|
73 | - $properties[$name] = "\x00" . $property->class . "\x00" . $name; |
|
74 | - } |
|
72 | + foreach (AccessorReflection::resolve_facade_properties($this) as $name => $property) { |
|
73 | + $properties[$name] = "\x00" . $property->class . "\x00" . $name; |
|
74 | + } |
|
75 | 75 | |
76 | - return $properties; |
|
77 | - } |
|
76 | + return $properties; |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Unsets null properties for which a lazy getter is defined so that it is called when |
|
81 | - * the property is accessed. |
|
82 | - */ |
|
83 | - private function accessor_wakeup(): void |
|
84 | - { |
|
85 | - $properties = get_object_vars($this); |
|
79 | + /** |
|
80 | + * Unsets null properties for which a lazy getter is defined so that it is called when |
|
81 | + * the property is accessed. |
|
82 | + */ |
|
83 | + private function accessor_wakeup(): void |
|
84 | + { |
|
85 | + $properties = get_object_vars($this); |
|
86 | 86 | |
87 | - foreach ($properties as $property => $value) { |
|
88 | - if ( |
|
89 | - $this->has_method(static::accessor_format( |
|
90 | - $property, |
|
91 | - HasAccessor::ACCESSOR_TYPE_GETTER, |
|
92 | - HasAccessor::ACCESSOR_IS_LAZY |
|
93 | - )) |
|
94 | - ) { |
|
95 | - unset($this->$property); |
|
96 | - } |
|
97 | - } |
|
98 | - } |
|
87 | + foreach ($properties as $property => $value) { |
|
88 | + if ( |
|
89 | + $this->has_method(static::accessor_format( |
|
90 | + $property, |
|
91 | + HasAccessor::ACCESSOR_TYPE_GETTER, |
|
92 | + HasAccessor::ACCESSOR_IS_LAZY |
|
93 | + )) |
|
94 | + ) { |
|
95 | + unset($this->$property); |
|
96 | + } |
|
97 | + } |
|
98 | + } |
|
99 | 99 | } |
@@ -70,7 +70,7 @@ |
||
70 | 70 | } |
71 | 71 | |
72 | 72 | foreach (AccessorReflection::resolve_facade_properties($this) as $name => $property) { |
73 | - $properties[$name] = "\x00" . $property->class . "\x00" . $name; |
|
73 | + $properties[$name] = "\x00".$property->class."\x00".$name; |
|
74 | 74 | } |
75 | 75 | |
76 | 76 | return $properties; |
@@ -16,11 +16,11 @@ |
||
16 | 16 | */ |
17 | 17 | trait FormatAsSnake |
18 | 18 | { |
19 | - public static function accessor_format( |
|
20 | - string $property, |
|
21 | - string $type, |
|
22 | - string $lazy = HasAccessor::ACCESSOR_IS_NOT_LAZY |
|
23 | - ): string { |
|
24 | - return ($lazy ? $lazy . '_' : '') . $type . '_' . $property; |
|
25 | - } |
|
19 | + public static function accessor_format( |
|
20 | + string $property, |
|
21 | + string $type, |
|
22 | + string $lazy = HasAccessor::ACCESSOR_IS_NOT_LAZY |
|
23 | + ): string { |
|
24 | + return ($lazy ? $lazy . '_' : '') . $type . '_' . $property; |
|
25 | + } |
|
26 | 26 | } |
@@ -21,6 +21,6 @@ |
||
21 | 21 | string $type, |
22 | 22 | string $lazy = HasAccessor::ACCESSOR_IS_NOT_LAZY |
23 | 23 | ): string { |
24 | - return ($lazy ? $lazy . '_' : '') . $type . '_' . $property; |
|
24 | + return ($lazy ? $lazy.'_' : '').$type.'_'.$property; |
|
25 | 25 | } |
26 | 26 | } |
@@ -18,13 +18,13 @@ |
||
18 | 18 | */ |
19 | 19 | trait FormatAsCamel |
20 | 20 | { |
21 | - public static function accessor_format( |
|
22 | - string $property, |
|
23 | - string $type, |
|
24 | - string $lazy = HasAccessor::ACCESSOR_IS_NOT_LAZY |
|
25 | - ): string { |
|
26 | - $format = $type . ucfirst($property); |
|
21 | + public static function accessor_format( |
|
22 | + string $property, |
|
23 | + string $type, |
|
24 | + string $lazy = HasAccessor::ACCESSOR_IS_NOT_LAZY |
|
25 | + ): string { |
|
26 | + $format = $type . ucfirst($property); |
|
27 | 27 | |
28 | - return $lazy ? $lazy . ucfirst($format) : $format; |
|
29 | - } |
|
28 | + return $lazy ? $lazy . ucfirst($format) : $format; |
|
29 | + } |
|
30 | 30 | } |
@@ -23,8 +23,8 @@ |
||
23 | 23 | string $type, |
24 | 24 | string $lazy = HasAccessor::ACCESSOR_IS_NOT_LAZY |
25 | 25 | ): string { |
26 | - $format = $type . ucfirst($property); |
|
26 | + $format = $type.ucfirst($property); |
|
27 | 27 | |
28 | - return $lazy ? $lazy . ucfirst($format) : $format; |
|
28 | + return $lazy ? $lazy.ucfirst($format) : $format; |
|
29 | 29 | } |
30 | 30 | } |