mirror of
https://github.com/bdnugget/magento2.git
synced 2026-11-07 09:17:35 +00:00
- Fix the matching query text is used for search instead of the original one. Because of inaccurate string comparison of utf8_general_ci, the search_query result text may be different from the original text (e.g organos, Organos, Órganos)
158 lines
4.0 KiB
PHP
158 lines
4.0 KiB
PHP
<?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);
|
|
}
|
|
}
|