Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
comparator.php
1<?
3
7
8Loader::registerAutoLoadClasses(
9 'sale',
10 array(
11 'Bitrix\Sale\Location\Comparator\Replacement' => 'lib/location/comparator/ru/replacement.php',
12 )
13);
14
16{
17 const LOCALITY = 0;
18 const DISTRICT = 1;
19 const REGION = 2;
20 const COUNTRY = 3;
21
22 public static $variants = null;
23 public static $replacement = null;
24
25 public static function isLocationsEqual($location1, $location2)
26 {
27 foreach($location1 as $type => $name)
28 {
29 if(empty($location2[$type]))
30 continue;
31
32 if($location1[$type] <> '' && $location2[$type] <> '')
33 {
35 $comparator = self::getConcreteComparatorClassaName($type);
36
37 if(!$comparator::isEntityEqual($location1[$type], $location2[$type]))
38 return false;
39 }
40 }
41
42 return true;
43 }
44
45 public static function getReplacement()
46 {
47 if(self::$replacement === null)
48 self::setReplacement();
49
50 return self::$replacement;
51 }
52
53 public static function setReplacement(Replacement $replacement = null)
54 {
55 if($replacement === null)
56 self::$replacement = new Replacement;
57 else
58 self::$replacement = $replacement;
59 }
60
61 public static function isCountryRussia($countryName)
62 {
63 return self::getReplacement()->isCountryRussia($countryName);
64 }
65
71 private static function getConcreteComparatorClassaName($type)
72 {
73 if($type === self::LOCALITY || $type === 'LOCALITY' || $type == 'CITY')
74 $result = 'ComparatorLocality';
75 elseif($type === self::DISTRICT || $type === 'SUBREGION')
76 $result = 'ComparatorDistrict';
77 elseif($type === self::REGION || $type === 'REGION')
78 $result = 'ComparatorRegion';
79 elseif($type === self::COUNTRY || $type === 'COUNTRY')
80 $result = 'ComparatorCountry';
81 else
82 throw new ArgumentOutOfRangeException('type');
83
84 return '\Bitrix\Sale\Location\\'.$result;
85 }
86
87 public static function isEntityEqual($entity1, $entity2, $type = '')
88 {
89 if($type <> '')
90 {
92 $comparator = self::getConcreteComparatorClassaName($type);
93 return $comparator::isEntityEqual($entity1, $entity2);
94 }
95
96 if(is_array($entity1) && !empty($entity1['NAME']))
97 {
98 $entity1N = array('NAME' => $entity1['NAME']);
99 $entity1N['TYPE'] = !empty($entity1['TYPE']) ? $entity1['TYPE'] : '';
100 }
101 else
102 {
103 $entity1N = static::normalize($entity1);
104 }
105
106 if(is_array($entity2) && !empty($entity2['NAME']))
107 {
108 $entity2N = array('NAME' => $entity2['NAME']);
109 $entity2N['TYPE'] = !empty($entity2['TYPE']) ? $entity2['TYPE'] : '';
110 }
111 else
112 {
113 $entity2N = static::normalize($entity2);
114 }
115
116 if($entity1N['NAME'] <> '' && $entity2N['NAME'] <> '')
117 if($entity1N['NAME'] != $entity2N['NAME'])
118 return false;
119
120 if($entity1N['TYPE'] <> '' && $entity2N['TYPE'] <> '')
121 if($entity1N['TYPE'] != $entity2N['TYPE'])
122 return false;
123
124 return true;
125 }
126
127 protected static function getTypes()
128 {
129 return array();
130 }
131
132 protected static function getVariantsValues()
133 {
134 if(static::$variants === null)
135 {
136 static::setVariantsValues(array());
137 }
138
139 return static::$variants;
140 }
141
142 public static function setVariantsValues(array $variants = array())
143 {
144 static::$variants = $variants;
145 }
146
147 public static function setVariants(array $variants = array())
148 {
149 foreach($variants as $type => $v)
150 {
152 $comparator = self::getConcreteComparatorClassaName($type);
153 $comparator::setVariantsValues(
154 self::normalizeVariants($v)
155 );
156 }
157 }
158
159 public static function flatten($value)
160 {
161 $result = preg_replace('/\s*(\‍(.*\‍))/i'.BX_UTF_PCRE_MODIFIER, ' ', $value);
162 $result = preg_replace('/[~\'\"\`\!\@\#\$\%\^\&\*\+\=\\\.\,\?\:\;\{\}\[\]\-]/i'.BX_UTF_PCRE_MODIFIER, ' ', $result);
163 $result = preg_replace('/\s{2,}/i'.BX_UTF_PCRE_MODIFIER, ' ', $result);
164 $result = ToUpper($result);
165 $result = self::getReplacement()->changeYoE($result);
166 $result = trim($result);
167
168 return $result;
169 }
170
171 protected static function normalizeVariants(array $variants)
172 {
173 $result = array();
174
175 foreach($variants as $k => $v)
176 $result[self::flatten($k)] = self::flatten($v);
177
178 return $result;
179 }
180
181 public static function normalizeEntity($name, $type)
182 {
184 $comparator = self::getConcreteComparatorClassaName($type);
185 return $comparator::normalize($name);
186 }
187
188 // Gadyukino d. | Derevnya Gadyukino => array( 'NAME' => 'Gadykino', 'TYPE' => 'DEREVNYA'
189 protected static function normalize($name)
190 {
191 $name = self::flatten($name);
192
193 if($name == '')
194 return array('NAME' => '', 'TYPE' => '');
195
196 $matches = array();
197 $types = static::getTypes();
198 $resultType = '';
199 $variants = static::getVariantsValues();
200
201 foreach($variants as $wrong => $correct)
202 {
203 if($name == self::flatten($wrong))
204 {
205 $name = $correct;
206 break;
207 }
208 }
209
210 foreach($types as $type => $search)
211 {
212 if(!is_array($search))
213 continue;
214
215 $search[] = $type;
216
217 foreach($search as $s)
218 {
219 $regexp = '';
220 $s = self::flatten($s);
221
222 if(mb_strpos($name, $s.' ') !== false)
223 $regexp = '/^'.$s.'\s+(.*)$/i'.BX_UTF_PCRE_MODIFIER;
224 elseif(mb_strpos($name, ' '.$s) !== false)
225 $regexp = '/^(.*)\s+'.$s.'$/i'.BX_UTF_PCRE_MODIFIER;
226
227 if($regexp <> '' && preg_match($regexp, $name, $matches))
228 {
229 $name = $matches[1];
230 $resultType = $type;
231 break 2;
232 }
233 }
234 }
235
236 return array(
237 'NAME' => $name,
238 'TYPE' => $resultType
239 );
240 }
241
242 public static function getLocalityNamesArray($name, $type)
243 {
244 if($name == '')
245 return array();
246
247 $result = array();
248 $types = self::getReplacement()->getLocalityTypes();
249
250 if($type <> '')
251 {
252 $result[] = ToUpper($type.' '.$name);
253 $result[] = ToUpper($name.' '.$type);
254
255 if(is_array($types[$type]) && !empty($types[$type]))
256 {
257 foreach($types[$type] as $t)
258 {
259 $result[] = ToUpper($t.' '.$name);
260 $result[] = ToUpper($name.' '.$t);
261 }
262 }
263 }
264 else
265 {
266 foreach($types as $k => $v)
267 {
268 $result[] = ToUpper($k.' '.$name);
269 $result[] = ToUpper($name.' '.$k);
270
271 if(is_array($v) && !empty($v))
272 {
273 foreach($v as $vv)
274 {
275 $result[] = ToUpper($vv.' '.$name);
276 $result[] = ToUpper($name.' '.$vv);
277 }
278 }
279 }
280 }
281
282 return $result;
283 }
284}
285
287{
288 public static $variants = null;
289
290 protected static function getTypes()
291 {
292 return self::getReplacement()->getLocalityTypes();
293 }
294}
295
297{
298 public static $variants = null;
299
300 protected static function getTypes()
301 {
302 return self::getReplacement()->getDistrictTypes();
303 }
304}
305
307{
308 public static $variants = null;
309
310 protected static function getTypes()
311 {
312 return self::getReplacement()->getRegionTypes();
313 }
314
315 public static function setVariantsValues(array $variants = array())
316 {
317 static::$variants = static::normalizeVariants(
318 array_merge(
319 self::getReplacement()->getRegionVariants(),
320 $variants
321 )
322 );
323 }
324}
325
327{
328 public static $variants = null;
329
330 public static function setVariantsValues(array $variants = array())
331 {
332 static::$variants = static::normalizeVariants(
333 array_merge(
334 self::getReplacement()->getCountryVariants(),
335 $variants
336 )
337 );
338 }
339}
static setVariantsValues(array $variants=array())
static isCountryRussia($countryName)
static setVariantsValues(array $variants=array())
static getLocalityNamesArray($name, $type)
static normalizeVariants(array $variants)
static setReplacement(Replacement $replacement=null)
static setVariantsValues(array $variants=array())