SearchResponseBuilder.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Search;
  7. use Magento\Framework\Api\Search\SearchResultInterface;
  8. use Magento\Framework\Api\Search\DocumentFactory;
  9. use Magento\Framework\Api\Search\SearchResultFactory;
  10. class SearchResponseBuilder
  11. {
  12. /**
  13. * @var DocumentFactory
  14. * @deprecated 100.1.0
  15. */
  16. private $documentFactory;
  17. /**
  18. * @var SearchResultFactory
  19. */
  20. private $searchResultFactory;
  21. /**
  22. * @param SearchResultFactory $searchResultFactory
  23. * @param DocumentFactory $documentFactory
  24. */
  25. public function __construct(
  26. SearchResultFactory $searchResultFactory,
  27. DocumentFactory $documentFactory
  28. ) {
  29. $this->documentFactory = $documentFactory;
  30. $this->searchResultFactory = $searchResultFactory;
  31. }
  32. /**
  33. * @param ResponseInterface $response
  34. * @return SearchResultInterface
  35. */
  36. public function build(ResponseInterface $response)
  37. {
  38. /** @var \Magento\Framework\Api\Search\SearchResult $searchResult */
  39. $searchResult = $this->searchResultFactory->create();
  40. $documents = iterator_to_array($response);
  41. $searchResult->setItems($documents);
  42. $searchResult->setAggregations($response->getAggregations());
  43. $searchResult->setTotalCount(count($documents));
  44. return $searchResult;
  45. }
  46. }