SearchResultFactory.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Ui\DataProvider;
  7. use Magento\Framework\Api\AttributeValue;
  8. use Magento\Framework\Api\AttributeValueFactory;
  9. use Magento\Framework\Api\Search\DocumentFactory;
  10. use Magento\Framework\Api\Search\SearchResultInterface;
  11. use Magento\Framework\Api\SearchCriteriaInterface;
  12. use Magento\Framework\Api\Search\SearchResultFactory as BaseSearchResultFactory;
  13. use Magento\Framework\EntityManager\HydratorInterface;
  14. /**
  15. * Allows to use Repositories (instead of Collections) in UI Components Data providers
  16. *
  17. * @api
  18. * @since 101.1.0
  19. */
  20. class SearchResultFactory
  21. {
  22. /**
  23. * @var HydratorInterface
  24. */
  25. private $hydrator;
  26. /**
  27. * @var DocumentFactory
  28. */
  29. private $documentFactory;
  30. /**
  31. * @var BaseSearchResultFactory
  32. */
  33. private $searchResultFactory;
  34. /**
  35. * @var AttributeValueFactory
  36. */
  37. private $attributeValueFactory;
  38. /**
  39. * @param HydratorInterface $hydrator
  40. * @param DocumentFactory $documentFactory
  41. * @param BaseSearchResultFactory $searchResultFactory
  42. * @param AttributeValueFactory $attributeValueFactory
  43. */
  44. public function __construct(
  45. HydratorInterface $hydrator,
  46. DocumentFactory $documentFactory,
  47. BaseSearchResultFactory $searchResultFactory,
  48. AttributeValueFactory $attributeValueFactory
  49. ) {
  50. $this->hydrator = $hydrator;
  51. $this->documentFactory = $documentFactory;
  52. $this->searchResultFactory = $searchResultFactory;
  53. $this->attributeValueFactory = $attributeValueFactory;
  54. }
  55. /**
  56. * @param array $items
  57. * @param int $totalCount
  58. * @param SearchCriteriaInterface SearchCriteriaInterface $searchCriteria
  59. * @param string $idFieldName
  60. * @return SearchResultInterface
  61. * @since 101.1.0
  62. */
  63. public function create(
  64. array $items,
  65. $totalCount,
  66. SearchCriteriaInterface $searchCriteria,
  67. $idFieldName
  68. ): SearchResultInterface {
  69. $documents = [];
  70. foreach ($items as $item) {
  71. $itemData = $this->hydrator->extract($item);
  72. $itemId = $itemData[$idFieldName];
  73. $attributes = $this->createAttributes($idFieldName, $itemData);
  74. $document = $this->documentFactory->create();
  75. $document->setId($itemId);
  76. $document->setCustomAttributes($attributes);
  77. $documents[] = $document;
  78. }
  79. $searchResult = $this->searchResultFactory->create();
  80. $searchResult->setItems($documents);
  81. $searchResult->setTotalCount($totalCount);
  82. $searchResult->setSearchCriteria($searchCriteria);
  83. return $searchResult;
  84. }
  85. /**
  86. * @param string $idFieldName
  87. * @param array $itemData
  88. * @return AttributeValue[]
  89. */
  90. private function createAttributes(string $idFieldName, array $itemData): array
  91. {
  92. $attributes = [];
  93. $idFieldNameAttribute = $this->attributeValueFactory->create();
  94. $idFieldNameAttribute->setAttributeCode('id_field_name');
  95. $idFieldNameAttribute->setValue($idFieldName);
  96. $attributes['id_field_name'] = $idFieldNameAttribute;
  97. foreach ($itemData as $key => $value) {
  98. $attribute = $this->attributeValueFactory->create();
  99. $attribute->setAttributeCode($key);
  100. if (is_bool($value)) {
  101. // for proper work of form and grid (for example for Yes/No properties)
  102. $value = (string)(int)$value;
  103. }
  104. $attribute->setValue($value);
  105. $attributes[$key] = $attribute;
  106. }
  107. return $attributes;
  108. }
  109. }