Attribute.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\Model\Adapter\Container;
  7. use Magento\Catalog\Model\ResourceModel\Product\Attribute\Collection;
  8. use Magento\Catalog\Model\ResourceModel\Eav\Attribute as EavAttribute;
  9. /**
  10. * @deprecated 100.2.0
  11. * This class is used only in deprecated \Magento\Elasticsearch\Model\Adapter\DataMapper\ProductDataMapper
  12. * and must not be used for new code
  13. */
  14. class Attribute
  15. {
  16. /**
  17. * @var string[]
  18. */
  19. private $idToCodeMap = [];
  20. /**
  21. * @var Collection
  22. */
  23. private $attributeCollection;
  24. /**
  25. * @var EavAttribute[]
  26. */
  27. private $attributes = [];
  28. /**
  29. * @param Collection $attributeCollection
  30. */
  31. public function __construct(Collection $attributeCollection)
  32. {
  33. $this->attributeCollection = $attributeCollection;
  34. }
  35. /**
  36. * @param int $attributeId
  37. * @return string
  38. */
  39. public function getAttributeCodeById($attributeId)
  40. {
  41. if (!array_key_exists($attributeId, $this->idToCodeMap)) {
  42. $code = $attributeId === 'options'
  43. ? 'options'
  44. : $this->attributeCollection->getItemById($attributeId)->getAttributeCode();
  45. $this->idToCodeMap[$attributeId] = $code;
  46. }
  47. return $this->idToCodeMap[$attributeId];
  48. }
  49. /**
  50. * @param string $attributeCode
  51. * @return int
  52. */
  53. public function getAttributeIdByCode($attributeCode)
  54. {
  55. if (!array_key_exists($attributeCode, array_flip($this->idToCodeMap))) {
  56. $attributeId = $attributeCode === 'options'
  57. ? 'options'
  58. : $this->attributeCollection->getItemByColumnValue('attribute_code', $attributeCode)->getId();
  59. $this->idToCodeMap[$attributeId] = $attributeCode;
  60. }
  61. $codeToIdMap = array_flip($this->idToCodeMap);
  62. return $codeToIdMap[$attributeCode];
  63. }
  64. /**
  65. * @param string $attributeCode
  66. * @return EavAttribute|null
  67. */
  68. public function getAttribute($attributeCode)
  69. {
  70. $searchableAttributes = $this->getAttributes();
  71. return array_key_exists($attributeCode, $searchableAttributes)
  72. ? $searchableAttributes[$attributeCode]
  73. : null;
  74. }
  75. /**
  76. * @return EavAttribute[]
  77. */
  78. public function getAttributes()
  79. {
  80. if (0 === count($this->attributes)) {
  81. /** @var Collection $attributesCollection */
  82. $attributesCollection = $this->attributeCollection;
  83. foreach ($attributesCollection as $attribute) {
  84. /** @var EavAttribute $attribute */
  85. $this->attributes[$attribute->getAttributeCode()] = $attribute;
  86. }
  87. }
  88. return $this->attributes;
  89. }
  90. }