summaryrefslogtreecommitdiff
path: root/vendor/doctrine/orm/src/Mapping/ReflectionReadonlyProperty.php
blob: 13e9f6ddda3922d66b019f29d0e4149d2502219a (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
<?php

declare(strict_types=1);

namespace Doctrine\ORM\Mapping;

use InvalidArgumentException;
use LogicException;
use ReflectionProperty;

use function assert;
use function func_get_args;
use function func_num_args;
use function is_object;
use function sprintf;

/** @internal */
final class ReflectionReadonlyProperty extends ReflectionProperty
{
    public function __construct(
        private readonly ReflectionProperty $wrappedProperty,
    ) {
        if (! $wrappedProperty->isReadOnly()) {
            throw new InvalidArgumentException('Given property is not readonly.');
        }

        parent::__construct($wrappedProperty->class, $wrappedProperty->name);
    }

    public function getValue(object|null $object = null): mixed
    {
        return $this->wrappedProperty->getValue(...func_get_args());
    }

    public function setValue(mixed $objectOrValue, mixed $value = null): void
    {
        if (func_num_args() < 2 || $objectOrValue === null || ! $this->isInitialized($objectOrValue)) {
            $this->wrappedProperty->setValue(...func_get_args());

            return;
        }

        assert(is_object($objectOrValue));

        if (parent::getValue($objectOrValue) !== $value) {
            throw new LogicException(sprintf('Attempting to change readonly property %s::$%s.', $this->class, $this->name));
        }
    }
}