CustomAttributesMapper.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model;
  7. use Magento\Eav\Api\AttributeRepositoryInterface;
  8. use Magento\Framework\Api\AttributeInterface;
  9. use Magento\Framework\Api\CustomAttributesDataInterface;
  10. use Magento\Framework\Api\SearchCriteriaBuilder;
  11. use Magento\Framework\EntityManager\MapperInterface;
  12. use Magento\Framework\EntityManager\MetadataPool;
  13. /**
  14. * Class CustomAttributesMapper
  15. */
  16. class CustomAttributesMapper implements MapperInterface
  17. {
  18. /**
  19. * @var AttributeRepositoryInterface
  20. */
  21. private $attributeRepository;
  22. /**
  23. * @var MetadataPool
  24. */
  25. private $metadataPool;
  26. /**
  27. * @var SearchCriteriaBuilder
  28. */
  29. private $searchCriteriaBuilder;
  30. /**
  31. * @var array
  32. */
  33. private $attributes;
  34. /**
  35. * @param AttributeRepositoryInterface $attributeRepository
  36. * @param MetadataPool $metadataPool
  37. * @param SearchCriteriaBuilder $searchCriteriaBuilder
  38. */
  39. public function __construct(
  40. AttributeRepositoryInterface $attributeRepository,
  41. MetadataPool $metadataPool,
  42. SearchCriteriaBuilder $searchCriteriaBuilder
  43. ) {
  44. $this->attributeRepository = $attributeRepository;
  45. $this->metadataPool = $metadataPool;
  46. $this->searchCriteriaBuilder = $searchCriteriaBuilder;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function entityToDatabase($entityType, $data)
  52. {
  53. if (!$this->metadataPool->hasConfiguration($entityType)
  54. || !$this->metadataPool->getMetadata($entityType)->getEavEntityType()
  55. ) {
  56. return $data;
  57. }
  58. if (isset($data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])) {
  59. foreach ($this->getNonStaticAttributes($entityType) as $attribute) {
  60. foreach ($data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES] as $key => $customAttribute) {
  61. if ($customAttribute[AttributeInterface::ATTRIBUTE_CODE] == $attribute->getAttributeCode()) {
  62. unset($data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES][$key]);
  63. $data[$attribute->getAttributeCode()] = $customAttribute[AttributeInterface::VALUE];
  64. }
  65. }
  66. }
  67. }
  68. return $data;
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function databaseToEntity($entityType, $data)
  74. {
  75. $metadata = $this->metadataPool->getMetadata($entityType);
  76. if (!$metadata->getEavEntityType()) {
  77. return $data;
  78. }
  79. foreach ($this->getNonStaticAttributes($entityType) as $attribute) {
  80. if (isset($data[$attribute->getAttributeCode()])) {
  81. $data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES][] = [
  82. AttributeInterface::ATTRIBUTE_CODE => $attribute->getAttributeCode(),
  83. AttributeInterface::VALUE => $data[$attribute->getAttributeCode()]
  84. ];
  85. }
  86. }
  87. return $data;
  88. }
  89. /**
  90. * Get custom attributes
  91. *
  92. * @param string $entityType
  93. * @return \Magento\Eav\Api\Data\AttributeInterface[]
  94. * @throws \Exception
  95. */
  96. private function getNonStaticAttributes($entityType)
  97. {
  98. if (!isset($this->attributes[$entityType])) {
  99. $metadata = $this->metadataPool->getMetadata($entityType);
  100. $searchResult = $this->attributeRepository->getList(
  101. $metadata->getEavEntityType(),
  102. $this->searchCriteriaBuilder->addFilter('attribute_set_id', null, 'neq')->create()
  103. );
  104. $attributes = [];
  105. foreach ($searchResult->getItems() as $attribute) {
  106. if (!$attribute->isStatic()) {
  107. $attributes[] = $attribute;
  108. }
  109. }
  110. $this->attributes[$entityType] = $attributes;
  111. }
  112. return $this->attributes[$entityType];
  113. }
  114. }