originphp / inflector
OriginPHP Inflector
2.0.0
2021-01-04 10:35 UTC
Requires
- php: >=7.3.0
Requires (Dev)
- phpstan/phpstan: ^0.12.64
- phpunit/phpunit: ^9.2
README
Inflector 类可以将单词从单数变为复数或反之,还可以更改单词的词形。
安装
要安装此包
$ composer require originphp/inflector
单数与复数转换
将单词转换为单数或复数形式
转换
use Origin\Inflector\Inflector; $singular = Inflector::singular('apples'); // apple $plural = Inflector::plural('apple'); // apples
规则
Inflector 内置了一些标准规则,涵盖了大多数单词,是一个项目的良好起点,然而有时你可能需要添加自定义规则。
在你的 bootstrap/configuration
use Origin\Inflector\Inflector; // regex or string Inflector::rules('singular',[ '/(quiz)zes$/i' => '\\1' ]); // regex or string Inflector::rules('plural',[ '/(quiz)$/i' => '\1zes' ]); // string only Inflector::rules('uncountable',['sheep']); // string only Inflector::rules('irregular',[ 'child' => 'children' ]);
单数和复数规则可以是字符串或正则表达式
use Origin\Inflector\Inflector; Inflector::rules('singular',['fezzes'=>'fez']); Inflector::rules('plural',['fez'=>'fezzes']);
更改词形
更改单词的词形
use Origin\Inflector\Inflector; // change underscored words $studyCaps = Inflector::studlyCaps('big_tree') ; // BigTree $camelCase = Inflector::camelCase('big_tree') ; // bigTree $human = Inflector::human('big_tree'); // Big Tree // Change studly capped words $underscored = Inflector::underscored('BigTree'); // big_tree
Inflector 还具有这两个方法,OriginPHP 也使用这些方法来实现其功能。
// converts class name into into table name (plural underscored) $tableName = Inflector::tableName('BigTree'); // big_trees // converts table names (plural underscored) into a class name $className = Inflector::className('big_trees'); // BigTree