diff options
Diffstat (limited to 'vendor/doctrine/persistence/src/Persistence/Reflection/RuntimeReflectionProperty.php')
-rw-r--r-- | vendor/doctrine/persistence/src/Persistence/Reflection/RuntimeReflectionProperty.php | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/vendor/doctrine/persistence/src/Persistence/Reflection/RuntimeReflectionProperty.php b/vendor/doctrine/persistence/src/Persistence/Reflection/RuntimeReflectionProperty.php new file mode 100644 index 0000000..5f52056 --- /dev/null +++ b/vendor/doctrine/persistence/src/Persistence/Reflection/RuntimeReflectionProperty.php | |||
@@ -0,0 +1,87 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\Persistence\Reflection; | ||
6 | |||
7 | use Doctrine\Common\Proxy\Proxy as CommonProxy; | ||
8 | use Doctrine\Persistence\Proxy; | ||
9 | use ReflectionProperty; | ||
10 | use ReturnTypeWillChange; | ||
11 | |||
12 | use function ltrim; | ||
13 | use function method_exists; | ||
14 | |||
15 | /** | ||
16 | * PHP Runtime Reflection Property. | ||
17 | * | ||
18 | * Avoids triggering lazy loading if the provided object | ||
19 | * is a {@see \Doctrine\Persistence\Proxy}. | ||
20 | */ | ||
21 | class RuntimeReflectionProperty extends ReflectionProperty | ||
22 | { | ||
23 | /** @var string */ | ||
24 | private $key; | ||
25 | |||
26 | /** @param class-string $class */ | ||
27 | public function __construct(string $class, string $name) | ||
28 | { | ||
29 | parent::__construct($class, $name); | ||
30 | |||
31 | $this->key = $this->isPrivate() ? "\0" . ltrim($class, '\\') . "\0" . $name : ($this->isProtected() ? "\0*\0" . $name : $name); | ||
32 | } | ||
33 | |||
34 | /** | ||
35 | * {@inheritDoc} | ||
36 | * | ||
37 | * @return mixed | ||
38 | */ | ||
39 | #[ReturnTypeWillChange] | ||
40 | public function getValue($object = null) | ||
41 | { | ||
42 | if ($object === null) { | ||
43 | return parent::getValue($object); | ||
44 | } | ||
45 | |||
46 | return ((array) $object)[$this->key] ?? null; | ||
47 | } | ||
48 | |||
49 | /** | ||
50 | * {@inheritDoc} | ||
51 | * | ||
52 | * @param object|null $object | ||
53 | * @param mixed $value | ||
54 | * | ||
55 | * @return void | ||
56 | */ | ||
57 | #[ReturnTypeWillChange] | ||
58 | public function setValue($object, $value = null) | ||
59 | { | ||
60 | if (! ($object instanceof Proxy && ! $object->__isInitialized())) { | ||
61 | parent::setValue($object, $value); | ||
62 | |||
63 | return; | ||
64 | } | ||
65 | |||
66 | if ($object instanceof CommonProxy) { | ||
67 | $originalInitializer = $object->__getInitializer(); | ||
68 | $object->__setInitializer(null); | ||
69 | |||
70 | parent::setValue($object, $value); | ||
71 | |||
72 | $object->__setInitializer($originalInitializer); | ||
73 | |||
74 | return; | ||
75 | } | ||
76 | |||
77 | if (! method_exists($object, '__setInitialized')) { | ||
78 | return; | ||
79 | } | ||
80 | |||
81 | $object->__setInitialized(true); | ||
82 | |||
83 | parent::setValue($object, $value); | ||
84 | |||
85 | $object->__setInitialized(false); | ||
86 | } | ||
87 | } | ||