1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author @jayS-de <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Commercetools\Core\Model\CustomField; |
7
|
|
|
|
8
|
|
|
use Commercetools\Core\Model\Common\DateDecorator; |
9
|
|
|
use Commercetools\Core\Model\Common\DateTimeDecorator; |
10
|
|
|
use Commercetools\Core\Model\Common\Enum; |
11
|
|
|
use Commercetools\Core\Model\Common\JsonObject; |
12
|
|
|
use Commercetools\Core\Model\Common\LocalizedEnum; |
13
|
|
|
use Commercetools\Core\Model\Common\LocalizedString; |
14
|
|
|
use Commercetools\Core\Model\Common\Money; |
15
|
|
|
use Commercetools\Core\Model\Common\Reference; |
16
|
|
|
use Commercetools\Core\Model\Common\Set; |
17
|
|
|
use Commercetools\Core\Model\Common\TimeDecorator; |
18
|
|
|
use Commercetools\Core\Model\Type\FieldDefinition; |
19
|
|
|
use Commercetools\Core\Model\Type\FieldDefinitionCollection; |
20
|
|
|
use Commercetools\Core\Model\Type\FieldType; |
21
|
|
|
use Commercetools\Core\Model\Type\Type; |
22
|
|
|
use Commercetools\Core\Model\Type\TypeReference; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @package Commercetools\Core\Model\CustomField |
26
|
|
|
* @link https://dev.commercetools.com/http-api-projects-custom-fields.html#customfields |
27
|
|
|
*/ |
28
|
|
|
class FieldContainer extends JsonObject |
29
|
|
|
{ |
30
|
47 |
|
public function fieldDefinition($field) |
31
|
|
|
{ |
32
|
47 |
|
if (!$this->parent instanceof CustomFieldObject) { |
33
|
30 |
|
return null; |
34
|
|
|
} |
35
|
19 |
|
$typeReference = $this->parent->getType(); |
36
|
|
|
|
37
|
19 |
|
if (!$typeReference instanceof TypeReference) { |
38
|
|
|
return null; |
39
|
|
|
} |
40
|
19 |
|
$type = $typeReference->getObj(); |
41
|
19 |
|
if (!$type instanceof Type) { |
42
|
12 |
|
return null; |
43
|
|
|
} |
44
|
7 |
|
$fieldDefinitions = $type->getFieldDefinitions(); |
45
|
7 |
|
if (!$fieldDefinitions instanceof FieldDefinitionCollection) { |
46
|
|
|
return null; |
47
|
|
|
} |
48
|
7 |
|
$fieldDefinition = $fieldDefinitions->getByName($field); |
49
|
7 |
|
if (!$fieldDefinition instanceof FieldDefinition) { |
50
|
|
|
return null; |
51
|
|
|
} |
52
|
7 |
|
$fieldType = $fieldDefinition->getType(); |
53
|
7 |
|
if (!$fieldType instanceof FieldType) { |
54
|
|
|
return null; |
55
|
|
|
} |
56
|
7 |
|
return $fieldType->fieldTypeDefinition(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $field |
61
|
|
|
* @param mixed $value |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
6 |
|
public function set($field, $value) |
65
|
|
|
{ |
66
|
6 |
|
return parent::set($field, $value); |
67
|
|
|
} |
68
|
|
|
|
69
|
20 |
|
protected function isValidField($field) |
70
|
|
|
{ |
71
|
20 |
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
1 |
|
public function getFieldAsBool($name) |
78
|
|
|
{ |
79
|
1 |
|
return (bool)$this->get($name); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return float |
84
|
|
|
*/ |
85
|
1 |
|
public function getFieldAsNumber($name) |
86
|
|
|
{ |
87
|
1 |
|
$value = $this->get($name); |
88
|
1 |
|
return (float)$value; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return int |
93
|
|
|
*/ |
94
|
1 |
|
public function getFieldAsInteger($name) |
95
|
|
|
{ |
96
|
1 |
|
$value = $this->get($name); |
97
|
1 |
|
return (int)$value; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
1 |
|
public function getFieldAsString($name) |
104
|
|
|
{ |
105
|
1 |
|
return (string)$this->get($name); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return LocalizedString |
110
|
|
|
*/ |
111
|
1 |
|
public function getFieldAsLocalizedString($name) |
112
|
|
|
{ |
113
|
1 |
|
$value = $this->get($name); |
114
|
|
|
|
115
|
1 |
|
if ($value instanceof LocalizedString) { |
116
|
|
|
return $value; |
117
|
|
|
} |
118
|
1 |
|
return LocalizedString::fromArray($value); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return LocalizedEnum |
123
|
|
|
*/ |
124
|
1 |
|
public function getFieldAsLocalizedEnum($name) |
125
|
|
|
{ |
126
|
1 |
|
$value = $this->get($name); |
127
|
|
|
|
128
|
1 |
|
if ($value instanceof LocalizedEnum) { |
129
|
|
|
return $value; |
130
|
|
|
} |
131
|
1 |
|
return LocalizedEnum::fromArray($value); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return Enum |
136
|
|
|
*/ |
137
|
1 |
|
public function getFieldAsEnum($name) |
138
|
|
|
{ |
139
|
1 |
|
$value = $this->get($name); |
140
|
|
|
|
141
|
1 |
|
if ($value instanceof Enum) { |
142
|
|
|
return $value; |
143
|
|
|
} |
144
|
1 |
|
return Enum::fromArray($value); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return Money |
149
|
|
|
*/ |
150
|
1 |
|
public function getFieldAsMoney($name) |
151
|
|
|
{ |
152
|
1 |
|
$value = $this->get($name); |
153
|
|
|
|
154
|
1 |
|
if ($value instanceof Money) { |
155
|
|
|
return $value; |
156
|
|
|
} |
157
|
1 |
|
return Money::fromArray($value); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return DateDecorator |
162
|
|
|
*/ |
163
|
1 |
|
public function getFieldAsDate($name) |
164
|
|
|
{ |
165
|
1 |
|
$value = $this->get($name); |
166
|
|
|
|
167
|
1 |
|
if ($value instanceof DateDecorator) { |
168
|
|
|
return $value; |
169
|
|
|
} |
170
|
1 |
|
return new DateDecorator($value); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return TimeDecorator |
175
|
|
|
*/ |
176
|
1 |
|
public function getFieldAsTime($name) |
177
|
|
|
{ |
178
|
1 |
|
$value = $this->get($name); |
179
|
|
|
|
180
|
1 |
|
if ($value instanceof TimeDecorator) { |
181
|
|
|
return $value; |
182
|
|
|
} |
183
|
1 |
|
return new TimeDecorator($value); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return DateTimeDecorator |
188
|
|
|
*/ |
189
|
1 |
|
public function getFieldAsDateTime($name) |
190
|
|
|
{ |
191
|
1 |
|
$value = $this->get($name); |
192
|
|
|
|
193
|
1 |
|
if ($value instanceof DateTimeDecorator) { |
194
|
|
|
return $value; |
195
|
|
|
} |
196
|
1 |
|
return new DateTimeDecorator($value); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return Reference |
201
|
|
|
*/ |
202
|
1 |
|
public function getFieldAsReference($name) |
203
|
|
|
{ |
204
|
1 |
|
$value = $this->get($name); |
205
|
|
|
|
206
|
1 |
|
if ($value instanceof Reference) { |
207
|
|
|
return $value; |
208
|
|
|
} |
209
|
1 |
|
return Reference::fromArray($value); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return Set |
214
|
|
|
*/ |
215
|
1 |
View Code Duplication |
public function getFieldAsBoolSet($name) |
|
|
|
|
216
|
|
|
{ |
217
|
1 |
|
$value = $this->get($name); |
218
|
|
|
|
219
|
1 |
|
if ($value instanceof Set && $value->getType() == 'bool') { |
220
|
|
|
return $value; |
221
|
|
|
} |
222
|
1 |
|
return Set::ofTypeAndData('bool', $value); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @return Set |
227
|
|
|
*/ |
228
|
1 |
View Code Duplication |
public function getFieldAsNumberSet($name) |
|
|
|
|
229
|
|
|
{ |
230
|
1 |
|
$value = $this->get($name); |
231
|
|
|
|
232
|
1 |
|
if ($value instanceof Set && $value->getType() == 'float') { |
233
|
|
|
return $value; |
234
|
|
|
} |
235
|
1 |
|
return Set::ofTypeAndData('float', $value); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @return Set |
240
|
|
|
*/ |
241
|
1 |
View Code Duplication |
public function getFieldAsIntegerSet($name) |
|
|
|
|
242
|
|
|
{ |
243
|
1 |
|
$value = $this->get($name); |
244
|
|
|
|
245
|
1 |
|
if ($value instanceof Set && $value->getType() == 'int') { |
246
|
|
|
return $value; |
247
|
|
|
} |
248
|
1 |
|
return Set::ofTypeAndData('int', $value); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @return Set |
253
|
|
|
*/ |
254
|
1 |
View Code Duplication |
public function getFieldAsStringSet($name) |
|
|
|
|
255
|
|
|
{ |
256
|
1 |
|
$value = $this->get($name); |
257
|
|
|
|
258
|
1 |
|
if ($value instanceof Set && $value->getType() == 'string') { |
259
|
|
|
return $value; |
260
|
|
|
} |
261
|
1 |
|
return Set::ofTypeAndData('string', $value); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @return Set |
266
|
|
|
*/ |
267
|
1 |
View Code Duplication |
public function getFieldAsLocalizedStringSet($name) |
|
|
|
|
268
|
|
|
{ |
269
|
1 |
|
$value = $this->get($name); |
270
|
|
|
|
271
|
1 |
|
if ($value instanceof Set && $value->getType() == LocalizedString::class) { |
272
|
|
|
return $value; |
273
|
|
|
} |
274
|
1 |
|
return Set::ofTypeAndData(LocalizedString::class, $value); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @return Set |
279
|
|
|
*/ |
280
|
1 |
View Code Duplication |
public function getFieldAsLocalizedEnumSet($name) |
|
|
|
|
281
|
|
|
{ |
282
|
1 |
|
$value = $this->get($name); |
283
|
|
|
|
284
|
1 |
|
if ($value instanceof Set && $value->getType() == LocalizedEnum::class) { |
285
|
|
|
return $value; |
286
|
|
|
} |
287
|
1 |
|
return Set::ofTypeAndData(LocalizedEnum::class, $value); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @return Set |
292
|
|
|
*/ |
293
|
1 |
View Code Duplication |
public function getFieldAsEnumSet($name) |
|
|
|
|
294
|
|
|
{ |
295
|
1 |
|
$value = $this->get($name); |
296
|
|
|
|
297
|
1 |
|
if ($value instanceof Set && $value->getType() == Enum::class) { |
298
|
|
|
return $value; |
299
|
|
|
} |
300
|
1 |
|
return Set::ofTypeAndData(Enum::class, $value); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @return Set |
305
|
|
|
*/ |
306
|
1 |
View Code Duplication |
public function getFieldAsMoneySet($name) |
|
|
|
|
307
|
|
|
{ |
308
|
1 |
|
$value = $this->get($name); |
309
|
|
|
|
310
|
1 |
|
if ($value instanceof Set && $value->getType() == Money::class) { |
311
|
|
|
return $value; |
312
|
|
|
} |
313
|
1 |
|
return Set::ofTypeAndData(Money::class, $value); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @return Set |
318
|
|
|
*/ |
319
|
1 |
View Code Duplication |
public function getFieldAsDateSet($name) |
|
|
|
|
320
|
|
|
{ |
321
|
1 |
|
$value = $this->get($name); |
322
|
|
|
|
323
|
1 |
|
if ($value instanceof Set && $value->getType() == DateDecorator::class) { |
324
|
|
|
return $value; |
325
|
|
|
} |
326
|
1 |
|
return Set::ofTypeAndData(DateDecorator::class, $value); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @return Set |
331
|
|
|
*/ |
332
|
1 |
View Code Duplication |
public function getFieldAsTimeSet($name) |
|
|
|
|
333
|
|
|
{ |
334
|
1 |
|
$value = $this->get($name); |
335
|
|
|
|
336
|
1 |
|
if ($value instanceof Set && $value->getType() == TimeDecorator::class) { |
337
|
|
|
return $value; |
338
|
|
|
} |
339
|
1 |
|
return Set::ofTypeAndData(TimeDecorator::class, $value); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @return Set |
344
|
|
|
*/ |
345
|
1 |
View Code Duplication |
public function getFieldAsDateTimeSet($name) |
|
|
|
|
346
|
|
|
{ |
347
|
1 |
|
$value = $this->get($name); |
348
|
|
|
|
349
|
1 |
|
if ($value instanceof Set && $value->getType() == DateTimeDecorator::class) { |
350
|
|
|
return $value; |
351
|
|
|
} |
352
|
1 |
|
return Set::ofTypeAndData(DateTimeDecorator::class, $value); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @return Set |
357
|
|
|
*/ |
358
|
1 |
View Code Duplication |
public function getFieldAsReferenceSet($name) |
|
|
|
|
359
|
|
|
{ |
360
|
1 |
|
$value = $this->get($name); |
361
|
|
|
|
362
|
1 |
|
if ($value instanceof Set && $value->getType() == Reference::class) { |
363
|
|
|
return $value; |
364
|
|
|
} |
365
|
1 |
|
return Set::ofTypeAndData(Reference::class, $value); |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.