placemat / editor
v2.0.3
2020-05-25 08:24 UTC
Requires
- php: >=7.1
- ext-mbstring: *
Requires (Dev)
- php-coveralls/php-coveralls: ^2.1
- phpunit/phpunit: ~6.0
README
PHP 7.1+ 的一个干净、优雅、Unicode 安全、流畅、不可变、可本地化、无依赖的字符串操作库
安装
您可以通过 composer 安装此软件包
composer require placemat/editor
使用方法
创建实例
要使用 Editor,首先需要在您的字符串上实例化它
echo Editor::create('hello world')->upperCase(); // HELLO WORLD
这有点冗长,所以 Editor 还提供了一个全局辅助函数
echo s('hello world')->upperCase(); // HELLO WORLD
做一些事情!
Editor 是流畅且链式的,因此您可以继续向它们添加操作
echo s(' EDITOR is pretty neat I guess 💩 ') ->trim() ->titleCase(); // Editor is Pretty Neat I Guess 💩
Editor 也是不可变的,因此您不会弄乱原始实例
$str = s('Apple'); echo $str->plural(); // Apples echo $str; // Apple
Editor 实现了 PHP 的 __toString() 魔术方法,因此在大多数情况下,您可以直接将其用作字符串,并且它将正常工作
$str = s('worlds'); echo 'Hello ' . $str->singular()->upperCaseFirst(); // Hello World
如果您出于某种原因需要获取常规字符串,您可以选择将 Editor 的实例转换为字符串,或者调用 str()
$hello = s('Hello World'); (string) $hello; // Will be a string $hello->str(); // Will also be a string
标题化
Editor 实现了正确的标题化,基于 John Gruber 的疯狂标题化脚本
s('this is a headline that will be properly title-cased')->titleCase(); // This Is a Headline That Will Be Properly Title-Cased
如果您追求其他库大胆声明的标题化,您需要 upperCaseFirst()
s('this is not actually title casing')->upperCaseFirst(); // This Is Not Actually Title-casing
变形
Editor 支持将字符串变为单数或复数
s('apples')->singular(); // 'apple' s('apple')->plural(); // 'apples' // plural also has a $count parameter, in case you need to pluralize dynamically s('apple')->plural($count); // 'apple' if $count is 1, 'apples' otherwise
Editor 中的变形是可本地化的。目前,Editor 支持 6 种语言
- 英语 ('en')
- 西班牙语 ('es')
- 法语 ('fr')
- 葡萄牙语 ('pt')
- 挪威语 Bokmal ('nb')
- 土耳其语 ('tr')
s('bijou')->plural($count, 'fr'); // 'bijoux'
如果您想添加变形器,请这样做
- 扩展
Hipsterjazzbo\Editor\Inflectors\Inflector- 使用
Editor::registerInflector($inflector)注册- 这就完了!
如果您添加了变形器,请随意打开一个 Pull Request!
函数参考
完整的函数参考文档可能是我应该写的内容。但到目前为止,您可以浏览 主 Editor 类。每个方法都有文档块和测试 👍
