Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
languagenormalizer.php
1<?php
3
6
15{
17 protected $letters = [];
19 protected $aliases = [];
21 protected $titles = [];
22
28 public function __construct($lang = LANGUAGE_ID, array $langData = [])
29 {
30 if(empty($langData))
31 {
32 $langData = $this->loadLangData($lang);
33 }
34
35 $this->setLangData($langData);
36 }
37
39 public function normalize(string $string): string
40 {
41 $result = $string;
42
43 if(is_array($this->letters) && !empty($this->letters))
44 {
45 $result = $this->replaceLetters($result, $this->letters);
46 }
47
48 if(is_array($this->aliases) && !empty($this->aliases))
49 {
50 $result = $this->replaceAliases($result, $this->aliases);
51 }
52
53 if(is_array($this->titles) && !empty($this->titles))
54 {
55 $result = $this->replaceTitles($result, $this->titles);
56 }
57
58 return $result;
59 }
60
66 protected function replaceLetters(string $string, array $letters): string
67 {
68 $result = $string;
69
70 foreach($letters as $search => $replace)
71 {
72 $result = str_replace($search, $replace, $result);
73 }
74
75 return $result;
76 }
77
83 protected function replaceTitles(string $string, array $titles): string
84 {
85 $result = $string;
86 $implodedTitles = implode('|', $titles);
87 $regexp = '/^('.$implodedTitles.')+\s+(.*?)$/i'.BX_UTF_PCRE_MODIFIER;
88 $result = preg_replace($regexp, '$2', $result);
89
90 if($result !== null)
91 {
92 $regexp = '/^(.*?)\s+(' . $implodedTitles . ')+$/i' . BX_UTF_PCRE_MODIFIER;
93 $result = preg_replace($regexp, '$1', $result);
94 }
95
96 return $result !== null ? $result : '';
97 }
98
104 protected function replaceAliases(string $string, array $aliases): string
105 {
106 return $aliases[$string] ?? $string;
107 }
108
113 protected function loadLangData(string $lang): array
114 {
115 $result = [];
116
117 if(empty($result))
118 {
119 if ($langDataPath = $this->getLangDataFilePath($lang))
120 {
121 if (File::isFileExists($langDataPath))
122 {
123 if (Localization\Translation::allowConvertEncoding())
124 {
125 if (class_exists('\Bitrix\Main\Localization\StreamConverter'))
126 {
127 $result = Localization\StreamConverter::include($langDataPath, $lang);
128 }
129 elseif (class_exists('\Bitrix\Main\Localization\SteamConverter'))
130 {
131 $result = Localization\SteamConverter::include($langDataPath, $lang);
132 }
133 }
134 else
135 {
136 $result = require $langDataPath;
137 }
138 }
139 }
140 }
141
142 return $result;
143 }
144
149 protected function getLangDataFilePath(string $lang): string
150 {
151 return $_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/location/lang/'.$lang.'/lib/entity/address/normalizer/langnormdata.php';
152 }
153
157 public function setLangData(array $langData): void
158 {
159 if(isset($langData['LETTERS']) && is_array($langData['LETTERS']))
160 {
161 $this->letters = $langData['LETTERS'];
162 }
163
164 if(isset($langData['ALIASES']) && is_array($langData['ALIASES']))
165 {
166 $this->aliases = $langData['ALIASES'];
167 }
168
169 if(isset($langData['TITLES']) && is_array($langData['TITLES']))
170 {
171 $this->titles = $langData['TITLES'];
172 }
173 }
174}