Source of file ObjectHelper.php
Size: 2,557 Bytes - Last Modified: 2023-11-16T22:56:02+01:00
/home/websites/teicee/packagist/site/phpdoc/conf/../vendor/teicee/core-bundle/src/Util/ObjectHelper.php
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 | <?phpnamespace TIC\CoreBundle\Util; /** * Méthodes statiques utilitaires pour manipuler des objets. */class ObjectHelper {/** * This class should not be instantiated. */private function __construct() { } /** * Changement d'une valeur de propriété sur un objet. * * @param object $item Instance d'objet à modifier * @param string $property Nom de la propriété concernée * @param mixed $value Nouvelle valeur à affecter * @return bool Vrai si propriété modifiée ; Faux si inchangée */public static function changeField(object $item, string $property, mixed $value): bool { $cur_value = null; $accessor = 'get' . \ucfirst($property); if ( \method_exists($item, $accessor)) $cur_value = \call_user_func([$item, $accessor]); elseif (\property_exists($item, $property)) $cur_value = $item->{$property}; if ($cur_value === $value) return false; $mutator = 'set' . \ucfirst($property); if ( \method_exists($item, $mutator) ) \call_user_func([$item, $mutator], $value); elseif (\property_exists($item, $property)) $item->{$property} = $value; else throw new \InvalidArgumentException(\sprintf("No property named `%s` exists", $property)); return true; } /** * Inversion d'une valeur de propriété (booléenne) sur un objet. * * @param object $item Instance d'objet à modifier * @param string $property Nom de la propriété booléenne à inverser * @param bool $value Nouvelle valeur imposée plutôt qu'une inversion de la courante * @return bool Retourne la nouvelle valeur de la propriété ; Null si spécifiée et inchangée */public static function toggleField(object $item, string $property, ?bool $value = null): ?bool { $cur_value = null; $accessor = 'get' . \ucfirst($property); if ( \method_exists($item, $accessor)) $cur_value = \call_user_func([$item, $accessor]); elseif (\property_exists($item, $property)) $cur_value = $item->{$property}; // valeur inverse par défaut ou retour null si inchangée if ($value === null) $value = ($cur_value) ? false : true; elseif ($value === $cur_value) return null; $mutator = 'set' . \ucfirst($property); if ( \method_exists($item, $mutator) ) \call_user_func([$item, $mutator], $value); elseif (\property_exists($item, $property)) $item->{$property} = $value; else throw new \InvalidArgumentException(\sprintf("No property named `%s` exists", $property)); return $value; } } |