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

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