Hydrator.php 2.9 KB

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