1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overtrue\Pinyin; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @method static Converter polyphonic() |
9
|
|
|
* @method static Converter surname() |
10
|
|
|
* @method static Converter noWords() |
11
|
|
|
* @method static Converter onlyHans() |
12
|
|
|
* @method static Converter noAlpha() |
13
|
|
|
* @method static Converter noNumber() |
14
|
|
|
* @method static Converter noPunctuation() |
15
|
|
|
* @method static Converter noTone() |
16
|
|
|
* @method static Converter useNumberTone() |
17
|
|
|
* @method static Converter yuToV() |
18
|
|
|
* @method static Converter yuToU() |
19
|
|
|
* @method static Converter withToneStyle(string $toneStyle = 'default') |
20
|
|
|
* @method static Collection convert(string $string, callable $beforeSplit = null) |
21
|
|
|
*/ |
22
|
|
|
class Pinyin |
23
|
1 |
|
{ |
24
|
1 |
|
public static function name(string $name, string $toneStyle = Converter::TONE_STYLE_DEFAULT): Collection |
25
|
1 |
|
{ |
26
|
|
|
return self::surname()->withToneStyle($toneStyle)->convert($name); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public static function phrase(string $string, string $toneStyle = Converter::TONE_STYLE_DEFAULT): Collection |
30
|
|
|
{ |
31
|
|
|
return self::noPunctuation()->withToneStyle($toneStyle)->convert($string); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static function sentence(string $string, string $toneStyle = Converter::TONE_STYLE_DEFAULT): Collection |
35
|
|
|
{ |
36
|
|
|
return self::withToneStyle($toneStyle)->convert($string); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public static function polyphones(string $string, string $toneStyle = Converter::TONE_STYLE_DEFAULT): Collection |
40
|
|
|
{ |
41
|
|
|
return self::polyphonic()->withToneStyle($toneStyle)->convert($string); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function chars(string $string, string $toneStyle = Converter::TONE_STYLE_DEFAULT): Collection |
45
|
|
|
{ |
46
|
|
|
return self::onlyHans()->noWords()->withToneStyle($toneStyle)->convert($string); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function permalink(string $string, string $delimiter = '-'): string |
50
|
|
|
{ |
51
|
|
|
if (!in_array($delimiter, ['_', '-', '.', ''], true)) { |
52
|
|
|
throw new InvalidArgumentException("Delimiter must be one of: '_', '-', '', '.'."); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return self::noPunctuation()->noTone()->convert($string)->join($delimiter); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public static function nameAbbr(string $string): Collection |
59
|
|
|
{ |
60
|
|
|
return self::abbr($string, true); |
61
|
|
|
} |
62
|
21 |
|
|
63
|
|
|
public static function abbr(string $string, bool $asName = false): Collection |
64
|
21 |
|
{ |
65
|
21 |
|
return self::noTone() |
66
|
|
|
->noPunctuation() |
67
|
|
|
->when($asName, fn ($c) => $c->surname()) |
68
|
|
|
->convert($string) |
69
|
|
|
->map(function ($pinyin) { |
70
|
|
|
// 常用于电影名称入库索引处理,例如:《晚娘2012》-> WN2012 |
71
|
|
|
return \is_numeric($pinyin) || preg_match('/\d{2,}/', $pinyin) ? $pinyin : \mb_substr($pinyin, 0, 1); |
72
|
|
|
}); |
73
|
|
|
} |
74
|
|
|
|
75
|
10 |
|
public static function __callStatic(string $name, array $arguments) |
76
|
|
|
{ |
77
|
10 |
|
$converter = Converter::make(); |
78
|
|
|
|
79
|
10 |
|
if (\method_exists($converter, $name)) { |
80
|
|
|
return $converter->$name(...$arguments); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
throw new InvalidArgumentException("Method {$name} does not exist."); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|