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