123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Search\Request;
- use Magento\Framework\ObjectManagerInterface;
- use Magento\Framework\Phrase;
- use Magento\Framework\Search\RequestInterface;
- /**
- * @api
- * @since 100.0.2
- */
- class Builder
- {
- /**
- * @var ObjectManagerInterface
- */
- private $objectManager;
- /**
- * @var Config
- */
- private $config;
- /**
- * @var Binder
- */
- private $binder;
- /**
- * @var array
- */
- private $data = [
- 'dimensions' => [],
- 'placeholder' => [],
- ];
- /**
- * @var Cleaner
- */
- private $cleaner;
- /**
- * Request Builder constructor
- *
- * @param ObjectManagerInterface $objectManager
- * @param Config $config
- * @param Binder $binder
- * @param Cleaner $cleaner
- */
- public function __construct(ObjectManagerInterface $objectManager, Config $config, Binder $binder, Cleaner $cleaner)
- {
- $this->objectManager = $objectManager;
- $this->config = $config;
- $this->binder = $binder;
- $this->cleaner = $cleaner;
- }
- /**
- * Set request name
- *
- * @param string $requestName
- * @return $this
- */
- public function setRequestName($requestName)
- {
- $this->data['requestName'] = $requestName;
- return $this;
- }
- /**
- * Set size
- *
- * @param int $size
- * @return $this
- */
- public function setSize($size)
- {
- $this->data['size'] = $size;
- return $this;
- }
- /**
- * Set from
- *
- * @param int $from
- * @return $this
- */
- public function setFrom($from)
- {
- $this->data['from'] = $from;
- return $this;
- }
- /**
- * Bind dimension data by name
- *
- * @param string $name
- * @param string $value
- * @return $this
- */
- public function bindDimension($name, $value)
- {
- $this->data['dimensions'][$name] = $value;
- return $this;
- }
- /**
- * Bind data to placeholder
- *
- * @param string $placeholder
- * @param mixed $value
- * @return $this
- */
- public function bind($placeholder, $value)
- {
- $this->data['placeholder']['$' . $placeholder . '$'] = $value;
- return $this;
- }
- /**
- * Create request object
- *
- * @return RequestInterface
- */
- public function create()
- {
- if (!isset($this->data['requestName'])) {
- throw new \InvalidArgumentException("Request name not defined.");
- }
- $requestName = $this->data['requestName'];
- /** @var array $data */
- $data = $this->config->get($requestName);
- if ($data === null) {
- throw new NonExistingRequestNameException(new Phrase("Request name '%1' doesn't exist.", [$requestName]));
- }
- $data = $this->binder->bind($data, $this->data);
- $data = $this->cleaner->clean($data);
- $this->clear();
- return $this->convert($data);
- }
- /**
- * Clear data
- *
- * @return void
- */
- private function clear()
- {
- $this->data = [
- 'dimensions' => [],
- 'placeholder' => [],
- ];
- }
- /**
- * Convert array to Request instance
- *
- * @param array $data
- * @return RequestInterface
- */
- private function convert($data)
- {
- /** @var Mapper $mapper */
- $mapper = $this->objectManager->create(
- \Magento\Framework\Search\Request\Mapper::class,
- [
- 'objectManager' => $this->objectManager,
- 'rootQueryName' => $data['query'],
- 'queries' => $data['queries'],
- 'aggregations' => $data['aggregations'],
- 'filters' => $data['filters']
- ]
- );
- return $this->objectManager->create(
- \Magento\Framework\Search\Request::class,
- [
- 'name' => $data['query'],
- 'indexName' => $data['index'],
- 'from' => $data['from'],
- 'size' => $data['size'],
- 'query' => $mapper->getRootQuery(),
- 'dimensions' => $this->buildDimensions(isset($data['dimensions']) ? $data['dimensions'] : []),
- 'buckets' => $mapper->getBuckets()
- ]
- );
- }
- /**
- * @param array $dimensionsData
- * @return array
- */
- private function buildDimensions(array $dimensionsData)
- {
- $dimensions = [];
- foreach ($dimensionsData as $dimensionData) {
- $dimensions[$dimensionData['name']] = $this->objectManager->create(
- \Magento\Framework\Search\Request\Dimension::class,
- $dimensionData
- );
- }
- return $dimensions;
- }
- }
|