commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
Loading...
Searching...
No Matches
src
Api
Models
Product
AttributeAccessor.php
1
<?php
2
3
4
namespace
Commercetools\Api\Models\Product
;
5
6
use
Commercetools\Api\Models\Common\LocalizedString
;
7
use
Commercetools\Api\Models\Common\LocalizedStringCollection
;
8
use
Commercetools\Api\Models\Common\LocalizedStringModel
;
9
use
Commercetools\Api\Models\Common\Money
;
10
use
Commercetools\Api\Models\Common\MoneyCollection
;
11
use
Commercetools\Api\Models\Common\MoneyModel
;
12
use
Commercetools\Api\Models\Common\Reference
;
13
use
Commercetools\Api\Models\Common\ReferenceModel
;
14
use
Commercetools\Api\Models\ProductType\AttributeLocalizedEnumValue
;
15
use
Commercetools\Api\Models\ProductType\AttributeLocalizedEnumValueCollection
;
16
use
Commercetools\Api\Models\ProductType\AttributeLocalizedEnumValueModel
;
17
use
Commercetools\Api\Models\ProductType\AttributePlainEnumValue
;
18
use
Commercetools\Api\Models\ProductType\AttributePlainEnumValueCollection
;
19
use
Commercetools\Api\Models\ProductType\AttributePlainEnumValueModel
;
20
use
Commercetools\Base\DateTimeImmutableCollection
;
21
use
Commercetools\Base\MapperFactory
;
22
use DateTimeImmutable;
23
use stdClass;
24
25
class
AttributeAccessor
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
68
public
function
getValueAsEnum
():
AttributePlainEnumValue
69
{
73
$value = $this->attribute->getValue();
74
return
AttributePlainEnumValueModel::of($value);
75
}
76
77
public
function
getValueAsLocalizedEnum
():
AttributeLocalizedEnumValue
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
133
public
function
getValueAsReference
():
Reference
134
{
138
$value = $this->attribute->getValue();
139
$refClass = ReferenceModel::resolveDiscriminatorClass($value);
140
return
$refClass::of($value);
141
}
142
143
public
function
getValueAsNested
():
NestedAttribute
144
{
148
$value = $this->attribute->getValue();
149
return
NestedAttribute::fromArray
($value);
150
}
151
152
public
function
getValueAsLocalizedString
():
LocalizedString
153
{
157
$value = $this->attribute->getValue();
158
return
LocalizedStringModel::of($value);
159
}
160
161
public
function
getValueAsEnumSet
():
AttributePlainEnumValueCollection
162
{
166
$value = $this->attribute->getValue();
167
return
AttributePlainEnumValueCollection::fromArray($value);
168
}
169
170
public
function
getValueAsLocalizedEnumSet
():
AttributeLocalizedEnumValueCollection
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
219
public
function
getValueAsDateTimeSet
():
DateTimeImmutableCollection
220
{
224
$value = $this->attribute->getValue();
225
return
DateTimeImmutableCollection::fromArray($value);
226
}
227
228
public
function
getValueAsTimeSet
():
DateTimeImmutableCollection
229
{
233
$value = $this->attribute->getValue();
234
return
DateTimeImmutableCollection::fromArray($value);
235
}
236
237
public
function
getValueAsDateSet
():
DateTimeImmutableCollection
238
{
242
$value = $this->attribute->getValue();
243
return
DateTimeImmutableCollection::fromArray($value);
244
}
245
246
public
function
getValueAsMoneySet
():
MoneyCollection
247
{
251
$value = $this->attribute->getValue();
252
return
MoneyCollection::fromArray($value);
253
}
254
255
public
function
getValueAsLocalizedStringSet
():
LocalizedStringCollection
256
{
260
$value = $this->attribute->getValue();
261
return
LocalizedStringCollection::fromArray($value);
262
}
263
264
public
function
getValueAsNestedSet
():
NestedAttributeCollection
265
{
269
$value = $this->attribute->getValue();
270
return
NestedAttributeCollection::fromArray
($value);
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
}
Commercetools\Api\Models\Common\LocalizedStringCollection
Definition
LocalizedStringCollection.php:22
Commercetools\Api\Models\Common\LocalizedStringModel
Definition
LocalizedStringModel.php:17
Commercetools\Api\Models\Common\MoneyCollection
Definition
MoneyCollection.php:26
Commercetools\Api\Models\Common\MoneyModel
Definition
MoneyModel.php:21
Commercetools\Api\Models\Common\ReferenceModel
Definition
ReferenceModel.php:91
Commercetools\Api\Models\Product\AttributeAccessor
Definition
AttributeAccessor.php:26
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsNested
getValueAsNested()
Definition
AttributeAccessor.php:143
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsTime
getValueAsTime()
Definition
AttributeAccessor.php:112
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsLocalizedString
getValueAsLocalizedString()
Definition
AttributeAccessor.php:152
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsFloat
getValueAsFloat()
Definition
AttributeAccessor.php:96
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsEnum
getValueAsEnum()
Definition
AttributeAccessor.php:68
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsLocalizedStringSet
getValueAsLocalizedStringSet()
Definition
AttributeAccessor.php:255
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsStringSet
getValueAsStringSet()
Definition
AttributeAccessor.php:179
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsDateTime
getValueAsDateTime()
Definition
AttributeAccessor.php:106
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsReference
getValueAsReference()
Definition
AttributeAccessor.php:133
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsFloatSet
getValueAsFloatSet()
Definition
AttributeAccessor.php:199
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsNestedSet
getValueAsNestedSet()
Definition
AttributeAccessor.php:264
Commercetools\Api\Models\Product\AttributeAccessor\of
static of()
Definition
AttributeAccessor.php:61
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsDate
getValueAsDate()
Definition
AttributeAccessor.php:118
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsString
getValueAsString()
Definition
AttributeAccessor.php:86
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsEnumSet
getValueAsEnumSet()
Definition
AttributeAccessor.php:161
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsInt
getValueAsInt()
Definition
AttributeAccessor.php:91
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsBoolSet
getValueAsBoolSet()
Definition
AttributeAccessor.php:210
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsDateSet
getValueAsDateSet()
Definition
AttributeAccessor.php:237
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsTimeSet
getValueAsTimeSet()
Definition
AttributeAccessor.php:228
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsLocalizedEnum
getValueAsLocalizedEnum()
Definition
AttributeAccessor.php:77
Commercetools\Api\Models\Product\AttributeAccessor\getName
getName()
Definition
AttributeAccessor.php:53
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsMoney
getValueAsMoney()
Definition
AttributeAccessor.php:124
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsIntSet
getValueAsIntSet()
Definition
AttributeAccessor.php:188
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsBool
getValueAsBool()
Definition
AttributeAccessor.php:101
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsDateTimeSet
getValueAsDateTimeSet()
Definition
AttributeAccessor.php:219
Commercetools\Api\Models\Product\AttributeAccessor\getValue
getValue()
Definition
AttributeAccessor.php:41
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsLocalizedEnumSet
getValueAsLocalizedEnumSet()
Definition
AttributeAccessor.php:170
Commercetools\Api\Models\Product\AttributeAccessor\getValueAsMoneySet
getValueAsMoneySet()
Definition
AttributeAccessor.php:246
Commercetools\Api\Models\Product\NestedAttributeCollection
Definition
NestedAttributeCollection.php:21
Commercetools\Api\Models\Product\NestedAttribute
Definition
NestedAttribute.php:17
Commercetools\Api\Models\ProductType\AttributeLocalizedEnumValueCollection
Definition
AttributeLocalizedEnumValueCollection.php:22
Commercetools\Api\Models\ProductType\AttributeLocalizedEnumValueModel
Definition
AttributeLocalizedEnumValueModel.php:23
Commercetools\Api\Models\ProductType\AttributePlainEnumValueCollection
Definition
AttributePlainEnumValueCollection.php:22
Commercetools\Api\Models\ProductType\AttributePlainEnumValueModel
Definition
AttributePlainEnumValueModel.php:21
Commercetools\Base\DateTimeImmutableCollection
Definition
DateTimeImmutableCollection.php:21
Commercetools\Base\MapperArraySequence\fromArray
static fromArray(array $data)
Definition
MapperArraySequence.php:56
Commercetools\Base\MapperFactory
Definition
MapperFactory.php:16
Commercetools\Api\Models\Common\LocalizedString
Definition
LocalizedString.php:14
Commercetools\Api\Models\Common\Money
Definition
Money.php:15
Commercetools\Api\Models\Common\Reference
Definition
Reference.php:50
Commercetools\Api\Models\Product\Attribute
Definition
Attribute.php:15
Commercetools\Api\Models\ProductType\AttributeLocalizedEnumValue
Definition
AttributeLocalizedEnumValue.php:16
Commercetools\Api\Models\ProductType\AttributePlainEnumValue
Definition
AttributePlainEnumValue.php:15
Commercetools\Base\CSequence\fromArray
static fromArray(array $data)
Commercetools\Api\Models\Product
Definition
AttributeAccessor.php:4
Generated by
1.9.8