ResponseFactory.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Elasticsearch\SearchAdapter;
  7. use Magento\Framework\ObjectManagerInterface;
  8. /**
  9. * Response Factory
  10. * @api
  11. * @since 100.1.0
  12. */
  13. class ResponseFactory
  14. {
  15. /**
  16. * Object Manager instance
  17. *
  18. * @var ObjectManagerInterface
  19. * @since 100.1.0
  20. */
  21. protected $objectManager;
  22. /**
  23. * Document Factory
  24. *
  25. * @var DocumentFactory
  26. * @since 100.1.0
  27. */
  28. protected $documentFactory;
  29. /**
  30. * Aggregation Factory
  31. *
  32. * @var AggregationFactory
  33. * @since 100.1.0
  34. */
  35. protected $aggregationFactory;
  36. /**
  37. * @param ObjectManagerInterface $objectManager
  38. * @param DocumentFactory $documentFactory
  39. * @param AggregationFactory $aggregationFactory
  40. */
  41. public function __construct(
  42. ObjectManagerInterface $objectManager,
  43. DocumentFactory $documentFactory,
  44. AggregationFactory $aggregationFactory
  45. ) {
  46. $this->objectManager = $objectManager;
  47. $this->documentFactory = $documentFactory;
  48. $this->aggregationFactory = $aggregationFactory;
  49. }
  50. /**
  51. * Create Query Response instance
  52. *
  53. * @param array $response
  54. * @return \Magento\Framework\Search\Response\QueryResponse
  55. * @since 100.1.0
  56. */
  57. public function create($response)
  58. {
  59. $documents = [];
  60. foreach ($response['documents'] as $rawDocument) {
  61. /** @var \Magento\Framework\Api\Search\Document[] $documents */
  62. $documents[] = $this->documentFactory->create(
  63. $rawDocument
  64. );
  65. }
  66. /** @var \Magento\Framework\Search\Response\Aggregation $aggregations */
  67. $aggregations = $this->aggregationFactory->create($response['aggregations']);
  68. return $this->objectManager->create(
  69. \Magento\Framework\Search\Response\QueryResponse::class,
  70. [
  71. 'documents' => $documents,
  72. 'aggregations' => $aggregations,
  73. ]
  74. );
  75. }
  76. }