1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AlgoWeb\PODataLaravel\Models\ObjectMap\Entities; |
6
|
|
|
|
7
|
|
|
use POData\Providers\Metadata\Type\EdmPrimitiveType; |
8
|
|
|
use POData\Providers\Metadata\Type\TypeCode; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class EntityField |
12
|
|
|
* @package AlgoWeb\PODataLaravel\Models\ObjectMap\Entities |
13
|
|
|
*/ |
14
|
|
|
class EntityField |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $name; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var EntityFieldType; |
23
|
|
|
*/ |
24
|
|
|
private $fieldType; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
private $isNullable = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var mixed |
33
|
|
|
*/ |
34
|
|
|
private $defaultValue; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
private $readOnly = false; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
private $createOnly = false; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
private $isKeyField = false; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var EntityFieldPrimitiveType |
53
|
|
|
*/ |
54
|
|
|
private $primitiveType; |
55
|
|
|
/** |
56
|
|
|
* @var EdmPrimitiveType |
57
|
|
|
*/ |
58
|
|
|
private $edmFieldType; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return EdmPrimitiveType |
62
|
|
|
*/ |
63
|
|
|
public function getEdmFieldType() |
64
|
|
|
{ |
65
|
|
|
return $this->edmFieldType; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return \AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\EntityFieldPrimitiveType |
70
|
|
|
*/ |
71
|
|
|
public function getPrimitiveType() |
72
|
|
|
{ |
73
|
|
|
return $this->primitiveType; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param \AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\EntityFieldPrimitiveType $primitiveType |
78
|
|
|
*/ |
79
|
|
|
public function setPrimitiveType(EntityFieldPrimitiveType $primitiveType): void |
80
|
|
|
{ |
81
|
|
|
$this->primitiveType = $primitiveType; |
82
|
|
|
$rawType = $this->primitiveTypeToEdmType($primitiveType); |
83
|
|
|
$this->edmFieldType = 'stream' === $rawType ? $rawType : new EdmPrimitiveType($rawType); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
|
|
public function getName() |
90
|
|
|
{ |
91
|
|
|
return $this->name; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param string $name |
96
|
|
|
*/ |
97
|
|
|
public function setName($name): void |
98
|
|
|
{ |
99
|
|
|
$this->name = $name; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return EntityFieldType |
104
|
|
|
*/ |
105
|
|
|
public function getFieldType() |
106
|
|
|
{ |
107
|
|
|
return $this->fieldType; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param EntityFieldType $fieldType |
112
|
|
|
*/ |
113
|
|
|
public function setFieldType(EntityFieldType $fieldType): void |
114
|
|
|
{ |
115
|
|
|
$this->fieldType = $fieldType; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function getIsNullable(): bool |
122
|
|
|
{ |
123
|
|
|
return $this->isNullable; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param bool $isNullable |
128
|
|
|
*/ |
129
|
|
|
public function setIsNullable($isNullable): void |
130
|
|
|
{ |
131
|
|
|
$this->isNullable = boolval($isNullable); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return mixed |
136
|
|
|
*/ |
137
|
|
|
public function getDefaultValue() |
138
|
|
|
{ |
139
|
|
|
return $this->defaultValue; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param mixed $defaultValue |
144
|
|
|
*/ |
145
|
|
|
public function setDefaultValue($defaultValue): void |
146
|
|
|
{ |
147
|
|
|
$this->defaultValue = $defaultValue; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return bool |
152
|
|
|
*/ |
153
|
|
|
public function getReadOnly() |
154
|
|
|
{ |
155
|
|
|
return $this->readOnly; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param bool $readOnly |
160
|
|
|
*/ |
161
|
|
|
public function setReadOnly($readOnly): void |
162
|
|
|
{ |
163
|
|
|
$this->readOnly = boolval($readOnly); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
|
|
public function getCreateOnly() |
170
|
|
|
{ |
171
|
|
|
return $this->createOnly; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param bool $createOnly |
176
|
|
|
*/ |
177
|
|
|
public function setCreateOnly($createOnly): void |
178
|
|
|
{ |
179
|
|
|
$this->createOnly = boolval($createOnly); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return bool |
184
|
|
|
*/ |
185
|
|
|
public function getIsKeyField() |
186
|
|
|
{ |
187
|
|
|
return $this->isKeyField; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param bool $keyField |
192
|
|
|
*/ |
193
|
|
|
public function setIsKeyField($keyField): void |
194
|
|
|
{ |
195
|
|
|
$this->isKeyField = boolval($keyField); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @var array<string, int|string> |
200
|
|
|
*/ |
201
|
|
|
private static $primitiveToEdmMapping = [ |
202
|
|
|
EntityFieldPrimitiveType::BINARY => EdmPrimitiveType::BINARY, |
203
|
|
|
EntityFieldPrimitiveType::BIGINT => EdmPrimitiveType::INT64, |
204
|
|
|
EntityFieldPrimitiveType::INTEGER => EdmPrimitiveType::INT32, |
205
|
|
|
EntityFieldPrimitiveType::STRING => EdmPrimitiveType::STRING, |
206
|
|
|
EntityFieldPrimitiveType::DATE => EdmPrimitiveType::DATETIME, |
207
|
|
|
EntityFieldPrimitiveType::DATETIME => EdmPrimitiveType::DATETIME, |
208
|
|
|
EntityFieldPrimitiveType::DATETIMETZ => EdmPrimitiveType::DATETIME, |
209
|
|
|
EntityFieldPrimitiveType::FLOAT => EdmPrimitiveType::SINGLE, |
210
|
|
|
EntityFieldPrimitiveType::DECIMAL => EdmPrimitiveType::DECIMAL, |
211
|
|
|
EntityFieldPrimitiveType::TEXT => EdmPrimitiveType::STRING, |
212
|
|
|
EntityFieldPrimitiveType::BOOLEAN => EdmPrimitiveType::BOOLEAN, |
213
|
|
|
EntityFieldPrimitiveType::BLOB => 'stream' |
214
|
|
|
]; |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param \AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\EntityFieldPrimitiveType $primitiveType |
218
|
|
|
* |
219
|
|
|
* @return int|string |
220
|
|
|
*/ |
221
|
|
|
private function primitiveTypeToEdmType(EntityFieldPrimitiveType $primitiveType) |
222
|
|
|
{ |
223
|
|
|
$value = $primitiveType->getValue(); |
224
|
|
|
|
225
|
|
|
return array_key_exists($value, self::$primitiveToEdmMapping) ? |
226
|
|
|
self::$primitiveToEdmMapping[$value] : EdmPrimitiveType::STRING; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.