vendor/doctrine/persistence/src/Persistence/Reflection/RuntimeReflectionProperty.php line 59

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Persistence\Reflection;
  4. use Doctrine\Common\Proxy\Proxy as CommonProxy;
  5. use Doctrine\Persistence\Proxy;
  6. use ReflectionProperty;
  7. use ReturnTypeWillChange;
  8. use function method_exists;
  9. /**
  10.  * PHP Runtime Reflection Property.
  11.  *
  12.  * Avoids triggering lazy loading if the provided object
  13.  * is a {@see \Doctrine\Persistence\Proxy}.
  14.  */
  15. class RuntimeReflectionProperty extends ReflectionProperty
  16. {
  17.     /** @var string */
  18.     private $key;
  19.     public function __construct(string $classstring $name)
  20.     {
  21.         parent::__construct($class$name);
  22.         $this->key $this->isPrivate() ? "\0" $class "\0" $name : ($this->isProtected() ? "\0*\0" $name $name);
  23.     }
  24.     /**
  25.      * {@inheritDoc}
  26.      *
  27.      * @return mixed
  28.      */
  29.     #[ReturnTypeWillChange]
  30.     public function getValue($object null)
  31.     {
  32.         if ($object === null) {
  33.             return parent::getValue($object);
  34.         }
  35.         return ((array) $object)[$this->key] ?? null;
  36.     }
  37.     /**
  38.      * {@inheritDoc}
  39.      *
  40.      * @param object|null $object
  41.      * @param mixed       $value
  42.      *
  43.      * @return void
  44.      */
  45.     #[ReturnTypeWillChange]
  46.     public function setValue($object$value null)
  47.     {
  48.         if (! ($object instanceof Proxy && ! $object->__isInitialized())) {
  49.             parent::setValue($object$value);
  50.             return;
  51.         }
  52.         if ($object instanceof CommonProxy) {
  53.             $originalInitializer $object->__getInitializer();
  54.             $object->__setInitializer(null);
  55.             parent::setValue($object$value);
  56.             $object->__setInitializer($originalInitializer);
  57.             return;
  58.         }
  59.         if (! method_exists($object'__setInitialized')) {
  60.             return;
  61.         }
  62.         $object->__setInitialized(true);
  63.         parent::setValue($object$value);
  64.         $object->__setInitialized(false);
  65.     }
  66. }