1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is part of Cycle ORM package. |
||
5 | * |
||
6 | * For the full copyright and license information, please view the LICENSE |
||
7 | * file that was distributed with this source code. |
||
8 | */ |
||
9 | |||
10 | declare(strict_types=1); |
||
11 | |||
12 | namespace Cycle\Database\Schema\Traits; |
||
13 | |||
14 | use Cycle\Database\Schema\Attribute\ColumnAttribute; |
||
15 | |||
16 | trait ColumnAttributesTrait |
||
17 | { |
||
18 | /** |
||
19 | * Additional attributes. |
||
20 | * |
||
21 | * @var array<non-empty-string, mixed> |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
22 | */ |
||
23 | protected array $attributes = []; |
||
24 | |||
25 | /** |
||
26 | * @see \Cycle\Database\Schema\AbstractColumn::getInternalType() |
||
27 | */ |
||
28 | abstract public function getInternalType(): string; |
||
29 | |||
30 | /** |
||
31 | * @param array<non-empty-string, mixed> $attributes |
||
0 ignored issues
–
show
|
|||
32 | */ |
||
33 | public function setAttributes(array $attributes): static |
||
34 | { |
||
35 | $this->attributes = []; |
||
36 | $this->fillAttributes($attributes); |
||
37 | return $this; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Get all related and additional attributes. |
||
42 | * |
||
43 | * @return array<non-empty-string, mixed> |
||
0 ignored issues
–
show
|
|||
44 | */ |
||
45 | public function getAttributes(): array |
||
46 | { |
||
47 | $result = $this->attributes; |
||
48 | foreach ($this->getAttributesMap() as $field => $attribute) { |
||
49 | if ($attribute->types !== null && !\in_array($this->getInternalType(), $attribute->types, true)) { |
||
50 | continue; |
||
51 | } |
||
52 | $result[$field] = $this->$field; |
||
53 | } |
||
54 | return $result; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @param non-empty-string $name |
||
0 ignored issues
–
show
|
|||
59 | * |
||
60 | * @return bool Returns {@see true} if attribute is defined in the current driver {@see static}. |
||
61 | */ |
||
62 | protected function isAttribute(string $name): bool |
||
63 | { |
||
64 | $map = $this->getAttributesMap(); |
||
65 | return match (true) { |
||
66 | !\array_key_exists($name, $map) => false, |
||
67 | $map[$name]->types === null, |
||
68 | \in_array($this->getInternalType(), $map[$name]->types, true) => true, |
||
69 | default => false, |
||
70 | }; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param array<non-empty-string, mixed> $attributes |
||
0 ignored issues
–
show
|
|||
75 | */ |
||
76 | protected function fillAttributes(array $attributes): void |
||
77 | { |
||
78 | if ($attributes === []) { |
||
79 | return; |
||
80 | } |
||
81 | |||
82 | foreach ($this->getAttributesMap() as $name => $attribute) { |
||
83 | if ($attribute->types !== null && !\in_array($this->getInternalType(), $attribute->types, true)) { |
||
84 | continue; |
||
85 | } |
||
86 | if (\array_key_exists($name, $attributes)) { |
||
87 | $this->$name = $attributes[$name]; |
||
88 | unset($attributes[$name]); |
||
89 | } |
||
90 | } |
||
91 | $this->attributes = \array_merge($this->attributes, $attributes); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return array<non-empty-string, ColumnAttribute> |
||
0 ignored issues
–
show
|
|||
96 | */ |
||
97 | private function getAttributesMap(): array |
||
98 | { |
||
99 | // Use cache to avoid reflection on each call |
||
100 | static $cache = []; |
||
101 | if (isset($cache[static::class])) { |
||
102 | return $cache[static::class]; |
||
103 | } |
||
104 | |||
105 | $map = []; |
||
106 | $reflection = new \ReflectionClass(static::class); |
||
107 | foreach ($reflection->getProperties() as $property) { |
||
108 | $attribute = $property->getAttributes(ColumnAttribute::class)[0] ?? null; |
||
109 | if ($attribute === null) { |
||
110 | continue; |
||
111 | } |
||
112 | |||
113 | $map[$property->getName()] = $attribute->newInstance(); |
||
114 | } |
||
115 | $cache[static::class] = $map; |
||
116 | return $map; |
||
117 | } |
||
118 | } |
||
119 |