Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
helper.php
1<?php
10
12
19class Helper
20{
22
32 public static function matchAgainstWildcard($phrase, $wildcard = '*', $minTokenSize = null)
33 {
34 $connection = \Bitrix\Main\Application::getConnection();
35 $helper = $connection->getSqlHelper();
36 $orValues = static::parseFulltextPhrase($phrase, $minTokenSize);
37
38 foreach ($orValues as $i => $andValues)
39 {
40 $orValues[$i] = '(' . $helper->getMatchAndExpression($andValues, $wildcard == '*') . ')';
41 }
42
43 return $helper->getMatchOrExpression($orValues);
44 }
45
46 public static function parseFulltextPhrase($phrase, $minTokenSize = null)
47 {
48 $ftMinTokenSize = $minTokenSize ?: static::getMinTokenSize();
49 $orValues = array();
50
51 //split to words by any non-word symbols
52 $andValues = static::splitWords($phrase);
53
54 if (!empty($andValues))
55 {
56 $andValues = array_filter(
57 $andValues,
58 function($val) use ($ftMinTokenSize)
59 {
60 return (mb_strlen($val) >= $ftMinTokenSize);
61 }
62 );
63
64 if (!empty($andValues))
65 {
66 $orValues[] = $andValues;
67 }
68 }
69
70 return $orValues;
71 }
72
76 public static function getMinTokenSize()
77 {
78 static $ftMinTokenSize = null;
79 if($ftMinTokenSize === null)
80 {
81 $config = \Bitrix\Main\Application::getConnection()->getConfiguration();
82 $ftMinTokenSize = ($config["ft_min_token_size"] ?? self::FT_MIN_TOKEN_SIZE);
83 }
84 return $ftMinTokenSize;
85 }
86
92 public static function splitWords($string)
93 {
94 static $encoding = null;
95 if($encoding === null)
96 {
97 $encoding = strtolower(\Bitrix\Main\Context::getCurrent()->getCulture()->getCharset());
98 }
99
100 if($encoding <> "utf-8")
101 {
102 $string = Text\Encoding::convertEncoding($string, $encoding, "UTF-8");
103 }
104 else
105 {
106 //mysql [1064] syntax error, unexpected $end
107 $string = Text\UtfSafeString::escapeInvalidUtf($string);
108 }
109
110 //split to words by any non-word symbols
111 $values = preg_split("/[^\\p{L}\\d_]/u", $string);
112
113 $values = array_filter(
114 $values,
115 function($val)
116 {
117 return ($val <> '');
118 }
119 );
120 $values = array_unique($values);
121
122 if($encoding <> "utf-8")
123 {
124 $values = Text\Encoding::convertEncoding($values, "UTF-8", $encoding);
125 }
126 return $values;
127 }
128}
static getCurrent()
Definition context.php:241
static parseFulltextPhrase($phrase, $minTokenSize=null)
Definition helper.php:46
static matchAgainstWildcard($phrase, $wildcard=' *', $minTokenSize=null)
Definition helper.php:32