Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
mapbuilder.php
1<?php
2
3namespace Bitrix\Main\Search;
4
7
9{
11 protected $tokens = array();
12
16 public function __construct()
17 {
18
19 }
20
25 public static function create()
26 {
27 return new static();
28 }
29
35 public function addText($token)
36 {
37 $token = (string)$token;
38 if($token == '')
39 return $this;
40
41 $value = Content::prepareStringToken($token);
42 $this->tokens[$value] = true;
43 return $this;
44 }
45
51 public function addInteger($token)
52 {
53 if (!Content::isIntegerToken($token))
54 return $this;
55
56 $token = Content::prepareIntegerToken($token);
57
58 $this->tokens[$token] = true;
59 return $this;
60 }
61
67 public function addPhone($phone)
68 {
69 $phone = (string)$phone;
70 $value = preg_replace("/[^0-9\#\*]/i", "", $phone);
71 if($value == '')
72 return $this;
73
74 $altPhone = str_replace(' ', '', $phone);
75 $this->tokens[$altPhone] = true;
76
77 $convertedPhone = PhoneNumber\Parser::getInstance()
78 ->parse($altPhone)
79 ->format(PhoneNumber\Format::E164);
80 if ($convertedPhone != $altPhone)
81 {
82 $this->tokens[$convertedPhone] = true;
83 }
84
85 $length = mb_strlen($value);
86 if($length >= 10 && mb_substr($value, 0, 1) === '7')
87 {
88 $altPhone = '8'.mb_substr($value, 1);
89 $this->tokens[$altPhone] = true;
90 }
91
92 //Right bound. We will stop when 3 digits are left.
93 $bound = $length - 2;
94 if($bound > 0)
95 {
96 for($i = 0; $i < $bound; $i++)
97 {
98 $key = mb_substr($value, $i);
99 $this->tokens[$key] = true;
100 }
101 }
102
103 return $this;
104 }
105
111 public function addEmail($email)
112 {
113 if($email === '')
114 {
115 return $this;
116 }
117
118 $keys = preg_split('/\W+/', $email, -1, PREG_SPLIT_NO_EMPTY);
119 foreach($keys as $key)
120 {
121 $key = Content::prepareStringToken($key);
122 if(!isset($this->tokens[$key]))
123 {
124 $this->tokens[$key] = true;
125 }
126 }
127
128 $key = Content::prepareStringToken($email);
129 $this->tokens[$key] = true;
130
131 return $this;
132 }
133
139 public function addUser($userId)
140 {
141 if(empty($userId))
142 {
143 return $this;
144 }
145
146 $orm = \Bitrix\Main\UserTable::getList(Array(
147 'select' => array('ID', 'LOGIN', 'NAME', 'LAST_NAME', 'SECOND_NAME', 'TITLE', 'EMAIL', 'PERSONAL_MOBILE'),
148 'filter' => array('=ID' => $userId)
149 ));
150
151 while($user = $orm->fetch())
152 {
153 $value = \CUser::FormatName(
154 \CSite::GetNameFormat(),
155 $user,
156 true,
157 false
158 );
159
160 $value = Content::prepareStringToken($value);
161 if($value != '')
162 {
163 $this->tokens[$value] = true;
164 }
165
166 self::addPhone($user['PERSONAL_MOBILE']);
167 self::addEmail($user['EMAIL']);
168 }
169
170 return $this;
171 }
172
177 public function build()
178 {
179 $tokens = array();
180
181 $minTokenSize = Filter\Helper::getMinTokenSize();
182
183 foreach ($this->tokens as $token => $result)
184 {
185 if (mb_strlen($token) >= $minTokenSize)
186 {
187 $tokens[$token] = $result;
188 }
189 }
190
191 return implode(" ", array_keys($tokens));
192 }
193}
static prepareIntegerToken($token)
Definition content.php:28
static isIntegerToken($token)
Definition content.php:39
static prepareStringToken($token)
Definition content.php:18