1 | <?php |
||
2 | |||
3 | namespace Bdf\Prime\Mapper\Info; |
||
4 | |||
5 | use Bdf\Prime\Relations\RelationInterface; |
||
6 | |||
7 | /** |
||
8 | * ObjectPropertyInfo |
||
9 | * |
||
10 | * @package Bdf\Prime\Mapper\Info |
||
11 | */ |
||
12 | class ObjectPropertyInfo implements InfoInterface |
||
13 | { |
||
14 | /** |
||
15 | * The property name |
||
16 | * |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $name; |
||
20 | |||
21 | /** |
||
22 | * The metadata from the metadata object |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $metadata; |
||
27 | |||
28 | /** |
||
29 | * The metadata from the metadata object |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $relation; |
||
34 | |||
35 | |||
36 | /** |
||
37 | * Constructor |
||
38 | * |
||
39 | * @param array $metadata The property metadata or the relation metadata |
||
40 | */ |
||
41 | 55 | public function __construct(string $name, array $metadata = []) |
|
42 | { |
||
43 | 55 | $this->name = $name; |
|
44 | 55 | $this->metadata = $metadata; |
|
45 | } |
||
46 | |||
47 | /** |
||
48 | * Set the relation info of this field |
||
49 | * |
||
50 | * @param array $metadata |
||
51 | * |
||
52 | * @return void |
||
53 | */ |
||
54 | 53 | public function setRelation(array $metadata): void |
|
55 | { |
||
56 | 53 | $this->relation = $metadata; |
|
57 | } |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | 44 | public function name(): string |
|
63 | { |
||
64 | 44 | return $this->name; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | 40 | public function isObject(): bool |
|
71 | { |
||
72 | 40 | return true; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * Check whether the property is a relation |
||
77 | * |
||
78 | * @return bool |
||
79 | */ |
||
80 | 43 | public function isRelation(): bool |
|
81 | { |
||
82 | 43 | return $this->relation !== null; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | 40 | public function isArray(): bool |
|
89 | { |
||
90 | 40 | return $this->isRelation() && !$this->isSingleRelation($this->relation['type']); |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Get the relation wrapper collection |
||
95 | * |
||
96 | * @see EntityCollection |
||
97 | * |
||
98 | * @return string|callable|null The wrapper, or null if not set |
||
99 | */ |
||
100 | 36 | public function wrapper() |
|
101 | { |
||
102 | 36 | return empty($this->relation['wrapper']) ? null : $this->relation['wrapper']; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | 41 | public function isEmbedded(): bool |
|
109 | { |
||
110 | 41 | if ($this->isRelation() && !empty($this->relation['detached'])) { |
|
111 | 2 | return false; |
|
112 | } |
||
113 | |||
114 | 41 | return true; |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | 40 | public function belongsToRoot(): bool |
|
121 | { |
||
122 | 40 | return $this->isEmbedded() && $this->metadata['parentPath'] === 'root'; |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Get the class name of the property if it is a class |
||
127 | * |
||
128 | * @return null|class-string |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
129 | */ |
||
130 | 40 | public function className(): ?string |
|
131 | { |
||
132 | 40 | if (isset($this->relation['entity'])) { |
|
133 | 34 | return $this->relation['entity']; |
|
134 | } |
||
135 | |||
136 | 6 | if (isset($this->metadata['class'])) { |
|
137 | 6 | return $this->metadata['class']; |
|
138 | } |
||
139 | |||
140 | return null; |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * Get the foreign info from relation |
||
145 | * |
||
146 | * @return array{0: class-string|null, 1: string|null} |
||
0 ignored issues
–
show
|
|||
147 | */ |
||
148 | 4 | public function foreignInfos(): array |
|
149 | { |
||
150 | 4 | if ($this->relation['type'] === RelationInterface::BELONGS_TO) { |
|
151 | 3 | return [$this->relation['entity'], $this->relation['distantKey']]; |
|
152 | } |
||
153 | |||
154 | 3 | return [null, null]; |
|
155 | } |
||
156 | |||
157 | /** |
||
158 | * Get the local key of the relation |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | 3 | public function relationKey(): string |
|
163 | { |
||
164 | 3 | return $this->relation['localKey']; |
|
165 | } |
||
166 | |||
167 | /** |
||
168 | * Is the relation a one_* |
||
169 | * |
||
170 | * @param string $relationType |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | 38 | protected function isSingleRelation(string $relationType): bool |
|
175 | { |
||
176 | 38 | return in_array($relationType, [RelationInterface::HAS_ONE, RelationInterface::MORPH_TO, RelationInterface::BELONGS_TO]); |
|
177 | } |
||
178 | } |
||
179 |