Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Attribute.php
Go to the documentation of this file.
1 <?php
7 
10 
16 class Attribute
17 {
21  private $idToCodeMap = [];
22 
26  private $attributeCollection;
27 
31  private $attributes = [];
32 
36  public function __construct(Collection $attributeCollection)
37  {
38  $this->attributeCollection = $attributeCollection;
39  }
40 
45  public function getAttributeCodeById($attributeId)
46  {
47  if (!array_key_exists($attributeId, $this->idToCodeMap)) {
48  $code = $attributeId === 'options'
49  ? 'options'
50  : $this->attributeCollection->getItemById($attributeId)->getAttributeCode();
51  $this->idToCodeMap[$attributeId] = $code;
52  }
53  return $this->idToCodeMap[$attributeId];
54  }
55 
61  {
62  if (!array_key_exists($attributeCode, array_flip($this->idToCodeMap))) {
63  $attributeId = $attributeCode === 'options'
64  ? 'options'
65  : $this->attributeCollection->getItemByColumnValue('attribute_code', $attributeCode)->getId();
66  $this->idToCodeMap[$attributeId] = $attributeCode;
67  }
68  $codeToIdMap = array_flip($this->idToCodeMap);
69  return $codeToIdMap[$attributeCode];
70  }
71 
76  public function getAttribute($attributeCode)
77  {
78  $searchableAttributes = $this->getAttributes();
79  return array_key_exists($attributeCode, $searchableAttributes)
80  ? $searchableAttributes[$attributeCode]
81  : null;
82  }
83 
87  public function getAttributes()
88  {
89  if (0 === count($this->attributes)) {
91  $attributesCollection = $this->attributeCollection;
92  foreach ($attributesCollection as $attribute) {
94  $this->attributes[$attribute->getAttributeCode()] = $attribute;
95  }
96  }
97  return $this->attributes;
98  }
99 }
__construct(Collection $attributeCollection)
Definition: Attribute.php:36
$attributeCode
Definition: extend.phtml:12
$code
Definition: info.phtml:12