Skip to content

Commit a847cd1

Browse files
author
Eugene Fureev
committed
feat: add some global functions
1 parent c011e9b commit a847cd1

File tree

2 files changed

+61
-13
lines changed

2 files changed

+61
-13
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].
66

7+
## v4.28.0
8+
9+
### Added
10+
11+
- Add global functions: `attributeToGetterMethod`, `attributeToSetterMethod`,
12+
`findGetterMethod`, `public_property_exists`, `get_property_value`
13+
714
## v4.27.0
815

916
### Added

src/Global/base.php

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Php\Support\Helpers\Arr;
4+
use Php\Support\Helpers\Str;
45
use Php\Support\Structures\Collections\ReadableCollection;
56

67
if (!function_exists('value')) {
@@ -306,24 +307,64 @@ function remoteStaticCallOrTrow(object|string|null $class, string $method, mixed
306307
}
307308
}
308309

309-
if (!function_exists('remoteCall')) {
310+
if (!function_exists('attributeToGetterMethod')) {
310311
/**
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
318313
*/
319-
function remoteCall(?object $class, string $method, mixed ...$params): mixed
314+
function attributeToGetterMethod(string $attribute): string
320315
{
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;
323338
}
324339

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;
327368
}
328369

329370
return null;

0 commit comments

Comments
 (0)