diff options
Diffstat (limited to 'vendor/doctrine/orm/src/Mapping/ReflectionEmbeddedProperty.php')
-rw-r--r-- | vendor/doctrine/orm/src/Mapping/ReflectionEmbeddedProperty.php | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/Mapping/ReflectionEmbeddedProperty.php b/vendor/doctrine/orm/src/Mapping/ReflectionEmbeddedProperty.php new file mode 100644 index 0000000..da3d097 --- /dev/null +++ b/vendor/doctrine/orm/src/Mapping/ReflectionEmbeddedProperty.php | |||
@@ -0,0 +1,61 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\ORM\Mapping; | ||
6 | |||
7 | use Doctrine\Instantiator\Instantiator; | ||
8 | use ReflectionProperty; | ||
9 | |||
10 | /** | ||
11 | * Acts as a proxy to a nested Property structure, making it look like | ||
12 | * just a single scalar property. | ||
13 | * | ||
14 | * This way value objects "just work" without UnitOfWork, Persisters or Hydrators | ||
15 | * needing any changes. | ||
16 | * | ||
17 | * TODO: Move this class into Common\Reflection | ||
18 | */ | ||
19 | final class ReflectionEmbeddedProperty extends ReflectionProperty | ||
20 | { | ||
21 | private Instantiator|null $instantiator = null; | ||
22 | |||
23 | /** | ||
24 | * @param ReflectionProperty $parentProperty reflection property of the class where the embedded object has to be put | ||
25 | * @param ReflectionProperty $childProperty reflection property of the embedded object | ||
26 | * @psalm-param class-string $embeddedClass | ||
27 | */ | ||
28 | public function __construct( | ||
29 | private readonly ReflectionProperty $parentProperty, | ||
30 | private readonly ReflectionProperty $childProperty, | ||
31 | private readonly string $embeddedClass, | ||
32 | ) { | ||
33 | parent::__construct($childProperty->getDeclaringClass()->name, $childProperty->getName()); | ||
34 | } | ||
35 | |||
36 | public function getValue(object|null $object = null): mixed | ||
37 | { | ||
38 | $embeddedObject = $this->parentProperty->getValue($object); | ||
39 | |||
40 | if ($embeddedObject === null) { | ||
41 | return null; | ||
42 | } | ||
43 | |||
44 | return $this->childProperty->getValue($embeddedObject); | ||
45 | } | ||
46 | |||
47 | public function setValue(mixed $object, mixed $value = null): void | ||
48 | { | ||
49 | $embeddedObject = $this->parentProperty->getValue($object); | ||
50 | |||
51 | if ($embeddedObject === null) { | ||
52 | $this->instantiator ??= new Instantiator(); | ||
53 | |||
54 | $embeddedObject = $this->instantiator->instantiate($this->embeddedClass); | ||
55 | |||
56 | $this->parentProperty->setValue($object, $embeddedObject); | ||
57 | } | ||
58 | |||
59 | $this->childProperty->setValue($embeddedObject, $value); | ||
60 | } | ||
61 | } | ||