1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author @jenschude <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Commercetools\Core\Model\Common; |
7
|
|
|
|
8
|
|
|
use Commercetools\Core\Model\ProductType\AttributeDefinitionCollection; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @package Commercetools\Core\Model\Common |
12
|
|
|
* @link https://docs.commercetools.com/http-api-projects-products.html#attribute |
13
|
|
|
* @method Attribute current() |
14
|
|
|
* @method AttributeCollection add(Attribute $element) |
15
|
|
|
*/ |
16
|
|
|
class AttributeCollection extends Collection |
17
|
|
|
{ |
18
|
|
|
const NAME = 'name'; |
19
|
|
|
|
20
|
|
|
protected $type = Attribute::class; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var AttributeDefinitionCollection |
24
|
|
|
*/ |
25
|
|
|
protected $attributeDefinitions; |
26
|
|
|
|
27
|
22 |
|
protected function indexRow($offset, $row) |
28
|
|
|
{ |
29
|
22 |
|
if ($row instanceof Attribute) { |
30
|
15 |
|
$name = $row->getName(); |
31
|
|
|
} else { |
32
|
9 |
|
$name = $row[static::NAME]; |
33
|
|
|
} |
34
|
22 |
|
$this->addToIndex(static::NAME, $offset, $name); |
35
|
22 |
|
} |
36
|
|
|
|
37
|
2 |
|
public function __get($attributeName) |
38
|
|
|
{ |
39
|
2 |
|
$attribute = $this->getByName($attributeName); |
40
|
2 |
|
if (!is_null($attribute)) { |
41
|
1 |
|
return $attribute->getValue(); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
return null; |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
public function __call($name, $arguments) |
48
|
|
|
{ |
49
|
1 |
|
if (strpos($name, 'get') === 0) { |
50
|
1 |
|
$name = lcfirst(substr($name, 3)); |
51
|
|
|
} |
52
|
1 |
|
return $this->getByName($name); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
2 |
|
public function offsetGet($offset) |
57
|
|
|
{ |
58
|
2 |
|
if (is_string($offset)) { |
59
|
1 |
|
return $this->getByName($offset); |
60
|
|
|
} |
61
|
1 |
|
return $this->getAt($offset); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $attributeName |
66
|
|
|
* @return Attribute|null |
67
|
|
|
*/ |
68
|
12 |
|
public function getByName($attributeName) |
69
|
|
|
{ |
70
|
12 |
|
return $this->getBy(static::NAME, $attributeName); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param AttributeDefinitionCollection $attributeDefinitions |
75
|
|
|
* @return $this |
76
|
|
|
*/ |
77
|
4 |
|
public function setAttributeDefinitions(AttributeDefinitionCollection $attributeDefinitions) |
78
|
|
|
{ |
79
|
4 |
|
$this->attributeDefinitions = $attributeDefinitions; |
80
|
|
|
|
81
|
4 |
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param $offset |
86
|
|
|
* @return Attribute |
87
|
|
|
*/ |
88
|
12 |
|
public function getAt($offset) |
89
|
|
|
{ |
90
|
|
|
/** |
91
|
|
|
* @var Attribute $attribute; |
92
|
|
|
*/ |
93
|
12 |
|
$attribute = parent::getAt($offset); |
94
|
12 |
|
if (!is_null($this->attributeDefinitions)) { |
95
|
4 |
|
$definition = $this->attributeDefinitions->getByName($attribute->getName()); |
96
|
4 |
|
if (!is_null($definition)) { |
97
|
4 |
|
$attribute->setAttributeDefinition($definition); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
12 |
|
return $attribute; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|