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() |
|
68 | { |
||
69 | 1 | if (!static::$dictionary) { |
|
|
|||
70 | 1 | $list = json_decode(file_get_contents(dirname(__DIR__).'/data/dict.php'), true); |
|
71 | 1 | static::appends($list); |
|
72 | 1 | } |
|
73 | 1 | } |
|
74 | |||
75 | /** |
||
76 | * Disable clone. |
||
77 | */ |
||
78 | private function __clone() |
||
81 | |||
82 | /** |
||
83 | * Get class instance. |
||
84 | * |
||
85 | * @return \Overtrue\Pinyin\Pinyin |
||
86 | */ |
||
87 | 14 | public static function getInstance() |
|
88 | { |
||
89 | 14 | if (is_null(self::$_instance)) { |
|
90 | 1 | self::$_instance = new static(); |
|
91 | 1 | } |
|
92 | |||
93 | 14 | return self::$_instance; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * Setter. |
||
98 | * |
||
99 | * @param string $key |
||
100 | * @param mixed $value |
||
101 | */ |
||
102 | 3 | public static function set($key, $value) |
|
106 | |||
107 | /** |
||
108 | * Global settings. |
||
109 | * |
||
110 | * @param array $settings settings. |
||
111 | */ |
||
112 | public static function settings(array $settings = array()) |
||
116 | |||
117 | /** |
||
118 | * Chinese to pinyin. |
||
119 | * |
||
120 | * @param string $string source string. |
||
121 | * @param array $settings settings. |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | 12 | public static function trans($string, array $settings = array()) |
|
131 | |||
132 | /** |
||
133 | * Get first letters of string. |
||
134 | * |
||
135 | * @param string $string source string. |
||
136 | * @param string $settings settings |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | 2 | public static function letter($string, array $settings = array()) |
|
148 | |||
149 | /** |
||
150 | * Parse the string to pinyin. |
||
151 | * |
||
152 | * Overtrue\Pinyin\Pinyin::parse('带着梦想旅行'); |
||
153 | * |
||
154 | * @param string $string |
||
155 | * @param array $settings |
||
156 | * |
||
157 | * @return array |
||
158 | */ |
||
159 | 14 | public static function parse($string, array $settings = array()) |
|
196 | |||
197 | /** |
||
198 | * Add custom words. |
||
199 | * |
||
200 | * @param array $appends |
||
201 | */ |
||
202 | 2 | public static function appends(array $appends) |
|
210 | |||
211 | /** |
||
212 | * Get first letters from pinyin. |
||
213 | * |
||
214 | * @param string $pinyin |
||
215 | * @param array $settings |
||
216 | * |
||
217 | * @return string |
||
218 | */ |
||
219 | 14 | protected function getFirstLetters($pinyin, $settings) |
|
239 | |||
240 | /** |
||
241 | * Replace string to pinyin. |
||
242 | * |
||
243 | * @param string $string |
||
244 | * |
||
245 | * @return string |
||
246 | */ |
||
247 | 14 | protected function string2pinyin($string) |
|
264 | |||
265 | /** |
||
266 | * Format user's words. |
||
267 | * |
||
268 | * @param array $words |
||
269 | * |
||
270 | * @return array |
||
271 | */ |
||
272 | 2 | public static function formatWords($words) |
|
280 | |||
281 | /** |
||
282 | * Format pinyin to lowercase. |
||
283 | * |
||
284 | * @param string $pinyin pinyin string. |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | 2 | protected static function formatDictPinyin($pinyin) |
|
296 | |||
297 | /** |
||
298 | * Check if the string has Chinese characters. |
||
299 | * |
||
300 | * @param string $string string to check. |
||
301 | * |
||
302 | * @return int |
||
303 | */ |
||
304 | protected function containChinese($string) |
||
308 | |||
309 | /** |
||
310 | * Remove the non-Chinese characters. |
||
311 | * |
||
312 | * @param string $string source string. |
||
313 | * |
||
314 | * @return string |
||
315 | */ |
||
316 | 2 | public function justChinese($string) |
|
320 | |||
321 | /** |
||
322 | * Prepare the string. |
||
323 | * |
||
324 | * @param string $string source string. |
||
325 | * |
||
326 | * @return string |
||
327 | */ |
||
328 | 14 | protected function prepare($string) |
|
336 | |||
337 | /** |
||
338 | * Add delimiter. |
||
339 | * |
||
340 | * @param string $string |
||
341 | */ |
||
342 | 14 | protected function delimit($string, $delimiter = '') |
|
346 | |||
347 | /** |
||
348 | * Remove tone. |
||
349 | * |
||
350 | * @param string $string string with tone. |
||
351 | * |
||
352 | * @return string |
||
353 | */ |
||
354 | 5 | protected function removeTone($string) |
|
363 | |||
364 | /** |
||
365 | * Credits for these 2 functions go to Bouke Versteegh, who shared these |
||
366 | * at http://stackoverflow.com/questions/1598856/convert-numbered-to-accentuated-pinyin. |
||
367 | * |
||
368 | * @param string $string The pinyin string with tone numbers, i.e. "ni3 hao3" |
||
369 | * |
||
370 | * @return string The formatted string with tone marks, i.e. |
||
371 | */ |
||
372 | 11 | protected function addAccents($string) |
|
380 | |||
381 | /** |
||
382 | * Helper callback. |
||
383 | * |
||
384 | * @param array $match |
||
385 | */ |
||
386 | 11 | protected function addAccentsCallback($match) |
|
429 | }//end class |
||
430 | |||
431 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.