1 | <?php |
||
27 | class Pinyin |
||
28 | { |
||
29 | /** |
||
30 | * Dictionary. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected static $dictionary = array(); |
||
35 | |||
36 | /** |
||
37 | * Settings. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | protected static $settings = array( |
||
42 | 'delimiter' => ' ', |
||
43 | 'accent' => true, |
||
44 | 'only_chinese' => false, |
||
45 | 'uppercase' => false, |
||
46 | 'charset' => 'UTF-8' // GB2312,UTF-8 |
||
47 | ); |
||
48 | /** |
||
49 | * Internal charset used by this package. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected static $internalCharset = 'UTF-8'; |
||
54 | |||
55 | /** |
||
56 | * The instance. |
||
57 | * |
||
58 | * @var \Overtrue\Pinyin\Pinyin |
||
59 | */ |
||
60 | private static $_instance; |
||
61 | |||
62 | /** |
||
63 | * Constructor. |
||
64 | * |
||
65 | * set dictionary path. |
||
66 | */ |
||
67 | 1 | private function __construct() |
|
72 | |||
73 | /** |
||
74 | * Disable clone. |
||
75 | */ |
||
76 | private function __clone() |
||
79 | |||
80 | /** |
||
81 | * Get class instance. |
||
82 | * |
||
83 | * @return \Overtrue\Pinyin\Pinyin |
||
84 | */ |
||
85 | 17 | public static function getInstance() |
|
93 | |||
94 | /** |
||
95 | * Setter. |
||
96 | * |
||
97 | * @param string $key |
||
98 | * @param mixed $value |
||
99 | */ |
||
100 | 4 | public static function set($key, $value) |
|
104 | |||
105 | /** |
||
106 | * Global settings. |
||
107 | * |
||
108 | * @param array $settings settings. |
||
109 | */ |
||
110 | public static function settings(array $settings = array()) |
||
114 | |||
115 | /** |
||
116 | * Chinese to pinyin. |
||
117 | * |
||
118 | * @param string $string source string. |
||
119 | * @param array $settings settings. |
||
120 | * |
||
121 | * @return string |
||
122 | */ |
||
123 | 15 | public static function trans($string, array $settings = array()) |
|
129 | |||
130 | /** |
||
131 | * Get first letters of string. |
||
132 | * |
||
133 | * @param string $string source string. |
||
134 | * @param string $settings settings |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | 2 | public static function letter($string, array $settings = array()) |
|
146 | |||
147 | /** |
||
148 | * Parse the string to pinyin. |
||
149 | * |
||
150 | * Overtrue\Pinyin\Pinyin::parse('带着梦想旅行'); |
||
151 | * |
||
152 | * @param string $string |
||
153 | * @param array $settings |
||
154 | * |
||
155 | * @return array |
||
156 | */ |
||
157 | 17 | public static function parse($string, array $settings = array()) |
|
194 | |||
195 | /** |
||
196 | * Add custom words. |
||
197 | * |
||
198 | * @param array $appends |
||
199 | */ |
||
200 | 2 | public static function appends(array $appends) |
|
209 | |||
210 | /** |
||
211 | * Get first letters from pinyin. |
||
212 | * |
||
213 | * @param string $pinyin |
||
214 | * @param array $settings |
||
215 | * |
||
216 | * @return string |
||
217 | */ |
||
218 | 17 | protected function getFirstLetters($pinyin, $settings) |
|
238 | |||
239 | /** |
||
240 | * Replace string to pinyin. |
||
241 | * |
||
242 | * @param string $string |
||
243 | * |
||
244 | * @return string |
||
245 | */ |
||
246 | 17 | protected function string2pinyin($string) |
|
263 | |||
264 | /** |
||
265 | * Format user's words. |
||
266 | * |
||
267 | * @param array $words |
||
268 | * |
||
269 | * @return array |
||
270 | */ |
||
271 | 2 | public static function formatWords($words) |
|
279 | |||
280 | /** |
||
281 | * Format pinyin to lowercase. |
||
282 | * |
||
283 | * @param string $pinyin pinyin string. |
||
284 | * |
||
285 | * @return string |
||
286 | */ |
||
287 | 2 | protected static function formatDictPinyin($pinyin) |
|
295 | |||
296 | /** |
||
297 | * Check if the string has Chinese characters. |
||
298 | * |
||
299 | * @param string $string string to check. |
||
300 | * |
||
301 | * @return int |
||
302 | */ |
||
303 | protected function containChinese($string) |
||
307 | |||
308 | /** |
||
309 | * Remove the non-Chinese characters. |
||
310 | * |
||
311 | * @param string $string source string. |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | 2 | public function justChinese($string) |
|
319 | |||
320 | /** |
||
321 | * Prepare the string. |
||
322 | * |
||
323 | * @param string $string source string. |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | 17 | protected function prepare($string) |
|
335 | |||
336 | /** |
||
337 | * Add delimiter. |
||
338 | * |
||
339 | * @param string $string |
||
340 | * @param string $delimiter |
||
341 | * |
||
342 | * @return string |
||
343 | */ |
||
344 | 17 | protected function delimit($string, $delimiter = '') |
|
353 | |||
354 | /** |
||
355 | * Remove tone. |
||
356 | * |
||
357 | * @param string $string string with tone. |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | 7 | protected function removeTone($string) |
|
370 | |||
371 | /** |
||
372 | * Credits for these 2 functions go to Bouke Versteegh, who shared these |
||
373 | * at http://stackoverflow.com/questions/1598856/convert-numbered-to-accentuated-pinyin. |
||
374 | * |
||
375 | * @param string $string The pinyin string with tone numbers, i.e. "ni3 hao3" |
||
376 | * |
||
377 | * @return string The formatted string with tone marks, i.e. |
||
378 | */ |
||
379 | 13 | protected function addAccents($string) |
|
387 | |||
388 | /** |
||
389 | * Helper callback. |
||
390 | * |
||
391 | * @param array $match |
||
392 | */ |
||
393 | 13 | protected function addAccentsCallback($match) |
|
436 | }//end class |
||
437 | |||
438 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.