commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
Loading...
Searching...
No Matches
AttributeAccessor.php
1<?php
2
3
5
22use DateTimeImmutable;
23use stdClass;
24
26{
30 private $attribute;
31
32 private function __construct(Attribute $attribute)
33 {
34 $this->attribute = $attribute;
35 }
36
41 public function getValue()
42 {
46 $value = $this->attribute->getValue();
47 return $value;
48 }
49
53 public function getName()
54 {
55 return $this->attribute->getName();
56 }
57
61 public static function of()
62 {
63 return function (Attribute $attribute): AttributeAccessor {
64 return new self($attribute);
65 };
66 }
67
69 {
73 $value = $this->attribute->getValue();
74 return AttributePlainEnumValueModel::of($value);
75 }
76
78 {
82 $value = $this->attribute->getValue();
83 return AttributeLocalizedEnumValueModel::of($value);
84 }
85
86 public function getValueAsString(): string
87 {
88 return (string)$this->attribute->getValue();
89 }
90
91 public function getValueAsInt(): int
92 {
93 return (int)$this->attribute->getValue();
94 }
95
96 public function getValueAsFloat(): float
97 {
98 return (float)$this->attribute->getValue();
99 }
100
101 public function getValueAsBool(): bool
102 {
103 return (bool)$this->attribute->getValue();
104 }
105
106 public function getValueAsDateTime(): ?DateTimeImmutable
107 {
108 $value = DateTimeImmutable::createFromFormat(MapperFactory::DATETIME_FORMAT, (string)$this->attribute->getValue());
109 return $value ? $value : null;
110 }
111
112 public function getValueAsTime(): ?DateTimeImmutable
113 {
114 $value = DateTimeImmutable::createFromFormat(MapperFactory::TIME_FORMAT, (string)$this->attribute->getValue());
115 return $value ? $value : null;
116 }
117
118 public function getValueAsDate(): ?DateTimeImmutable
119 {
120 $value = DateTimeImmutable::createFromFormat(MapperFactory::DATE_FORMAT, (string)$this->attribute->getValue());
121 return $value ? $value : null;
122 }
123
124 public function getValueAsMoney(): Money
125 {
129 $value = $this->attribute->getValue();
130 return MoneyModel::of($value);
131 }
132
134 {
138 $value = $this->attribute->getValue();
139 $refClass = ReferenceModel::resolveDiscriminatorClass($value);
140 return $refClass::of($value);
141 }
142
144 {
148 $value = $this->attribute->getValue();
149 return NestedAttribute::fromArray($value);
150 }
151
153 {
157 $value = $this->attribute->getValue();
158 return LocalizedStringModel::of($value);
159 }
160
162 {
166 $value = $this->attribute->getValue();
167 return AttributePlainEnumValueCollection::fromArray($value);
168 }
169
171 {
175 $value = $this->attribute->getValue();
176 return AttributeLocalizedEnumValueCollection::fromArray($value);
177 }
178
179 public function getValueAsStringSet(): array
180 {
184 $value = $this->attribute->getValue();
185 return $value;
186 }
187
188 public function getValueAsIntSet(): array
189 {
193 $value = $this->attribute->getValue();
194 return array_map(function ($value) {
195 return (int)$value;
196 }, $value);
197 }
198
199 public function getValueAsFloatSet(): array
200 {
204 $value = $this->attribute->getValue();
205 return array_map(function ($value) {
206 return (float)$value;
207 }, $value);
208 }
209
210 public function getValueAsBoolSet(): array
211 {
215 $value = $this->attribute->getValue();
216 return $value;
217 }
218
220 {
224 $value = $this->attribute->getValue();
225 return DateTimeImmutableCollection::fromArray($value);
226 }
227
229 {
233 $value = $this->attribute->getValue();
234 return DateTimeImmutableCollection::fromArray($value);
235 }
236
238 {
242 $value = $this->attribute->getValue();
243 return DateTimeImmutableCollection::fromArray($value);
244 }
245
247 {
251 $value = $this->attribute->getValue();
252 return MoneyCollection::fromArray($value);
253 }
254
256 {
260 $value = $this->attribute->getValue();
261 return LocalizedStringCollection::fromArray($value);
262 }
263
265 {
269 $value = $this->attribute->getValue();
271 }
272
273 // public function getTypedValue()
274 // {
275 // $value = $this->attribute->getValue();
276 // if (is_scalar($value)) {
277 // return $value;
278 // }
279 // if ($value instanceof stdClass) {
280 // return $this->objectToValue($value);
281 // }
282 // if (is_array($value)) {
283 // return $this->arrayToValue($value);
284 // }
285 // return $value;
286 // }
287 //
288 // private function objectToValue(stdClass $value)
289 // {
290 // if (isset($value->key)) {
291 // if ($value->label instanceof stdClass) {
292 // return AttributeLocalizedEnumValueModel::fromStdClass($value);
293 // }
294 // return AttributePlainEnumValueModel::fromStdClass($value);
295 // }
296 // if (isset($value->currencyCode)) {
297 // return MoneyModel::fromStdClass($value);
298 // }
299 // if (isset($value->typeId)) {
300 // $refClass = ReferenceModel::resolveDiscriminatorClass($value);
301 // return $refClass::fromStdClass($value);
302 // }
303 // return LocalizedStringModel::fromStdClass($value);
304 // }
305 //
306 // private function arrayToValue(array $value)
307 // {
308 // $firstItem = current($value);
309 // if (is_array($firstItem)) {
310 // return $this->objectToValueCollection($firstItem, $value);
311 // }
312 // if (isset($value['key'])) {
313 // if ($value['label'] instanceof stdClass) {
314 // return AttributeLocalizedEnumValueModel::fromArray($value);
315 // }
316 // return AttributePlainEnumValueModel::fromArray($value);
317 // }
318 // if (isset($value['currencyCode'])) {
319 // return MoneyModel::fromArray($value);
320 // }
321 // if (isset($value['typeId'])) {
322 // $refClass = ReferenceModel::resolveDiscriminatorClass($value);
323 // return $refClass::fromArray($value);
324 // }
325 // if (is_string(key($value))) {
326 // return LocalizedStringModel::fromArray($value);
327 // }
328 // return $value;
329 // }
330 //
331 // private function objectToValueCollection($firstItem, array $value)
332 // {
333 // if (isset($firstItem->key)) {
334 // if ($firstItem->label instanceof stdClass) {
335 // return AttributeLocalizedEnumValueCollection::fromArray($value);
336 // }
337 // return AttributePlainEnumValueCollection::fromArray($value);
338 // }
339 // if (isset($firstItem->currencyCode)) {
340 // return MoneyCollection::fromArray($value);
341 // }
342 // if (isset($firstItem->typeId)) {
343 // $refClass = ReferenceModel::resolveDiscriminatorClass($value) . "Collection";
344 // return $refClass::fromArray($value);
345 // }
346 // return LocalizedStringCollection::fromArray($value);
347 // }
348}
static fromArray(array $data)