Builder.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Search\Request;
  7. use Magento\Framework\ObjectManagerInterface;
  8. use Magento\Framework\Phrase;
  9. use Magento\Framework\Search\RequestInterface;
  10. /**
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Builder
  15. {
  16. /**
  17. * @var ObjectManagerInterface
  18. */
  19. private $objectManager;
  20. /**
  21. * @var Config
  22. */
  23. private $config;
  24. /**
  25. * @var Binder
  26. */
  27. private $binder;
  28. /**
  29. * @var array
  30. */
  31. private $data = [
  32. 'dimensions' => [],
  33. 'placeholder' => [],
  34. ];
  35. /**
  36. * @var Cleaner
  37. */
  38. private $cleaner;
  39. /**
  40. * Request Builder constructor
  41. *
  42. * @param ObjectManagerInterface $objectManager
  43. * @param Config $config
  44. * @param Binder $binder
  45. * @param Cleaner $cleaner
  46. */
  47. public function __construct(ObjectManagerInterface $objectManager, Config $config, Binder $binder, Cleaner $cleaner)
  48. {
  49. $this->objectManager = $objectManager;
  50. $this->config = $config;
  51. $this->binder = $binder;
  52. $this->cleaner = $cleaner;
  53. }
  54. /**
  55. * Set request name
  56. *
  57. * @param string $requestName
  58. * @return $this
  59. */
  60. public function setRequestName($requestName)
  61. {
  62. $this->data['requestName'] = $requestName;
  63. return $this;
  64. }
  65. /**
  66. * Set size
  67. *
  68. * @param int $size
  69. * @return $this
  70. */
  71. public function setSize($size)
  72. {
  73. $this->data['size'] = $size;
  74. return $this;
  75. }
  76. /**
  77. * Set from
  78. *
  79. * @param int $from
  80. * @return $this
  81. */
  82. public function setFrom($from)
  83. {
  84. $this->data['from'] = $from;
  85. return $this;
  86. }
  87. /**
  88. * Bind dimension data by name
  89. *
  90. * @param string $name
  91. * @param string $value
  92. * @return $this
  93. */
  94. public function bindDimension($name, $value)
  95. {
  96. $this->data['dimensions'][$name] = $value;
  97. return $this;
  98. }
  99. /**
  100. * Bind data to placeholder
  101. *
  102. * @param string $placeholder
  103. * @param mixed $value
  104. * @return $this
  105. */
  106. public function bind($placeholder, $value)
  107. {
  108. $this->data['placeholder']['$' . $placeholder . '$'] = $value;
  109. return $this;
  110. }
  111. /**
  112. * Create request object
  113. *
  114. * @return RequestInterface
  115. */
  116. public function create()
  117. {
  118. if (!isset($this->data['requestName'])) {
  119. throw new \InvalidArgumentException("Request name not defined.");
  120. }
  121. $requestName = $this->data['requestName'];
  122. /** @var array $data */
  123. $data = $this->config->get($requestName);
  124. if ($data === null) {
  125. throw new NonExistingRequestNameException(new Phrase("Request name '%1' doesn't exist.", [$requestName]));
  126. }
  127. $data = $this->binder->bind($data, $this->data);
  128. $data = $this->cleaner->clean($data);
  129. $this->clear();
  130. return $this->convert($data);
  131. }
  132. /**
  133. * Clear data
  134. *
  135. * @return void
  136. */
  137. private function clear()
  138. {
  139. $this->data = [
  140. 'dimensions' => [],
  141. 'placeholder' => [],
  142. ];
  143. }
  144. /**
  145. * Convert array to Request instance
  146. *
  147. * @param array $data
  148. * @return RequestInterface
  149. */
  150. private function convert($data)
  151. {
  152. /** @var Mapper $mapper */
  153. $mapper = $this->objectManager->create(
  154. \Magento\Framework\Search\Request\Mapper::class,
  155. [
  156. 'objectManager' => $this->objectManager,
  157. 'rootQueryName' => $data['query'],
  158. 'queries' => $data['queries'],
  159. 'aggregations' => $data['aggregations'],
  160. 'filters' => $data['filters']
  161. ]
  162. );
  163. return $this->objectManager->create(
  164. \Magento\Framework\Search\Request::class,
  165. [
  166. 'name' => $data['query'],
  167. 'indexName' => $data['index'],
  168. 'from' => $data['from'],
  169. 'size' => $data['size'],
  170. 'query' => $mapper->getRootQuery(),
  171. 'dimensions' => $this->buildDimensions(isset($data['dimensions']) ? $data['dimensions'] : []),
  172. 'buckets' => $mapper->getBuckets()
  173. ]
  174. );
  175. }
  176. /**
  177. * @param array $dimensionsData
  178. * @return array
  179. */
  180. private function buildDimensions(array $dimensionsData)
  181. {
  182. $dimensions = [];
  183. foreach ($dimensionsData as $dimensionData) {
  184. $dimensions[$dimensionData['name']] = $this->objectManager->create(
  185. \Magento\Framework\Search\Request\Dimension::class,
  186. $dimensionData
  187. );
  188. }
  189. return $dimensions;
  190. }
  191. }