PHP code example of neunerlei / inflection

1. Go to this page and download the library: Download neunerlei/inflection library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

neunerlei / inflection example snippets


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

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

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

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"

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"];

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

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

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

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

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

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

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";

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

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

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"

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"

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());