crysalead/inflector

Inflector 库

2.0.2 2016-05-17 16:18 UTC

This package is auto-updated.

Last update: 2024-08-29 04:14:24 UTC


README

Build Status Code Coverage

Inflector 是一个轻量级库,可以执行字符串转换,如单复数转换、下划线到驼峰命名、单词大写等。转换规则可以本地化。

Inflector 类预先填充了英语的单复数转换规则,可以直接使用。

用法

以下是一些使用默认地区(即英语)的 inflector 的示例

use Lead\Inflector\Inflector;

# pluralize

Inflector::pluralize('post');                       // "posts"
Inflector::pluralize('posts');                      // "posts"
Inflector::pluralize('child');                      // "children"
Inflector::pluralize('ContactPerson');              // "ContactPeople"

# singularize

Inflector::singularize('posts');                    // "post"
Inflector::singularize('children');                 // "child"
Inflector::singularize('ContactPeople');            // "ContactPerson"

# transliterate

Inflector::transliterate('の話が出たので大丈夫かなあと'); // "no huaga chutanode da zhang fukanaato"

# slug

Inflector::slug('Foo:Bar & Cie');                   // "Foo-Bar-Cie"
Inflector::slug('Foo:Bar & Cie', '_');              // "Foo_Bar_Cie"

# parameterize

Inflector::parameterize('Foo:Bar & Cie');           // "foo-bar-cie"
Inflector::parameterize('Foo:Bar & Cie', '_');      // "foo_bar_cie"

# camelize

Inflector::camelize('test_field');                  // "TestField"
Inflector::camelize('TEST_FIELD');                  // "TestField"
Inflector::camelize('my_name\space');               // "MyName\Space"

# camelback

Inflector::camelback('test_field');                 // "testField"
Inflector::camelback('TEST_FIELD');                 // "testField"

# underscore

Inflector::underscore('TestField');                 // "test_field"
Inflector::underscore('MyName\Space');              // "my_name\space"
Inflector::underscore('dashed-string');             // "dashed_string"

# dasherize

Inflector::dasherize('underscored_string');         // "underscored_string"

# humanize

Inflector::humanize('employee_salary');             // "Employee salary"
Inflector::humanize('author_id');                   // "Author"

# titleize

Inflector::titleize('man from the boondocks');      // "Man From The Boondocks"
Inflector::titleize('x-men: the last stand');       // "X Men: The Last Stand"
Inflector::titleize('TheManWithoutAPast');          // "The Man Without A Past"
Inflector::titleize('raiders_of_the_lost_ark');     // "Raiders Of The Lost Ark"

#### 自定义地区的使用示例

namespace inflector\Inflector;

Inflector::pluralize('child');                       // "children"

//Load custom definition for `'zz'` locale using a closure
Inflector::singular('/x$/i', '', 'zz');
Inflector::plural('/([^x])$/i', '\1x', 'zz');

Inflector::singularize('abcdefx', 'zz');             // "abcdef"
Inflector::pluralize('abcdef', 'zz');                // "abcdefx"

如果您想为转换规则使用其他默认语言,可以直接覆盖 'default' 规则。

Inflector::reset(true); // Remove all loaded inflection rules.

Inflector::singular('/x$/i', '', 'default');
Inflector::plural('/([^x])$/i', '\1x', 'default');

Inflector::singularize('abcdefx');             // "abcdef"
Inflector::pluralize('abcdef');                // "abcdefx"

注意:您可以在 spec/fixture 中查看西班牙语和法语的一些转换规则示例。

要求

需要 PHP >= 5.4 和 PECL intl 扩展。

sudo apt-get install php-intl

使用 Composer 安装

推荐通过 Composer 安装此包。创建一个 composer.json 文件并运行 composer install 命令来安装

{
	"require":
	{
		"crysalead/inflector": "~1.0"
	}
}

测试

可以使用以下命令运行 spec 套件

cd inflector
composer install
./bin/kahlan

PS:您的系统上需要安装 Composer

致谢

大部分代码和文档是从 Ruby On RailsInflector 中改编而来。