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