![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/magento/module-search/Model/ |
<?php /** * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ namespace Magento\Search\Model; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\App\Helper\Context; use Magento\Framework\ObjectManagerInterface; use Magento\Framework\Stdlib\StringUtils as StdlibString; use Magento\Search\Helper\Data; /** * Search Query Factory * * @api * @since 100.0.2 */ class QueryFactory implements QueryFactoryInterface { /** * Query variable */ const QUERY_VAR_NAME = 'q'; /** * @var \Magento\Framework\App\RequestInterface */ private $request; /** * @var Query */ private $query; /** * @var ObjectManagerInterface */ private $objectManager; /** * @var StdlibString */ private $string; /** * @var ScopeConfigInterface */ private $scopeConfig; /** * @var Data */ private $queryHelper; /** * @param Context $context * @param ObjectManagerInterface $objectManager * @param StdlibString $string * @param Data|null $queryHelper */ public function __construct( Context $context, ObjectManagerInterface $objectManager, StdlibString $string, Data $queryHelper = null ) { $this->request = $context->getRequest(); $this->objectManager = $objectManager; $this->string = $string; $this->scopeConfig = $context->getScopeConfig(); $this->queryHelper = $queryHelper === null ? $this->objectManager->get(Data::class) : $queryHelper; } /** * @inheritdoc */ public function get() { if (!$this->query) { $maxQueryLength = $this->queryHelper->getMaxQueryLength(); $minQueryLength = $this->queryHelper->getMinQueryLength(); $rawQueryText = $this->getRawQueryText(); $preparedQueryText = $this->getPreparedQueryText($rawQueryText, $maxQueryLength); $query = $this->create()->loadByQueryText($preparedQueryText); $query->setQueryText($preparedQueryText); $query->setIsQueryTextExceeded($this->isQueryTooLong($rawQueryText, $maxQueryLength)); $query->setIsQueryTextShort($this->isQueryTooShort($rawQueryText, $minQueryLength)); $this->query = $query; } return $this->query; } /** * Create new instance * * @param array $data * @return Query */ public function create(array $data = []) { return $this->objectManager->create(Query::class, $data); } /** * Retrieve search query text * * @return string */ private function getRawQueryText() { $queryText = $this->request->getParam(self::QUERY_VAR_NAME); return ($queryText === null || is_array($queryText)) ? '' : $this->string->cleanString(trim($queryText)); } /** * Prepare query text * * @param string $queryText * @param int|string $maxQueryLength * @return string */ private function getPreparedQueryText($queryText, $maxQueryLength) { if ($this->isQueryTooLong($queryText, $maxQueryLength)) { $queryText = $this->string->substr($queryText, 0, $maxQueryLength); } return $queryText; } /** * Check if the provided text exceeds the provided length * * @param string $queryText * @param int|string $maxQueryLength * @return bool */ private function isQueryTooLong($queryText, $maxQueryLength) { return ($maxQueryLength !== '' && $this->string->strlen($queryText) > $maxQueryLength); } /** * Check if the provided text is shorter than the provided length * * @param string $queryText * @param int|string $minQueryLength * @return bool */ private function isQueryTooShort($queryText, $minQueryLength) { return ($this->string->strlen($queryText) < $minQueryLength); } }