1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Prime\Mapper\Info; |
4
|
|
|
|
5
|
|
|
use Bdf\Prime\Mapper\Metadata; |
6
|
|
|
use Bdf\Prime\Types\PhpTypeInterface; |
7
|
|
|
use Bdf\Prime\Types\TypeInterface; |
8
|
|
|
use Bdf\Prime\Types\TypesRegistryInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* PropertyInfo |
12
|
|
|
*/ |
13
|
|
|
class PropertyInfo implements InfoInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The property name |
17
|
|
|
* |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $name; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The metadata from the metadata object |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $metadata; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The metadata from the metadata object |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $relation; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The types registry |
38
|
|
|
* |
39
|
|
|
* @var TypesRegistryInterface |
40
|
|
|
*/ |
41
|
|
|
protected $typesRegistry; |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor |
46
|
|
|
* |
47
|
|
|
* @param string $name The property name |
48
|
|
|
* @param array $metadata The property metadata or the relation metadata |
49
|
|
|
* @param TypesRegistryInterface $typesRegistry The types registry |
50
|
|
|
*/ |
51
|
71 |
|
public function __construct($name, array $metadata = [], TypesRegistryInterface $typesRegistry = null) |
|
|
|
|
52
|
|
|
{ |
53
|
71 |
|
$this->name = $name; |
54
|
71 |
|
$this->metadata = $metadata; |
55
|
71 |
|
$this->typesRegistry = $typesRegistry; |
56
|
71 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
45 |
|
public function name() |
62
|
|
|
{ |
63
|
45 |
|
return $this->name; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the property type |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
54 |
|
public function type() |
72
|
|
|
{ |
73
|
54 |
|
return $this->metadata['type']; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get the property alias |
78
|
|
|
* |
79
|
|
|
* @return string|null |
80
|
|
|
*/ |
81
|
1 |
|
public function alias() |
82
|
|
|
{ |
83
|
1 |
|
return $this->metadata['field'] !== $this->metadata['attribute'] |
84
|
1 |
|
? $this->metadata['field'] |
|
|
|
|
85
|
1 |
|
: null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the php type of the property |
90
|
|
|
* Class should have '\' char to be resolved with namespace. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
53 |
|
public function phpType() |
95
|
|
|
{ |
96
|
53 |
|
if (isset($this->metadata['phpOptions']['className'])) { |
97
|
7 |
|
return '\\'.ltrim($this->metadata['phpOptions']['className'], '\\'); |
98
|
|
|
} |
99
|
|
|
|
100
|
51 |
|
$type = $this->type(); |
101
|
|
|
|
102
|
51 |
|
if ($this->typesRegistry === null || !$this->typesRegistry->has($type)) { |
103
|
1 |
|
return $type; |
104
|
|
|
} |
105
|
|
|
|
106
|
50 |
|
return $this->typesRegistry->get($type)->phpType(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Check whether the property is a primary key |
111
|
|
|
* |
112
|
|
|
* @return bool |
|
|
|
|
113
|
|
|
*/ |
114
|
70 |
|
public function isPrimary() |
115
|
|
|
{ |
116
|
70 |
|
return $this->metadata['primary'] !== null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Check if the property value is auto-generated (auto increment or sequence) |
121
|
|
|
* |
122
|
|
|
* @return bool |
|
|
|
|
123
|
|
|
*/ |
124
|
9 |
|
public function isGenerated(): bool |
125
|
|
|
{ |
126
|
9 |
|
return in_array($this->metadata['primary'], [Metadata::PK_AUTOINCREMENT, Metadata::PK_SEQUENCE], true); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Check if the property can be null |
131
|
|
|
* |
132
|
|
|
* - Marked as nullable on mapper |
133
|
|
|
* - It's value is auto-generated |
|
|
|
|
134
|
|
|
* |
135
|
|
|
* @return bool |
|
|
|
|
136
|
|
|
*/ |
137
|
8 |
|
public function isNullable(): bool |
138
|
|
|
{ |
139
|
8 |
|
return !empty($this->metadata['nillable']) || $this->isGenerated(); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
70 |
|
public function isEmbedded() |
146
|
|
|
{ |
147
|
70 |
|
return $this->metadata['embedded'] !== null; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
70 |
|
public function belongsToRoot() |
154
|
|
|
{ |
155
|
70 |
|
return !$this->isEmbedded(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
40 |
|
public function isObject() |
162
|
|
|
{ |
163
|
40 |
|
return false; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Check whether the property is a date time object |
168
|
|
|
* |
169
|
|
|
* @return bool |
|
|
|
|
170
|
|
|
*/ |
171
|
29 |
|
public function isDateTime() |
172
|
|
|
{ |
173
|
29 |
|
return is_subclass_of($this->phpType(), \DateTimeInterface::class); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Gets the date timezone |
178
|
|
|
* |
179
|
|
|
* @return string|null |
180
|
|
|
*/ |
181
|
4 |
|
public function getTimezone(): ?string |
182
|
|
|
{ |
183
|
4 |
|
if (!$this->isDateTime()) { |
184
|
1 |
|
return null; |
185
|
|
|
} |
186
|
|
|
|
187
|
4 |
|
if (isset($this->metadata['phpOptions']['timezone'])) { |
188
|
4 |
|
return $this->metadata['phpOptions']['timezone']; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** @var \DateTimeZone $timezone */ |
|
|
|
|
192
|
4 |
|
$timezone = $this->getType()->getTimezone(); |
|
|
|
|
193
|
|
|
|
194
|
4 |
|
return $timezone ? $timezone->getName() : null; |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritdoc} |
199
|
|
|
*/ |
200
|
29 |
|
public function isArray() |
201
|
|
|
{ |
202
|
29 |
|
return $this->phpType() === PhpTypeInterface::TARRAY; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Check whether the property has a default value |
207
|
|
|
* |
208
|
|
|
* @return bool |
|
|
|
|
209
|
|
|
*/ |
210
|
29 |
|
public function hasDefault() |
211
|
|
|
{ |
212
|
29 |
|
return $this->getDefault() !== null; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Get the default value |
217
|
|
|
* |
218
|
|
|
* @return mixed |
219
|
|
|
*/ |
220
|
30 |
|
public function getDefault() |
221
|
|
|
{ |
222
|
30 |
|
return $this->metadata['default']; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Get the default value |
227
|
|
|
* |
228
|
|
|
* @param mixed $value |
229
|
|
|
* @param bool $toPhp |
|
|
|
|
230
|
|
|
* @param array $fieldOptions |
231
|
|
|
* |
232
|
|
|
* @return mixed |
233
|
|
|
*/ |
234
|
6 |
|
public function convert($value, $toPhp = true, array $fieldOptions = []) |
235
|
|
|
{ |
236
|
6 |
|
if ($toPhp) { |
237
|
5 |
|
return $this->getType()->fromDatabase($value, $fieldOptions); |
238
|
|
|
} |
239
|
|
|
|
240
|
1 |
|
return $this->getType()->toDatabase($value); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Get the type object |
245
|
|
|
* |
246
|
|
|
* @return TypeInterface |
247
|
|
|
*/ |
248
|
7 |
|
protected function getType() |
249
|
|
|
{ |
250
|
7 |
|
return $this->typesRegistry->get($this->type()); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|