neunerlei/inflection

一个具有无实现依赖的屈折词适配器和花哨字符串修改功能的屈折词

2.0.0 2023-01-02 12:10 UTC

This package is auto-updated.

Last update: 2024-08-30 01:44:53 UTC


README

此包包含另一个屈折词,但有所不同。实际屈折(将字符串变为复数和单数)的实现与您想使用的实际实现无关。默认情况下,屈折由symfony/inflector完成,但您可以使用适配器类轻松切换到任何其他屈折词。

此外,屈折词还包含额外的字符串修改功能,如toDashed、toHuman或toGetter等。

安装

使用composer安装此包

composer require neunerlei/inflection

方法

屈折词位于Neunerlei\Inflection\Inflector

toSingular()

返回单词的单数形式

use Neunerlei\Inflection\Inflector;
Inflector::toSingular("trees"); // "tree"

toPlural()

返回单词的复数形式

use Neunerlei\Inflection\Inflector;
Inflector::toPlural("tree"); // "trees"

toSlug()

将“给定字符串”转换为“given-string”或“another.String-you wouldWant”转换为“another-string-you-wouldwant”。但除此之外,它还将“Annahäuser_Römertopf.jpg”转换为“annahaeuser-roemertopf-jpg”。

注意:该实现深受cakephp的Inflector::slug方法的启发(咳咳大部分是偷来的咳咳)。向他们致敬!

use Neunerlei\Inflection\Inflector;
Inflector::toSlug("Given string"); // "given-string"

toFile()

类似于toSlug(),但能够检测文件扩展名和路径段,这两个在将文件转换为slugified版本时将被忽略。

use Neunerlei\Inflection\Inflector;
Inflector::toFile("Given string.jpg"); // "given-string.jpg"

// With and without path handling
Inflector::toFile("/path/with/Given string.jpg", true); 
// "/path/with/given-string.jpg"
Inflector::toFile("/path/with/Given string.jpg"); 
// "path-with-given-string.jpg"

toArray()

将“给定字符串”转换为“given”,“string”或“another.String-you wouldWant”转换为“another”,“string”,“you”,“would”,“want”。

关于智能分割的想法:默认分割器在处理边缘情况(如IP、URL等)时相当愚蠢,因为它会将它们分割成I、P和U、R、L等,而像HandMeAMango这样的东西将正确分割为:hand、me、a、mango。如果您将第二个参数设置为true,这些边缘情况将得到更智能的处理。当给定的内容是“ThisIsFAQandMore”时,可能会出现问题,因为驼峰式命名被破坏了,结果将是:“this is fa qand more”。

use Neunerlei\Inflection\Inflector;
Inflector::toArray("Given string"); // ["given", "string"];
Inflector::toArray(" Given   string   "); // ["given", "string"];

// Intelligent vs default splitting
// Default:
Inflector::toArray("HelloWORLD"); // ["hello", "w", "o", "r", "l", "d"];
Inflector::toArray("FAQ"); // ["f", "a", "q"];
// Intelligent:
Inflector::toArray("HelloWORLD", TRUE);  // ["hello", "world"]
Inflector::toArray("FAQ", TRUE); // ["faq"];

toSpacedUpper() | toHuman()

将“给定字符串”转换为“Given String”或“another.String-you wouldWant”转换为“Another String You Would Want”。

use Neunerlei\Inflection\Inflector;
Inflector::toSpacedUpper("Given string"); // "Given String"
// Alias: toHuman()
Inflector::toHuman("Given string"); // "Given String"

toCamelCase()

将“给定字符串”转换为“GivenString”或“another.String-you wouldWant”转换为“AnotherStringYouWouldWant”。

use Neunerlei\Inflection\Inflector;
Inflector::toCamelCase("Given string"); // "GivenString"

toCamelBack()

将“给定字符串”转换为“givenString”或“another.String-you wouldWant”转换为“anotherStringYouWouldWant”。

use Neunerlei\Inflection\Inflector;
Inflector::toCamelBack("Given string"); // "givenString"

toDashed()

将“给定字符串”转换为“given-string”或“another.String-you wouldWant”转换为“another-string-you-would-want”。

use Neunerlei\Inflection\Inflector;
Inflector::toDashed("Given string"); // "given-string"

setInflectorAdapter()

可用于注入自定义屈折词适配器,如果您不想使用symfony屈折词

use Neunerlei\Inflection\Inflector;
Inflector::setInflectorAdapter(new MyInflectorAdapter());

toUnderscore() | toDatabase()

将“给定字符串”转换为“given_string”或“another.String-you wouldWant”转换为“another_string_you_would_want”。

use Neunerlei\Inflection\Inflector;
Inflector::toUnderscore("Given string"); // "given_string"
// Alias: toUnderscore()
Inflector::toDatabase("Given string"); // "given_string"

toGetter()

将“给定字符串”转换为“getGivenString”或“another.String-you wouldWant”转换为“getAnotherStringYouWouldWant”。它允许您使用第二个参数添加除“get”之外的自定义前缀。它还将对传入的字符串进行清理,因此如果您将“getMyProperty”作为字符串给出,您最终仍然会得到“getMyProperty”而不是“getGetMyProperty”,这不太有意义。清理将删除以下前缀:“is”、“has”、“get”和“set”。

use Neunerlei\Inflection\Inflector;

// Sanitization in action
Inflector::toGetter("myProperty"); // "getMyProperty"
Inflector::toGetter("hasMyProperty"); // "getMyProperty"
Inflector::toGetter("my-Property"); // "getMyProperty"
Inflector::toGetter("isMyProperty"); // "getMyProperty"
Inflector::toGetter("issetProperty"); // "getIssetProperty" this works, too!

// Disable sanitization
Inflector::toGetter("isMyProperty", null, ["noSanitization"]); // "getIsMyProperty"
Inflector::toGetter("hasMyProperty", null, ["ns"]); // "getHasMyProperty"

// Change the prefix
Inflector::toGetter("myProperty", "is"); // "isMyProperty"
Inflector::toGetter("getMyProperty", "has"); // "hasMyProperty"

// Intelligent splitting works here to
Inflector::toGetter("FAQ", "is", ["intelligentSplitting"]); // "isFaq";

toSetter()

将“给定字符串”转换为“setGivenString”或“another.String-you wouldWant”转换为“setAnotherStringYouWouldWant”。像toGetter()一样清理输入字符串,并且有相同的功能。

use Neunerlei\Inflection\Inflector;
Inflector::toSetter("Given string"); // "setGivenString"

toProperty()

通常这是toCamelBack()的别名;但它还会在生成驼峰版本之前,从给定值中删除has/get/is/set前缀。

use Neunerlei\Inflection\Inflector;
Inflector::toSetter("hasMyProperty"); // "myProperty"

toComparable()

此方法将通过统一给定字符串进行转换。统一意味着,它通过删除所有特殊字符,将所有内容转换为小写,计算所有单词及其出现次数(可选)并对它们进行字母排序,使其与其他字符串可比较。这也意味着,文本将不再对人类有意义,但易于用于搜索和比较操作。

use Neunerlei\Inflection\Inflector;
Inflector::toComparable("max mustermann"); // "max1 mustermann1"
Inflector::toComparable("Mustermann, Max "); // "max1 mustermann1"
Inflector::toComparable("first name last name"); // "first1 last1 name2"

toUuid()

将任何给定字符串转换为类似于123e4567-e89b-12d3-a456-426655440000的UUID。如果您想创建唯一的ID,但想结合具有不同单词顺序的多个字符串,这很有用。

请注意,“ASDF QWER”将产生与“QWER ASDF”相同的ID,因为创建ID之前值将被排序。这使得通过姓名和姓氏排序变得容易得多。

use Neunerlei\Inflection\Inflector;
Inflector::toUuid("max mustermann"); // "c47276d9-be31-5329-40d9-25fc290609ec"
Inflector::toUuid("Mustermann, Max "); // "c47276d9-be31-5329-40d9-25fc290609ec"
Inflector::toUuid("first name last name"); // "7f9f995d-6b94-460e-0158-edd97a8b016a"

更改Inflector实现

如上所述,您可以通过提供您最想使用的Inflector的适配器类来更改Inflector的实际实现(欢迎提交拉取请求)。

编写适配器很简单,创建一个新的类

namespace YourVendor\YourNamespace;
use Neunerlei\Inflection\Adapter\InflectorAdapterInterface;

class MyInflectorAdapter implements InflectorAdapterInterface{
    public function toSingular(string $pluralWord) : string{
        // Your fancy inflector does it's job here...
    }

    public function toPlural(string $singularWord) : string{
      // Your fancy inflector does it's job here...
    }
}

编写适配器后,只需按以下方式注册

namespace YourVendor\YourNamespace;
use Neunerlei\Inflection\Inflector;

// If your adapter does not need any dependencies
Inflector::$inflectorAdapterClass = MyInflectorAdapter::class;

// If you have to instantiate your adapter first
Inflector::setInflectorAdapter(new MyInflectorAdapter());

之后,toSingular()和toPlural()方法将使用您自己的适配器实现进行解析。

运行测试

  • 克隆仓库
  • 使用composer install安装依赖项
  • 使用composer test运行测试

Postcardware

您可以自由使用此包,但如果它进入您的生产环境,我将非常感激您从您的家乡寄给我一张明信片,说明您正在使用我们的哪个包。

您可以在这里找到我的地址。

谢谢 :D