FieldContainer::getFieldAsNumberSet()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * @author @jenschude <[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://docs.commercetools.com/http-api-projects-custom-fields.html#customfields
27
 */
28
class FieldContainer extends JsonObject
29
{
30 62
    public function fieldDefinition($field)
31
    {
32 62
        if (!$this->parent instanceof CustomFieldObject) {
33 43
            return null;
34
        }
35 34
        $typeReference = $this->parent->getType();
36
37 34
        if (!$typeReference instanceof TypeReference) {
0 ignored issues
show
introduced by
$typeReference is always a sub-type of Commercetools\Core\Model\Type\TypeReference.
Loading history...
38
            return null;
39
        }
40 34
        $type = $typeReference->getObj();
41 34
        if (!$type instanceof Type) {
0 ignored issues
show
introduced by
$type is always a sub-type of Commercetools\Core\Model\Type\Type.
Loading history...
42 27
            return null;
43
        }
44 7
        $fieldDefinitions = $type->getFieldDefinitions();
45 7
        if (!$fieldDefinitions instanceof FieldDefinitionCollection) {
0 ignored issues
show
introduced by
$fieldDefinitions is always a sub-type of Commercetools\Core\Model...eldDefinitionCollection.
Loading history...
46
            return null;
47
        }
48 7
        $fieldDefinition = $fieldDefinitions->getByName($field);
49 7
        if (!$fieldDefinition instanceof FieldDefinition) {
0 ignored issues
show
introduced by
$fieldDefinition is always a sub-type of Commercetools\Core\Model\Type\FieldDefinition.
Loading history...
50
            return null;
51
        }
52 7
        $fieldType = $fieldDefinition->getType();
53 7
        if (!$fieldType instanceof FieldType) {
0 ignored issues
show
introduced by
$fieldType is always a sub-type of Commercetools\Core\Model\Type\FieldType.
Loading history...
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 19
    public function set($field, $value)
65
    {
66 19
        return parent::set($field, $value);
67
    }
68
69 35
    protected function isValidField($field)
70
    {
71 35
        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
        return $this->getFieldAsType($name, LocalizedString::class);
114
    }
115
116
    /**
117
     * @return LocalizedEnum
118
     */
119 1
    public function getFieldAsLocalizedEnum($name)
120
    {
121 1
        return $this->getFieldAsType($name, LocalizedEnum::class);
122
    }
123
124
    /**
125
     * @return Enum
126
     */
127 1
    public function getFieldAsEnum($name)
128
    {
129 1
        return $this->getFieldAsType($name, Enum::class);
130
    }
131
132
    /**
133
     * @return Money
134
     */
135 1
    public function getFieldAsMoney($name)
136
    {
137 1
        return $this->getFieldAsType($name, Money::class);
138
    }
139
140
    /**
141
     * @return DateDecorator
142
     */
143 1
    public function getFieldAsDate($name)
144
    {
145 1
        return $this->getFieldAsType($name, DateDecorator::class);
146
    }
147
148
    /**
149
     * @return TimeDecorator
150
     */
151 1
    public function getFieldAsTime($name)
152
    {
153 1
        return $this->getFieldAsType($name, TimeDecorator::class);
154
    }
155
156
    /**
157
     * @return DateTimeDecorator
158
     */
159 1
    public function getFieldAsDateTime($name)
160
    {
161 1
        return $this->getFieldAsType($name, DateTimeDecorator::class);
162
    }
163
164
    /**
165
     * @return Reference
166
     */
167 1
    public function getFieldAsReference($name)
168
    {
169 1
        return $this->getFieldAsType($name, Reference::class);
170
    }
171
172
    /**
173
     * @return Set
174
     */
175 1
    public function getFieldAsBoolSet($name)
176
    {
177 1
        return $this->getFieldAsSetType($name, 'bool');
178
    }
179
180
    /**
181
     * @return Set
182
     */
183 1
    public function getFieldAsNumberSet($name)
184
    {
185 1
        return $this->getFieldAsSetType($name, 'float');
186
    }
187
188
    /**
189
     * @return Set
190
     */
191 1
    public function getFieldAsIntegerSet($name)
192
    {
193 1
        return $this->getFieldAsSetType($name, 'int');
194
    }
195
196
    /**
197
     * @return Set
198
     */
199 1
    public function getFieldAsStringSet($name)
200
    {
201 1
        return $this->getFieldAsSetType($name, 'string');
202
    }
203
204
    /**
205
     * @return Set
206
     */
207 1
    public function getFieldAsLocalizedStringSet($name)
208
    {
209 1
        return $this->getFieldAsSetType($name, LocalizedString::class);
210
    }
211
212
    /**
213
     * @return Set
214
     */
215 1
    public function getFieldAsLocalizedEnumSet($name)
216
    {
217 1
        return $this->getFieldAsSetType($name, LocalizedEnum::class);
218
    }
219
220
    /**
221
     * @return Set
222
     */
223 1
    public function getFieldAsEnumSet($name)
224
    {
225 1
        return $this->getFieldAsSetType($name, Enum::class);
226
    }
227
228
    /**
229
     * @return Set
230
     */
231 1
    public function getFieldAsMoneySet($name)
232
    {
233 1
        return $this->getFieldAsSetType($name, Money::class);
234
    }
235
236
    /**
237
     * @return Set
238
     */
239 1
    public function getFieldAsDateSet($name)
240
    {
241 1
        return $this->getFieldAsSetType($name, DateDecorator::class);
242
    }
243
244
    /**
245
     * @return Set
246
     */
247 1
    public function getFieldAsTimeSet($name)
248
    {
249 1
        return $this->getFieldAsSetType($name, TimeDecorator::class);
250
    }
251
252
    /**
253
     * @return Set
254
     */
255 1
    public function getFieldAsDateTimeSet($name)
256
    {
257 1
        return $this->getFieldAsSetType($name, DateTimeDecorator::class);
258
    }
259
260
    /**
261
     * @return Set
262
     */
263 1
    public function getFieldAsReferenceSet($name)
264
    {
265 1
        return $this->getFieldAsSetType($name, Reference::class);
266
    }
267
268 12
    private function getFieldAsSetType($name, $type)
269
    {
270 12
        $value = $this->get($name);
271
272 12
        if ($value instanceof Set && $value->getType() === $type) {
273
            return $value;
274
        }
275 12
        return Set::ofTypeAndData($type, $value);
276
    }
277
278 8
    private function getFieldAsType($name, $type)
279
    {
280 8
        $value = $this->get($name);
281
282 8
        if ($value instanceof $type) {
283
            return $value;
284
        }
285 8
        if ($this->isDeserializableType($type)) {
286 5
            return $type::fromArray($value);
287
        } else {
288 3
            return new $type($value);
289
        }
290
    }
291
}
292