Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 50 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
Search | |
0.00% |
0 / 50 |
|
0.00% |
0 / 4 |
240 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
fetchSearchRow | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
getSearchResults | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
42 | |||
execute | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | namespace BO\Zmsdldb\Helper\MySQL; |
4 | |
5 | use BO\Zmsdldb\MySQLAccess; |
6 | use BO\Zmsdldb\Entity\SearchResult; |
7 | use BO\Zmsdldb\Collection\SearchResults; |
8 | use Error; |
9 | |
10 | class Search |
11 | { |
12 | protected $objectTypesClasses = [ |
13 | 'service' => '\\BO\\Zmsdldb\\MySQL\\Entity\\Service', |
14 | 'location' => '\\BO\\Zmsdldb\\MySQL\\Entity\\Location', |
15 | 'topic' => '\\BO\\Zmsdldb\\MySQL\\Entity\\Topic', |
16 | #'link' => '\\BO\\Zmsdldb\\MySQL\\Entity\\Link' |
17 | ]; |
18 | |
19 | protected $searchTypes = []; |
20 | protected $entityTypes = []; |
21 | protected $mysqlAccess = null; |
22 | |
23 | protected $entityIds = []; |
24 | |
25 | public function __construct(MySQLAccess $mysqlAccess, array $entityTypes = [], array $searchTypes = []) |
26 | { |
27 | $this->entityTypes = $entityTypes; |
28 | $this->searchTypes = $searchTypes; |
29 | $this->mysqlAccess = $mysqlAccess; |
30 | } |
31 | |
32 | public function fetchSearchRow($object_id, $locale, $entity_type) |
33 | { |
34 | if (!isset($this->entityIds[$entity_type])) { |
35 | $this->entityIds[$entity_type] = []; |
36 | } |
37 | if (!isset($this->entityIds[$entity_type][$locale])) { |
38 | $this->entityIds[$entity_type][$locale] = []; |
39 | } |
40 | if (!isset($this->entityIds[$entity_type][$locale][$object_id])) { |
41 | $this->entityIds[$entity_type][$locale][$object_id] = $object_id; |
42 | } |
43 | } |
44 | |
45 | protected function getSearchResults(string $query): SearchResults |
46 | { |
47 | try { |
48 | $resultList = new SearchResults(); |
49 | |
50 | foreach ($this->objectTypesClasses as $type => $entityClass) { |
51 | if (isset($this->entityIds[$type])) { |
52 | $sql = "SELECT data_json FROM " . $type . " WHERE "; |
53 | $where = []; |
54 | |
55 | foreach ($this->entityIds[$type] as $locale => $ids) { |
56 | $where[] = "(locale = '" . $locale . "' AND id IN (" . implode(',', $ids) . "))"; |
57 | } |
58 | $sql .= implode(' OR ', $where); |
59 | |
60 | $stm = $this->mysqlAccess->prepare($sql); |
61 | $stm->execute(); |
62 | $stm->fetchAll(\PDO::FETCH_FUNC, function ($data_json) use ($entityClass, $resultList) { |
63 | $entity = new $entityClass(); |
64 | $entity->offsetSet('data_json', $data_json); |
65 | |
66 | $resultList[] = SearchResult::create($entity); |
67 | }); |
68 | } |
69 | } |
70 | $links = $this->mysqlAccess->fromLink()->readSearchResultList($query); |
71 | |
72 | foreach ($links as $link) { |
73 | $resultList[] = SearchResult::create($link); |
74 | } |
75 | return $resultList; |
76 | } catch (\Exception $e) { |
77 | throw $e; |
78 | } |
79 | } |
80 | |
81 | public function execute(string $query): SearchResults |
82 | { |
83 | try { |
84 | $query = '+' . implode(' +', explode(' ', $query)); |
85 | |
86 | $sqlArgs = [$query]; |
87 | $sql = "SELECT |
88 | object_id, locale, entity_type |
89 | FROM |
90 | search |
91 | WHERE |
92 | MATCH (search_value) AGAINST (? IN BOOLEAN MODE) |
93 | "; |
94 | |
95 | if (!empty($this->entityTypes)) { |
96 | $sql .= " AND entity_type "; |
97 | if (1 == count($this->entityTypes)) { |
98 | $sql .= " = ?"; |
99 | $sqlArgs[] = current($this->entityTypes); |
100 | } else { |
101 | $questionMarks = array_fill(0, count($this->entityTypes), '?'); |
102 | $sql .= ' IN (' . implode(', ', $questionMarks) . ')'; |
103 | array_push($sqlArgs, ...$this->entityTypes); |
104 | } |
105 | } |
106 | $sql .= ' GROUP BY object_id, entity_type, locale'; |
107 | |
108 | $stm = $this->mysqlAccess->prepare($sql); |
109 | |
110 | $stm->execute($sqlArgs); |
111 | |
112 | $stm->fetchAll(\PDO::FETCH_FUNC, [$this, 'fetchSearchRow']); |
113 | |
114 | $resultList = $this->getSearchResults($query); |
115 | |
116 | return $resultList; |
117 | } catch (\Exception $e) { |
118 | throw $e; |
119 | } |
120 | } |
121 | } |