Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
languagenormalizer.php
1<?
3
5
12{
14 protected $letters = [];
16 protected $aliases = [];
18 protected $titles = [];
19
25 public function __construct($lang = LANGUAGE_ID, array $langData = [])
26 {
27 if(empty($langData))
28 {
29 $langData = $this->loadLangData($lang);
30 }
31
32 $this->setLangData($langData);
33 }
34
36 public function normalize($string)
37 {
38 $result = $string;
39
40 if(is_array($this->letters) && !empty($this->letters))
41 {
42 $result = $this->replaceLetters($result, $this->letters);
43 }
44
45 if(is_array($this->aliases) && !empty($this->aliases))
46 {
47 $result = $this->replaceAliases($result, $this->aliases);
48 }
49
50 if(is_array($this->titles) && !empty($this->titles))
51 {
52 $result = $this->replaceTitles($result, $this->titles);
53 }
54
55 return $result;
56 }
57
63 protected function replaceLetters($string, array $letters)
64 {
65 $result = $string;
66
67 foreach($letters as $search => $replace)
68 {
69 $result = str_replace($search, $replace, $result);
70 }
71
72 return $result;
73 }
74
80 protected function replaceTitles($string, $titles)
81 {
82 $result = $string;
83 $implodedTitles = implode('|', $titles);
84 $regexp = '/^('.$implodedTitles.')+\s+(.*?)$/i'.BX_UTF_PCRE_MODIFIER;
85 $result = preg_replace($regexp, '$2', $result);
86 $regexp = '/^(.*?)\s+('.$implodedTitles.')+$/i'.BX_UTF_PCRE_MODIFIER;
87 $result = preg_replace($regexp, '$1', $result);
88 return $result;
89 }
90
96 protected function replaceAliases($string, $aliases)
97 {
98 $result = $string;
99
100 if(isset($aliases[$string]))
101 {
102 $result = $aliases[$string];
103 }
104
105 return $result;
106 }
107
112 protected function loadLangData($lang)
113 {
114 $result = [];
115
116 if(empty($result))
117 {
118 if ($langDataPath = $this->getLangDataFilePath($lang))
119 {
120 if (File::isFileExists($langDataPath))
121 {
122 $result = require $langDataPath;
123 }
124 }
125 }
126
127 return $result;
128 }
129
134 protected function getLangDataFilePath($lang)
135 {
136 return __DIR__.'/lang/'.$lang.'/langnormdata.php';
137 }
138
142 public function setLangData($langData)
143 {
144 if (isset($langData['LETTERS']) && is_array($langData['LETTERS']))
145 {
146 $this->letters = $langData['LETTERS'];
147 }
148
149 if (isset($langData['ALIASES']) && is_array($langData['ALIASES']))
150 {
151 $this->aliases = $langData['ALIASES'];
152 }
153
154 if (isset($langData['TITLES']) && is_array($langData['TITLES']))
155 {
156 $this->titles = $langData['TITLES'];
157 }
158 }
159}
__construct($lang=LANGUAGE_ID, array $langData=[])