Document.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\View\Element\UiComponent\DataProvider;
  7. use Magento\Framework\Api\Search\DocumentInterface;
  8. use Magento\Framework\DataObject;
  9. use Magento\Framework\Api\AttributeValueFactory;
  10. /**
  11. * Class Document
  12. */
  13. class Document extends DataObject implements DocumentInterface
  14. {
  15. /**
  16. * @var string|int
  17. */
  18. protected $id;
  19. /**
  20. * @var AttributeValueFactory
  21. */
  22. protected $attributeValueFactory;
  23. /**
  24. * @param AttributeValueFactory $attributeValueFactory
  25. */
  26. public function __construct(AttributeValueFactory $attributeValueFactory)
  27. {
  28. $this->attributeValueFactory = $attributeValueFactory;
  29. }
  30. /**
  31. * @return int|string
  32. */
  33. public function getId()
  34. {
  35. if (!$this->id) {
  36. $this->id = $this->getData($this->getIdFieldName());
  37. }
  38. return $this->id;
  39. }
  40. /**
  41. * @param int $id
  42. * @return void
  43. */
  44. public function setId($id)
  45. {
  46. $this->id = $id;
  47. }
  48. /**
  49. * Get an attribute value.
  50. *
  51. * @param string $attributeCode
  52. * @return \Magento\Framework\Api\AttributeInterface|null
  53. */
  54. public function getCustomAttribute($attributeCode)
  55. {
  56. /** @var \Magento\Framework\Api\AttributeInterface $attributeValue */
  57. $attributeValue = $this->attributeValueFactory->create();
  58. $attributeValue->setAttributeCode($attributeCode);
  59. $attributeValue->setValue($this->getData($attributeCode));
  60. return $attributeValue;
  61. }
  62. /**
  63. * Set an attribute value for a given attribute code
  64. *
  65. * @param string $attributeCode
  66. * @param mixed $attributeValue
  67. * @return $this
  68. */
  69. public function setCustomAttribute($attributeCode, $attributeValue)
  70. {
  71. $this->setData($attributeCode, $attributeValue);
  72. return $this;
  73. }
  74. /**
  75. * Retrieve custom attributes values.
  76. *
  77. * @return \Magento\Framework\Api\AttributeInterface[]|null
  78. */
  79. public function getCustomAttributes()
  80. {
  81. $output = [];
  82. foreach ($this->getData() as $key => $value) {
  83. $attribute = $this->attributeValueFactory->create();
  84. $output[] = $attribute->setAttributeCode($key)->setValue($value);
  85. }
  86. return $output;
  87. }
  88. /**
  89. * Set array of custom attributes
  90. *
  91. * @param \Magento\Framework\Api\AttributeInterface[] $attributes
  92. * @return $this
  93. * @throws \LogicException
  94. */
  95. public function setCustomAttributes(array $attributes)
  96. {
  97. /** @var \Magento\Framework\Api\AttributeInterface $attribute */
  98. foreach ($attributes as $attribute) {
  99. $this->setData(
  100. $attribute->getAttributeCode(),
  101. $attribute->getValue()
  102. );
  103. }
  104. return $this;
  105. }
  106. }