diff options
Diffstat (limited to 'vendor/doctrine/orm/src/Mapping/ReflectionReadonlyProperty.php')
-rw-r--r-- | vendor/doctrine/orm/src/Mapping/ReflectionReadonlyProperty.php | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/Mapping/ReflectionReadonlyProperty.php b/vendor/doctrine/orm/src/Mapping/ReflectionReadonlyProperty.php new file mode 100644 index 0000000..13e9f6d --- /dev/null +++ b/vendor/doctrine/orm/src/Mapping/ReflectionReadonlyProperty.php | |||
@@ -0,0 +1,49 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\ORM\Mapping; | ||
6 | |||
7 | use InvalidArgumentException; | ||
8 | use LogicException; | ||
9 | use ReflectionProperty; | ||
10 | |||
11 | use function assert; | ||
12 | use function func_get_args; | ||
13 | use function func_num_args; | ||
14 | use function is_object; | ||
15 | use function sprintf; | ||
16 | |||
17 | /** @internal */ | ||
18 | final class ReflectionReadonlyProperty extends ReflectionProperty | ||
19 | { | ||
20 | public function __construct( | ||
21 | private readonly ReflectionProperty $wrappedProperty, | ||
22 | ) { | ||
23 | if (! $wrappedProperty->isReadOnly()) { | ||
24 | throw new InvalidArgumentException('Given property is not readonly.'); | ||
25 | } | ||
26 | |||
27 | parent::__construct($wrappedProperty->class, $wrappedProperty->name); | ||
28 | } | ||
29 | |||
30 | public function getValue(object|null $object = null): mixed | ||
31 | { | ||
32 | return $this->wrappedProperty->getValue(...func_get_args()); | ||
33 | } | ||
34 | |||
35 | public function setValue(mixed $objectOrValue, mixed $value = null): void | ||
36 | { | ||
37 | if (func_num_args() < 2 || $objectOrValue === null || ! $this->isInitialized($objectOrValue)) { | ||
38 | $this->wrappedProperty->setValue(...func_get_args()); | ||
39 | |||
40 | return; | ||
41 | } | ||
42 | |||
43 | assert(is_object($objectOrValue)); | ||
44 | |||
45 | if (parent::getValue($objectOrValue) !== $value) { | ||
46 | throw new LogicException(sprintf('Attempting to change readonly property %s::$%s.', $this->class, $this->name)); | ||
47 | } | ||
48 | } | ||
49 | } | ||