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