Skip to content

Commit

Permalink
CS.
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Jul 2, 2015
1 parent 5d87aac commit 316df74
Show file tree
Hide file tree
Showing 6 changed files with 1,134 additions and 1,116 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
.gitignore export-ignore
.scrutinizer.yml export-ignore
.travis.yml export-ignore
.styleci.yml export-ignore
.php_cs export-ignore
phpunit.php export-ignore
phpunit.xml export-ignore
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.DS_Store
*test.php
/vendor
tmp*
.php_cs.cache
14 changes: 14 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* PHP-CS-fixer configuration.
*/

$finder = Symfony\CS\Finder\DefaultFinder::create()
->in(__DIR__ . '/src/Pinyin/')
;

return Symfony\CS\Config\Config::create()
->fixers(array('-symfony'))
->finder($finder)
->setUsingCache(true)
;
88 changes: 43 additions & 45 deletions src/Pinyin/Pinyin.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
<?php

namespace Overtrue\Pinyin;

/**
* Pinyin.php
* Pinyin.php.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author overtrue <i@overtrue.me>
* @copyright 2015 overtrue <i@overtrue.me>
*
* @author overtrue <anzhengchao@gmail.com>
* @date [2014-07-17 15:49]
* @link https://github.com/overtrue/pinyin
* @link http://overtrue.me
*/

namespace Overtrue\Pinyin;

/**
* Chinese to pinyin translator
* Chinese to pinyin translator.
*
* @example
* <pre>
Expand All @@ -20,7 +26,6 @@
*/
class Pinyin
{

/**
* Dictionary.
*
Expand Down Expand Up @@ -48,10 +53,10 @@ class Pinyin
* @var array
*/
protected static $settings = array(
'delimiter' => ' ',
'accent' => true,
'delimiter' => ' ',
'accent' => true,
'only_chinese' => false,
'uppercase' => false
'uppercase' => false,
);

/**
Expand All @@ -61,7 +66,6 @@ class Pinyin
*/
private static $_instance;


/**
* Constructor.
*
Expand All @@ -70,26 +74,26 @@ class Pinyin
private function __construct()
{
if (is_null(static::$dictionary)) {
self::$dictionary = unserialize(file_get_contents(__DIR__ .'/data/dict.php'));
self::$dictionary = unserialize(file_get_contents(__DIR__.'/data/dict.php'));
}
}

/**
* Disable clone.
*
* @return void
*/
private function __clone() {}
private function __clone()
{
}

/**
* Get class instance
* Get class instance.
*
* @return \Overtrue\Pinyin\Pinyin
*/
public static function getInstance()
{
if (is_null(self::$_instance)) {
self::$_instance = new static;
self::$_instance = new static();
}

return self::$_instance;
Expand All @@ -100,8 +104,6 @@ public static function getInstance()
*
* @param string $key
* @param mixed $value
*
* @return void
*/
public static function set($key, $value)
{
Expand All @@ -112,8 +114,6 @@ public static function set($key, $value)
* Global settings.
*
* @param array $settings settings.
*
* @return void
*/
public static function settings(array $settings = array())
{
Expand All @@ -123,7 +123,7 @@ public static function settings(array $settings = array())
/**
* Chinese to pinyin.
*
* @param string $string source string.
* @param string $string source string.
* @param array $settings settings.
*
* @return string
Expand Down Expand Up @@ -184,7 +184,7 @@ public static function parse($string, array $settings = array())
$delimitedPinyin = $instance->delimit($pinyin, $settings['delimiter']);

$return = array(
'src' => $string,
'src' => $string,
'pinyin' => stripslashes($delimitedPinyin),
'letter' => stripslashes($instance->getFirstLetters($source, $settings)),
);
Expand All @@ -196,8 +196,6 @@ public static function parse($string, array $settings = array())
* Add custom words.
*
* @param array $appends
*
* @return void
*/
public static function appends($appends = array())
{
Expand Down Expand Up @@ -230,7 +228,7 @@ protected function getFirstLetters($pinyin, $settings)
}
}

return join($settings['delimiter'], $letters);
return implode($settings['delimiter'], $letters);
}

/**
Expand All @@ -243,9 +241,9 @@ protected function getFirstLetters($pinyin, $settings)
protected function string2pinyin($string)
{
$dictionary = array_merge(self::$dictionary, $this->getAdditionalWords());
$pinyin = strtr($this->prepare($string), $dictionary);
$pinyin = strtr($this->prepare($string), $dictionary);

return trim(str_replace(" ", ' ', $pinyin));
return trim(str_replace(' ', ' ', $pinyin));
}

/**
Expand All @@ -258,7 +256,7 @@ protected function getAdditionalWords()
static $additionalWords;

if (empty($additionalWords)) {
$additionalWords = static::formatAdditionalWords(include __DIR__ . '/data/additional.php');
$additionalWords = static::formatAdditionalWords(include __DIR__.'/data/additional.php');
}

return array_merge($additionalWords, static::$appends);
Expand Down Expand Up @@ -289,7 +287,7 @@ public static function formatAdditionalWords($additionalWords)
*/
protected static function formatDictPinyin($pinyin)
{
return preg_replace_callback('/[a-z]{1,}:?\d{1}/i', function($matches){
return preg_replace_callback('/[a-z]{1,}:?\d{1}/i', function ($matches) {
return strtolower($matches[0]);
}, " {$pinyin} ");
}
Expand Down Expand Up @@ -354,7 +352,7 @@ protected function delimit($string, $delimiter = '')
protected function removeTone($string)
{
$replacement = array(
'/u:/' => 'u',
'/u:/' => 'u',
'/([a-z])[1-5]/i' => '\\1',
);

Expand All @@ -363,7 +361,7 @@ protected function removeTone($string)

/**
* Credits for these 2 functions go to Bouke Versteegh, who shared these
* at http://stackoverflow.com/questions/1598856/convert-numbered-to-accentuated-pinyin
* at http://stackoverflow.com/questions/1598856/convert-numbered-to-accentuated-pinyin.
*
* @param string $string The pinyin string with tone numbers, i.e. "ni3 hao3"
*
Expand All @@ -379,7 +377,7 @@ protected function addAccents($string)
}

/**
* helper callback
* Helper callback.
*
* @param array $match
*/
Expand All @@ -389,17 +387,17 @@ protected function addAccentsCallback($match)

if ($accentmap === null) {
// where to place the accent marks
$stars = 'a* e* i* o* u* ü* ü* ' .
'A* E* I* O* U* Ü* ' .
'a*i a*o e*i ia* ia*o ie* io* iu* ' .
'A*I A*O E*I IA* IA*O IE* IO* IU* ' .
'o*u ua* ua*i ue* ui* uo* üe* ' .
$stars = 'a* e* i* o* u* ü* ü* '.
'A* E* I* O* U* Ü* '.
'a*i a*o e*i ia* ia*o ie* io* iu* '.
'A*I A*O E*I IA* IA*O IE* IO* IU* '.
'o*u ua* ua*i ue* ui* uo* üe* '.
'O*U UA* UA*I UE* UI* UO* ÜE*';
$nostars = 'a e i o u u: ü ' .
'A E I O U Ü ' .
'ai ao ei ia iao ie io iu ' .
'AI AO EI IA IAO IE IO IU ' .
'ou ua uai ue ui uo üe ' .
$nostars = 'a e i o u u: ü '.
'A E I O U Ü '.
'ai ao ei ia iao ie io iu '.
'AI AO EI IA IAO IE IO IU '.
'ou ua uai ue ui uo üe '.
'OU UA UAI UE UI UO ÜE';

// build an array like array('a' => 'a*') and store statically
Expand All @@ -413,7 +411,7 @@ protected function addAccentsCallback($match)
2 => array('á', 'é', 'í', 'ó', 'ú', 'ǘ', 'Á', 'É', 'Í', 'Ó', 'Ú', 'Ǘ'),
3 => array('ǎ', 'ě', 'ǐ', 'ǒ', 'ǔ', 'ǚ', 'Ǎ', 'Ě', 'Ǐ', 'Ǒ', 'Ǔ', 'Ǚ'),
4 => array('à', 'è', 'ì', 'ò', 'ù', 'ǜ', 'À', 'È', 'Ì', 'Ò', 'Ù', 'Ǜ'),
5 => array('a', 'e', 'i', 'o', 'u', 'ü', 'A', 'E', 'I', 'O', 'U', 'Ü')
5 => array('a', 'e', 'i', 'o', 'u', 'ü', 'A', 'E', 'I', 'O', 'U', 'Ü'),
);

list(, $word, $tone) = $match;
Expand All @@ -426,5 +424,5 @@ protected function addAccentsCallback($match)

return $word;
}
}//end class

}// END OF CLASS
1 change: 1 addition & 0 deletions src/Pinyin/data/additional.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

//additional words
return array(
'了无生趣' => 'liao3 wu2 sheng1 qu4',
Expand Down
Loading

0 comments on commit 316df74

Please sign in to comment.