Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
searchquery.php
1<?
3
4class SearchQuery implements \JsonSerializable
5{
6 protected $queryWords = [];
7 protected $query = '';
8 protected $cacheable = true;
9 protected $dynamicSearchEntities = [];
10 protected $rawQuery = '';
11
12 public function __construct(array $options)
13 {
14 if (isset($options['queryWords']) && is_array($options['queryWords']))
15 {
16 $this->setQueryWords($options['queryWords']);
17 }
18
19 if (isset($options['cacheable']) && is_bool($options['cacheable']))
20 {
21 $this->setCacheable($options['cacheable']);
22 }
23
24 if (isset($options['dynamicSearchEntities']) && is_array($options['dynamicSearchEntities']))
25 {
26 $this->setDynamicSearchEntities($options['dynamicSearchEntities']);
27 }
28
29 if (isset($options['query']) && is_string($options['query']))
30 {
31 $this->setRawQuery($options['query']);
32 }
33 }
34
35 public function getQueryWords(): array
36 {
37 return $this->queryWords;
38 }
39
40 public function getQuery(): string
41 {
42 return $this->query;
43 }
44
45 protected function setQueryWords(array $queryWords)
46 {
47 foreach ($queryWords as $queryWord)
48 {
49 if (is_string($queryWord) && mb_strlen(trim($queryWord)) > 0)
50 {
51 $this->queryWords[] = trim($queryWord);
52 $this->query = join(' ', $this->queryWords);
53 }
54 }
55 }
56
57 public function isCacheable(): bool
58 {
59 return $this->cacheable;
60 }
61
62 public function setDynamicSearchEntities(array $entities): void
63 {
64 $this->dynamicSearchEntities = $entities;
65 }
66
67 public function hasDynamicSearchEntity(string $entityId): bool
68 {
69 return in_array($entityId, $this->dynamicSearchEntities);
70 }
71
72 public function setCacheable(bool $flag = true)
73 {
74 if (is_bool($flag))
75 {
76 $this->cacheable = $flag;
77 }
78
79 return $this;
80 }
81
82 public function jsonSerialize()
83 {
84 return [
85 'queryWords' => $this->getQueryWords(),
86 'cacheable' => $this->isCacheable()
87 ];
88 }
89
90 public function getRawQuery(): string
91 {
92 return $this->rawQuery;
93 }
94
95 protected function setRawQuery(string $rawQuery): void
96 {
97 $this->rawQuery = $rawQuery;
98 }
99}