1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Donatas Navidonskis <[email protected]> |
5
|
|
|
* @since 2017 |
6
|
|
|
* @class LangFulltextBooleanFilter |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class LangFulltextBooleanFilter extends FulltextFilter { |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @param DataQuery $query |
13
|
|
|
* |
14
|
|
|
* @return DataQuery |
15
|
|
|
*/ |
16
|
|
|
protected function applyOne(DataQuery $query) { |
17
|
|
|
$this->model = $query->applyRelation($this->relation); |
|
|
|
|
18
|
|
|
$predicate = sprintf("MATCH (%s) AGAINST (? IN BOOLEAN MODE)", $this->getDbName()); |
19
|
|
|
|
20
|
|
|
$keywords = $this->convertForeignToLatin($this->getValue()); |
|
|
|
|
21
|
|
|
|
22
|
|
|
$andProcessor = create_function('$matches', ' |
|
|
|
|
23
|
|
|
return " +" . $matches[2] . " +" . $matches[4] . " "; |
24
|
|
|
'); |
25
|
|
|
$notProcessor = create_function('$matches', ' |
|
|
|
|
26
|
|
|
return " -" . $matches[3]; |
27
|
|
|
'); |
28
|
|
|
|
29
|
|
|
$keywords = preg_replace_callback('/()("[^()"]+")( and )("[^"()]+")()/i', $andProcessor, $keywords); |
30
|
|
|
$keywords = preg_replace_callback('/(^| )([^() ]+)( and )([^ ()]+)( |$)/i', $andProcessor, $keywords); |
31
|
|
|
$keywords = preg_replace_callback('/(^| )(not )("[^"()]+")/i', $notProcessor, $keywords); |
32
|
|
|
$keywords = preg_replace_callback('/(^| )(not )([^() ]+)( |$)/i', $notProcessor, $keywords); |
33
|
|
|
|
34
|
|
|
$keywords = $this->addStarsToKeywords($keywords); |
35
|
|
|
|
36
|
|
|
return $query->where([$predicate => $keywords]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $keywords |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
protected function addStarsToKeywords($keywords) { |
45
|
|
|
if (! trim($keywords)) { |
46
|
|
|
return ""; |
47
|
|
|
} |
48
|
|
|
// Add * to each keyword |
49
|
|
|
$splitWords = preg_split("/ +/", trim($keywords)); |
50
|
|
|
while (list($i, $word) = each($splitWords)) { |
|
|
|
|
51
|
|
|
if ($word[0] == '"') { |
52
|
|
|
while (list($i, $subword) = each($splitWords)) { |
|
|
|
|
53
|
|
|
$word .= ' '.$subword; |
54
|
|
|
if (substr($subword, -1) == '"') { |
55
|
|
|
break; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} else { |
59
|
|
|
$word .= '*'; |
60
|
|
|
} |
61
|
|
|
$newWords[] = $word; |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return implode(" ", $newWords); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Convert Foreign symbols to Latin |
69
|
|
|
* |
70
|
|
|
* @param string $str . |
71
|
|
|
* |
72
|
|
|
* @return string with only Latin Symbols |
73
|
|
|
*/ |
74
|
|
|
public static function convertForeignToLatin($str) { |
75
|
|
|
$tr = [ |
76
|
|
|
"А" => "a", "Б" => "b", "В" => "v", "Г" => "g", "Д" => "d", |
77
|
|
|
"Е" => "e", "Ё" => "yo", "Ж" => "zh", "З" => "z", "И" => "i", |
78
|
|
|
"Й" => "j", "К" => "k", "Л" => "l", "М" => "m", "Н" => "n", |
79
|
|
|
"О" => "o", "П" => "p", "Р" => "r", "С" => "s", "Т" => "t", |
80
|
|
|
"У" => "u", "Ф" => "f", "Х" => "kh", "Ц" => "ts", "Ч" => "ch", |
81
|
|
|
"Ш" => "sh", "Щ" => "sch", "Ъ" => "", "Ы" => "y", "Ь" => "", |
82
|
|
|
"Э" => "e", "Ю" => "yu", "Я" => "ya", "а" => "a", "б" => "b", |
83
|
|
|
"в" => "v", "г" => "g", "д" => "d", "е" => "e", "ё" => "yo", |
84
|
|
|
"ж" => "zh", "з" => "z", "и" => "i", "й" => "j", "к" => "k", |
85
|
|
|
"л" => "l", "м" => "m", "н" => "n", "о" => "o", "п" => "p", |
86
|
|
|
"р" => "r", "с" => "s", "т" => "t", "у" => "u", "ф" => "f", |
87
|
|
|
"х" => "kh", "ц" => "ts", "ч" => "ch", "ш" => "sh", "щ" => "sch", |
88
|
|
|
"ъ" => "", "ы" => "y", "ь" => "", "э" => "e", "ю" => "yu", |
89
|
|
|
"я" => "ya", "ą" => "a", "č" => "c", "ę" => "e", "ė" => "e", |
90
|
|
|
"į" => "i", "š" => "s", "ų" => "u", "ū" => "u", "ž" => "z", |
91
|
|
|
]; |
92
|
|
|
|
93
|
|
|
return strtr($str, $tr); |
94
|
|
|
} |
95
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.