Files
swiftadmin/vendor/overtrue/pinyin/src/Pinyin.php

86 lines
2.9 KiB
PHP
Raw Normal View History

2022-08-19 19:48:37 +08:00
<?php
namespace Overtrue\Pinyin;
use InvalidArgumentException;
2023-04-25 20:11:49 +08:00
/**
* @method static Converter polyphonic()
* @method static Converter surname()
* @method static Converter noWords()
* @method static Converter onlyHans()
* @method static Converter noAlpha()
* @method static Converter noNumber()
* @method static Converter noPunctuation()
* @method static Converter noTone()
* @method static Converter useNumberTone()
* @method static Converter yuToV()
* @method static Converter yuToU()
* @method static Converter withToneStyle(string $toneStyle = 'symbol')
* @method static Collection convert(string $string, callable $beforeSplit = null)
*/
2022-08-19 19:48:37 +08:00
class Pinyin
{
2023-04-25 20:11:49 +08:00
public static function name(string $name, string $toneStyle = Converter::TONE_STYLE_SYMBOL): Collection
2022-08-19 19:48:37 +08:00
{
2023-04-25 20:11:49 +08:00
return self::surname()->withToneStyle($toneStyle)->convert($name);
2022-08-19 19:48:37 +08:00
}
2023-04-25 20:11:49 +08:00
public static function phrase(string $string, string $toneStyle = Converter::TONE_STYLE_SYMBOL): Collection
2022-08-19 19:48:37 +08:00
{
2023-04-25 20:11:49 +08:00
return self::noPunctuation()->withToneStyle($toneStyle)->convert($string);
2022-08-19 19:48:37 +08:00
}
2023-04-25 20:11:49 +08:00
public static function sentence(string $string, string $toneStyle = Converter::TONE_STYLE_SYMBOL): Collection
2022-08-19 19:48:37 +08:00
{
2023-04-25 20:11:49 +08:00
return self::withToneStyle($toneStyle)->convert($string);
2022-08-19 19:48:37 +08:00
}
2023-04-25 20:11:49 +08:00
public static function polyphones(string $string, string $toneStyle = Converter::TONE_STYLE_SYMBOL): Collection
2022-08-19 19:48:37 +08:00
{
2023-04-25 20:11:49 +08:00
return self::polyphonic()->withToneStyle($toneStyle)->convert($string);
2022-08-19 19:48:37 +08:00
}
2023-04-25 20:11:49 +08:00
public static function chars(string $string, string $toneStyle = Converter::TONE_STYLE_SYMBOL): Collection
2022-08-19 19:48:37 +08:00
{
2023-04-25 20:11:49 +08:00
return self::onlyHans()->noWords()->withToneStyle($toneStyle)->convert($string);
2022-08-19 19:48:37 +08:00
}
2023-04-25 20:11:49 +08:00
public static function permalink(string $string, string $delimiter = '-'): string
2022-08-19 19:48:37 +08:00
{
2023-04-25 20:11:49 +08:00
if (!in_array($delimiter, ['_', '-', '.', ''], true)) {
throw new InvalidArgumentException("Delimiter must be one of: '_', '-', '', '.'.");
2022-08-19 19:48:37 +08:00
}
2023-04-25 20:11:49 +08:00
return self::noPunctuation()->noTone()->convert($string)->join($delimiter);
2022-08-19 19:48:37 +08:00
}
2023-04-25 20:11:49 +08:00
public static function nameAbbr(string $string): Collection
2022-08-19 19:48:37 +08:00
{
2023-04-25 20:11:49 +08:00
return self::abbr($string, true);
2022-08-19 19:48:37 +08:00
}
2023-04-25 20:11:49 +08:00
public static function abbr(string $string, bool $asName = false): Collection
2022-08-19 19:48:37 +08:00
{
2023-04-25 20:11:49 +08:00
return self::noTone()
->noPunctuation()
->when($asName, fn ($c) => $c->surname())
->convert($string)
->map(function ($pinyin) {
// 常用于电影名称入库索引处理例如《晚娘2012》-> WN2012
return \is_numeric($pinyin) || preg_match('/\d{2,}/', $pinyin) ? $pinyin : \mb_substr($pinyin, 0, 1);
});
2022-08-19 19:48:37 +08:00
}
2023-04-25 20:11:49 +08:00
public static function __callStatic(string $name, array $arguments)
2022-08-19 19:48:37 +08:00
{
2023-04-25 20:11:49 +08:00
$converter = Converter::make();
2022-08-19 19:48:37 +08:00
2023-04-25 20:11:49 +08:00
if (\method_exists($converter, $name)) {
return $converter->$name(...$arguments);
2022-08-19 19:48:37 +08:00
}
2023-04-25 20:11:49 +08:00
throw new InvalidArgumentException("Method {$name} does not exist.");
2022-08-19 19:48:37 +08:00
}
}