Total Complexity | 25 |
Total Lines | 233 |
Duplicated Lines | 0 % |
Coverage | 73.81% |
Changes | 0 |
1 | <?php |
||
91 | trait PropertiesHandler |
||
92 | { |
||
93 | /** |
||
94 | * Gets the property value using the auto-magic method `$getterPrefix.$name()` (getter), |
||
95 | * where `$name` is the name of property and `$getterPrefix` is 'get' by default (but can be customized). |
||
1 ignored issue
–
show
|
|||
96 | * |
||
97 | * @param string $name Property name. |
||
98 | * |
||
99 | * @return mixed |
||
100 | * @throws BadMethodCallException If unable to get the property value. |
||
101 | * @see PropertiesHandler::getPropertyGetter() |
||
102 | * */ |
||
103 | 327 | public function __get($name) |
|
104 | { |
||
105 | try { |
||
106 | 327 | $getter = static::getPropertyGetter($name); |
|
107 | 4 | } catch (InvalidArgumentException $error) { |
|
108 | 4 | $msg = msg('Unable to get the property value in "{0}" class.', get_class($this)); |
|
109 | 4 | throw new BadMethodCallException($msg, 31, $error); |
|
110 | } catch (BadMethodCallException $error) { |
||
111 | $msg = msg('Unable to get the property value in "{0}" class.', get_class($this)); |
||
112 | throw new BadMethodCallException($msg, 32, $error); |
||
113 | } |
||
114 | |||
115 | 327 | return $this->$getter(); |
|
116 | } |
||
117 | |||
118 | |||
119 | /** |
||
120 | * Sets the property value using the auto-magic method `$setterPrefix.$name()` (setter), |
||
121 | * where `$name` is the name of property and `$setterPrefix` is 'set' by default (but can be customized). |
||
1 ignored issue
–
show
|
|||
122 | * |
||
123 | * @param string $name Property name. |
||
124 | * @param mixed $value Property value. |
||
125 | * |
||
126 | * @return void |
||
127 | * @throws BadMethodCallException If unable to set property value. |
||
128 | * @see PropertiesHandler::getPropertySetter() |
||
129 | * */ |
||
130 | 34 | public function __set($name, $value) |
|
131 | { |
||
132 | try { |
||
133 | 34 | $setter = static::getPropertySetter($name); |
|
134 | 28 | } catch (InvalidArgumentException $error) { |
|
135 | 28 | $msg = msg('Unable to set the property value in "{0}" class.', get_class($this)); |
|
136 | 28 | throw new BadMethodCallException($msg, 41, $error); |
|
137 | } catch (BadMethodCallException $error) { |
||
138 | $msg = msg('Unable to set the property value in "{0}" class.', get_class($this)); |
||
139 | throw new BadMethodCallException($msg, 42, $error); |
||
140 | } |
||
141 | |||
142 | 8 | $this->$setter($value); |
|
143 | 6 | } |
|
144 | |||
145 | |||
146 | /** |
||
147 | * Ensures that property provided exists in this class. |
||
148 | * |
||
149 | * @param string $name Property name. |
||
150 | * |
||
151 | * @return string Same property name, but validated. |
||
152 | * @throws InvalidArgumentException If property name is not valid (10) or do not exists (11). |
||
153 | */ |
||
154 | 328 | protected static function ensurePropertyExists($name) |
|
155 | { |
||
156 | $args = [ |
||
157 | 328 | 'class' => get_called_class(), |
|
158 | ]; |
||
159 | |||
160 | try { |
||
161 | 328 | $args['property'] = Text::ensureIsValidVarName($name); |
|
162 | } catch (InvalidArgumentException $error) { |
||
163 | $msg = msg('Property name is not valid.'); |
||
164 | throw new InvalidArgumentException($msg, 10, $error); |
||
165 | } |
||
166 | |||
167 | 328 | if (!property_exists($args['class'], $args['property'])) { |
|
168 | // Check in parent classes for private property |
||
169 | 15 | $current = $args['class']; |
|
170 | 15 | $exists = false; |
|
171 | 15 | while ($current = get_parent_class($current) and !$exists) { |
|
172 | 11 | $exists = property_exists($current, $args['property']); |
|
173 | } |
||
174 | |||
175 | 15 | if (!$exists) { |
|
176 | 12 | $msg = msg( |
|
177 | 12 | '"{property}" property do not exists in "{class}" class or parent classes.', |
|
178 | 12 | $args |
|
179 | ); |
||
180 | 12 | throw new InvalidArgumentException($msg, 11); |
|
181 | } |
||
182 | } |
||
183 | |||
184 | 328 | return $name; |
|
185 | } |
||
186 | |||
187 | |||
188 | /** |
||
189 | * Ensures that method provided exists in this class. |
||
190 | * |
||
191 | * @param string $name Method name. |
||
192 | * |
||
193 | * @return string Same method name, but validated. |
||
194 | * @throws InvalidArgumentException If method name is not valid (20) or do not exists (21). |
||
195 | */ |
||
196 | 328 | protected static function ensureMethodExists($name) |
|
197 | { |
||
198 | $args = [ |
||
199 | 328 | 'class' => get_called_class(), |
|
200 | ]; |
||
201 | |||
202 | try { |
||
203 | 328 | $args['method'] = Text::ensureIsValidVarName($name); |
|
204 | } catch (InvalidArgumentException $error) { |
||
205 | $msg = msg('Method name is not valid.'); |
||
206 | throw new InvalidArgumentException($msg, 20, $error); |
||
207 | } |
||
208 | |||
209 | 328 | if (method_exists($args['class'], $args['method']) === false) { |
|
210 | 23 | $msg = msg('"{class}::{method}" do not exists.', $args); |
|
211 | |||
212 | 23 | throw new InvalidArgumentException($msg, 21); |
|
213 | } |
||
214 | |||
215 | 328 | return $name; |
|
216 | } |
||
217 | |||
218 | |||
219 | /** |
||
220 | * Gets the property setter method name. |
||
221 | * You can customize the setter prefix by implementing ``ICustomPrefixedPropertiesContainer`` interface. |
||
1 ignored issue
–
show
|
|||
222 | * |
||
223 | * @param string $name Property name. |
||
224 | * |
||
225 | * @return string |
||
226 | * @throws InvalidArgumentException If property is not valid or has not setter. |
||
227 | * @throws BadMethodCallException If custom prefix is not an ``string`` instance. |
||
228 | * @see ICustomPrefixedPropertiesContainer::getCustomSetterPrefix() |
||
229 | */ |
||
230 | 34 | protected static function getPropertySetter($name) |
|
270 | } |
||
271 | |||
272 | |||
273 | /** |
||
274 | * Gets the property getter method name. |
||
275 | * You can customize the getter prefix by implementing ``ICustomPrefixedPropertiesContainer`` interface. |
||
1 ignored issue
–
show
|
|||
276 | * |
||
277 | * @param string $name Property name. |
||
278 | * |
||
279 | * @return string |
||
280 | * @throws InvalidArgumentException If property is not valid or has not getter. |
||
281 | * @throws BadMethodCallException If custom prefix is not an ``string`` instance. |
||
282 | * @see ICustomPrefixedPropertiesContainer::getCustomGetterPrefix() |
||
283 | */ |
||
284 | 327 | protected static function getPropertyGetter($name) |
|
324 | } |
||
325 | } |
||
326 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.