diff options
Diffstat (limited to 'vendor/symfony/var-exporter/Hydrator.php')
| -rw-r--r-- | vendor/symfony/var-exporter/Hydrator.php | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/vendor/symfony/var-exporter/Hydrator.php b/vendor/symfony/var-exporter/Hydrator.php new file mode 100644 index 0000000..5f456fb --- /dev/null +++ b/vendor/symfony/var-exporter/Hydrator.php | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | /* | ||
| 4 | * This file is part of the Symfony package. | ||
| 5 | * | ||
| 6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
| 7 | * | ||
| 8 | * For the full copyright and license information, please view the LICENSE | ||
| 9 | * file that was distributed with this source code. | ||
| 10 | */ | ||
| 11 | |||
| 12 | namespace Symfony\Component\VarExporter; | ||
| 13 | |||
| 14 | use Symfony\Component\VarExporter\Internal\Hydrator as InternalHydrator; | ||
| 15 | |||
| 16 | /** | ||
| 17 | * Utility class to hydrate the properties of an object. | ||
| 18 | * | ||
| 19 | * @author Nicolas Grekas <p@tchwork.com> | ||
| 20 | */ | ||
| 21 | final class Hydrator | ||
| 22 | { | ||
| 23 | /** | ||
| 24 | * Sets the properties of an object, including private and protected ones. | ||
| 25 | * | ||
| 26 | * For example: | ||
| 27 | * | ||
| 28 | * // Sets the public or protected $object->propertyName property | ||
| 29 | * Hydrator::hydrate($object, ['propertyName' => $propertyValue]); | ||
| 30 | * | ||
| 31 | * // Sets a private property defined on its parent Bar class: | ||
| 32 | * Hydrator::hydrate($object, ["\0Bar\0privateBarProperty" => $propertyValue]); | ||
| 33 | * | ||
| 34 | * // Alternative way to set the private $object->privateBarProperty property | ||
| 35 | * Hydrator::hydrate($object, [], [ | ||
| 36 | * Bar::class => ['privateBarProperty' => $propertyValue], | ||
| 37 | * ]); | ||
| 38 | * | ||
| 39 | * Instances of ArrayObject, ArrayIterator and SplObjectStorage can be hydrated | ||
| 40 | * by using the special "\0" property name to define their internal value: | ||
| 41 | * | ||
| 42 | * // Hydrates an SplObjectStorage where $info1 is attached to $obj1, etc. | ||
| 43 | * Hydrator::hydrate($object, ["\0" => [$obj1, $info1, $obj2, $info2...]]); | ||
| 44 | * | ||
| 45 | * // Hydrates an ArrayObject populated with $inputArray | ||
| 46 | * Hydrator::hydrate($object, ["\0" => [$inputArray]]); | ||
| 47 | * | ||
| 48 | * @template T of object | ||
| 49 | * | ||
| 50 | * @param T $instance The object to hydrate | ||
| 51 | * @param array<string, mixed> $properties The properties to set on the instance | ||
| 52 | * @param array<class-string, array<string, mixed>> $scopedProperties The properties to set on the instance, | ||
| 53 | * keyed by their declaring class | ||
| 54 | * | ||
| 55 | * @return T | ||
| 56 | */ | ||
| 57 | public static function hydrate(object $instance, array $properties = [], array $scopedProperties = []): object | ||
| 58 | { | ||
| 59 | if ($properties) { | ||
| 60 | $class = $instance::class; | ||
| 61 | $propertyScopes = InternalHydrator::$propertyScopes[$class] ??= InternalHydrator::getPropertyScopes($class); | ||
| 62 | |||
| 63 | foreach ($properties as $name => &$value) { | ||
| 64 | [$scope, $name, $readonlyScope] = $propertyScopes[$name] ?? [$class, $name, $class]; | ||
| 65 | $scopedProperties[$readonlyScope ?? $scope][$name] = &$value; | ||
| 66 | } | ||
| 67 | unset($value); | ||
| 68 | } | ||
| 69 | |||
| 70 | foreach ($scopedProperties as $scope => $properties) { | ||
| 71 | if ($properties) { | ||
| 72 | (InternalHydrator::$simpleHydrators[$scope] ??= InternalHydrator::getSimpleHydrator($scope))($properties, $instance); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | return $instance; | ||
| 77 | } | ||
| 78 | } | ||
