1 | <?php |
||
7 | class Property extends ReflectionProperty |
||
8 | { |
||
9 | /** @var array */ |
||
10 | protected static $typeMapping = [ |
||
11 | 'int' => 'integer', |
||
12 | 'bool' => 'boolean', |
||
13 | ]; |
||
14 | |||
15 | /** @var \Spatie\ValueObject\ValueObject */ |
||
16 | protected $valueObject; |
||
17 | |||
18 | /** @var bool */ |
||
19 | protected $hasTypeDeclaration = false; |
||
20 | |||
21 | /** @var bool */ |
||
22 | protected $isNullable = true; |
||
23 | |||
24 | /** @var bool */ |
||
25 | protected $isInitialised = false; |
||
26 | |||
27 | /** @var array */ |
||
28 | protected $types = []; |
||
29 | |||
30 | public static function fromReflection(ValueObject $valueObject, ReflectionProperty $reflectionProperty) |
||
34 | |||
35 | public function __construct(ValueObject $valueObject, ReflectionProperty $reflectionProperty) |
||
43 | |||
44 | public function set($value) |
||
45 | { |
||
46 | if (! $this->isValidType($value)) { |
||
47 | throw ValueObjectError::invalidType($this, $value); |
||
48 | } |
||
49 | |||
50 | $this->isInitialised = true; |
||
51 | |||
52 | $this->valueObject->{$this->getName()} = $value; |
||
53 | } |
||
54 | |||
55 | public function getTypes(): array |
||
56 | { |
||
57 | return $this->types; |
||
58 | } |
||
59 | |||
60 | public function getFqn(): string |
||
61 | { |
||
62 | return "{$this->getDeclaringClass()->getName()}::{$this->getName()}"; |
||
63 | } |
||
64 | |||
65 | public function isNullable(): bool |
||
66 | { |
||
67 | return $this->isNullable; |
||
68 | } |
||
69 | |||
70 | protected function resolveTypeDefinition() |
||
92 | |||
93 | protected function isValidType($value): bool |
||
113 | |||
114 | protected function assertTypeEquals(string $type, $value): bool |
||
115 | { |
||
116 | if (strpos($type, '[]') !== false) { |
||
117 | return $this->isValidGenericCollection($type, $value); |
||
127 | |||
128 | protected function isValidGenericCollection(string $type, $collection): bool |
||
144 | } |
||
145 |