|
1 | 1 | <?php
|
2 | 2 |
|
3 | 3 | use Php\Support\Helpers\Arr;
|
| 4 | +use Php\Support\Helpers\Str; |
4 | 5 | use Php\Support\Structures\Collections\ReadableCollection;
|
5 | 6 |
|
6 | 7 | if (!function_exists('value')) {
|
@@ -306,24 +307,64 @@ function remoteStaticCallOrTrow(object|string|null $class, string $method, mixed
|
306 | 307 | }
|
307 | 308 | }
|
308 | 309 |
|
309 |
| -if (!function_exists('remoteCall')) { |
| 310 | +if (!function_exists('attributeToGetterMethod')) { |
310 | 311 | /**
|
311 |
| - * Returns result of an object's method if it exists in the object. |
312 |
| - * |
313 |
| - * @param object|null $class |
314 |
| - * @param string $method |
315 |
| - * @param mixed ...$params |
316 |
| - * |
317 |
| - * @return mixed |
| 312 | + * Returns getter-method's name or null by an attribute |
318 | 313 | */
|
319 |
| - function remoteCall(?object $class, string $method, mixed ...$params): mixed |
| 314 | + function attributeToGetterMethod(string $attribute): string |
320 | 315 | {
|
321 |
| - if (!$class) { |
322 |
| - return null; |
| 316 | + return 'get' . ucfirst($attribute); |
| 317 | + } |
| 318 | +} |
| 319 | + |
| 320 | +if (!function_exists('attributeToSetterMethod')) { |
| 321 | + /** |
| 322 | + * Returns getter-method's name or null by an attribute |
| 323 | + */ |
| 324 | + function attributeToSetterMethod(string $attribute): string |
| 325 | + { |
| 326 | + return 'set' . ucfirst($attribute); |
| 327 | + } |
| 328 | +} |
| 329 | + |
| 330 | +if (!function_exists('findGetterMethod')) { |
| 331 | + /** |
| 332 | + * Returns getter-method's name or null by an attribute |
| 333 | + */ |
| 334 | + function findGetterMethod(object $instance, string $attribute): ?string |
| 335 | + { |
| 336 | + if (method_exists($instance, $method = attributeToGetterMethod($attribute))) { |
| 337 | + return $method; |
323 | 338 | }
|
324 | 339 |
|
325 |
| - if (method_exists($class, $method)) { |
326 |
| - return $class->$method(...$params); |
| 340 | + return null; |
| 341 | + } |
| 342 | +} |
| 343 | + |
| 344 | + |
| 345 | +if (!function_exists('public_property_exists')) { |
| 346 | + /** |
| 347 | + * Returns existing public method (name) or null if missing |
| 348 | + */ |
| 349 | + function public_property_exists(object $instance, string $attribute): ?string |
| 350 | + { |
| 351 | + $property = Str::toLowerCamel($attribute); |
| 352 | + $vars = get_object_vars($instance); |
| 353 | + |
| 354 | + return array_key_exists($property, $vars) ? $property : null; |
| 355 | + } |
| 356 | +} |
| 357 | + |
| 358 | + |
| 359 | +if (!function_exists('get_property_value')) { |
| 360 | + /** |
| 361 | + * Returns a value from public property or null |
| 362 | + */ |
| 363 | + function get_property_value(object $instance, string $attribute): mixed |
| 364 | + { |
| 365 | + $property = public_property_exists($instance, $attribute); |
| 366 | + if ($property) { |
| 367 | + return $instance->$property; |
327 | 368 | }
|
328 | 369 |
|
329 | 370 | return null;
|
|
0 commit comments