Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Data.php
Go to the documentation of this file.
1 <?php
7 
14 use Magento\Search\Model\Query as SearchQuery;
16 
22 class Data extends AbstractHelper
23 {
29  protected $_messages = [];
30 
36  protected $string;
37 
44  protected $scopeConfig;
45 
50  protected $escaper;
51 
56  protected $storeManager;
57 
66  public function __construct(
67  Context $context,
71  ) {
72  $this->string = $string;
73  $this->scopeConfig = $context->getScopeConfig();
74  $this->escaper = $escaper;
75  $this->storeManager = $storeManager;
76  parent::__construct($context);
77  }
78 
84  public function isMinQueryLength()
85  {
86  $minQueryLength = $this->getMinQueryLength();
87  $thisQueryLength = $this->string->strlen($this->getQueryText());
88  return !$thisQueryLength || $minQueryLength !== '' && $thisQueryLength < $minQueryLength;
89  }
90 
96  public function getEscapedQueryText()
97  {
98  return $this->escaper->escapeHtml(
99  $this->getPreparedQueryText($this->getQueryText(), $this->getMaxQueryLength())
100  );
101  }
102 
110  public function getResultUrl($query = null)
111  {
112  return $this->_getUrl(
113  'catalogsearch/result',
114  ['_query' => [QueryFactory::QUERY_VAR_NAME => $query], '_secure' => $this->_request->isSecure()]
115  );
116  }
117 
123  public function getSuggestUrl()
124  {
125  return $this->_getUrl(
126  'search/ajax/suggest',
127  ['_secure' => $this->_getRequest()->isSecure()]
128  );
129  }
130 
136  public function getSearchTermUrl()
137  {
138  return $this->_getUrl('search/term/popular');
139  }
140 
147  public function getMinQueryLength($store = null)
148  {
149  return $this->scopeConfig->getValue(
150  SearchQuery::XML_PATH_MIN_QUERY_LENGTH,
151  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
152  $store
153  );
154  }
155 
162  public function getMaxQueryLength($store = null)
163  {
164  return $this->scopeConfig->getValue(
165  SearchQuery::XML_PATH_MAX_QUERY_LENGTH,
166  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
167  $store
168  );
169  }
170 
177  public function addNoteMessage($message)
178  {
179  $this->_messages[] = $message;
180  return $this;
181  }
182 
189  public function setNoteMessages(array $messages)
190  {
191  $this->_messages = $messages;
192  return $this;
193  }
194 
200  public function getNoteMessages()
201  {
202  return $this->_messages;
203  }
204 
212  public function checkNotes($store = null)
213  {
214  if ($this->isQueryTooLong($this->getQueryText(), $this->getMaxQueryLength())) {
215  $this->addNoteMessage(
216  __(
217  'Your search query can\'t be longer than %1, so we shortened your query.',
218  $this->getMaxQueryLength()
219  )
220  );
221  }
222 
223  return $this;
224  }
225 
229  public function getQueryParamName()
230  {
232  }
233 
239  private function isQueryTooLong($queryText, $maxQueryLength)
240  {
241  return ($maxQueryLength !== '' && $this->string->strlen($queryText) > $maxQueryLength);
242  }
243 
249  private function getQueryText()
250  {
251  $queryText = $this->_request->getParam($this->getQueryParamName());
252  return($queryText === null || is_array($queryText))
253  ? ''
254  : $this->string->cleanString(trim($queryText));
255  }
256 
262  private function getPreparedQueryText($queryText, $maxQueryLength)
263  {
264  if ($this->isQueryTooLong($queryText, $maxQueryLength)) {
265  $queryText = $this->string->substr($queryText, 0, $maxQueryLength);
266  }
267  return $queryText;
268  }
269 }
setNoteMessages(array $messages)
Definition: Data.php:189
__()
Definition: __.php:13
$message
__construct(Context $context, StringUtils $string, Escaper $escaper, StoreManagerInterface $storeManager)
Definition: Data.php:66
getMinQueryLength($store=null)
Definition: Data.php:147
checkNotes($store=null)
Definition: Data.php:212
addNoteMessage($message)
Definition: Data.php:177
$maxQueryLength
Definition: form.phtml:17
getMaxQueryLength($store=null)
Definition: Data.php:162
getResultUrl($query=null)
Definition: Data.php:110