Skip to content

Commit

Permalink
Doctrine Inflector 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jwage committed Jun 13, 2018
1 parent 1cec3f8 commit 4a0fe1b
Show file tree
Hide file tree
Showing 33 changed files with 1,669 additions and 719 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ jobs:
- stage: Code Quality
env: STATIC_ANALYSIS
install: travis_retry composer install --prefer-dist
script: vendor/bin/phpstan analyse -l 7 -c phpstan.neon lib
script: vendor/bin/phpstan analyse -l 7 -c phpstan.neon lib tests
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@
"require-dev": {
"doctrine/coding-standard": "^4.0",
"phpstan/phpstan": "^0.9.2",
"phpstan/phpstan-phpunit": "^0.9.4",
"phpstan/phpstan-strict-rules": "^0.9",
"phpunit/phpunit": "^7.0"
},
"autoload": {
"psr-4": { "Doctrine\\Common\\Inflector\\": "lib/Doctrine/Common/Inflector" }
"psr-4": { "Doctrine\\Inflector\\": "lib/Doctrine/Inflector" }
},
"autoload-dev": {
"psr-4": { "Doctrine\\Tests\\Common\\Inflector\\": "tests/Doctrine/Tests/Common/Inflector" }
"psr-4": { "Doctrine\\Tests\\Inflector\\": "tests/Doctrine/Tests/Inflector" }
},
"extra": {
"branch-alias": {
"dev-master": "1.4.x-dev"
"dev-master": "2.0.x-dev"
}
}
}
59 changes: 52 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 55 additions & 37 deletions docs/en/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,62 @@ You can install the Inflector with composer:
Here are the available methods that you can use:

Default Setup
=============

If you want to use the default rules that come with Doctrine you can use the following.

.. code-block:: php
use Doctrine\Inflector\InflectorFactory;
$inflectorFactory = new InflectorFactory();
$inflector = $inflectorFactory->createInflector();
Custom Setup
============

If you want to setup custom singular and plural rules, you can configure the inflector like this.

.. code-block:: php
use Doctrine\Inflector\InflectorFactory;
use Doctrine\Inflector\Rules\Irregular;
use Doctrine\Inflector\Rules\Rule;
use Doctrine\Inflector\Rules\Rules;
use Doctrine\Inflector\Rules\Uninflected;
use Doctrine\Inflector\Rules\Word;
$inflectorFactory = new InflectorFactory();
$inflector = $inflectorFactory->createInflector(
$inflectorFactory->createSingularRuleset(
new Rules(
new Rule('/^(bil)er$/i', '\1'),
new Rule('/^(inflec|contribu)tors$/i', '\1ta')
),
new Uninflected(new Word('singulars')),
new Irregular(new Rule('spins', 'spinor'))
),
$inflectorFactory->createPluralRuleset(
new Rules(new Rule('/^(alert)$/i', '\1ables')),
new Uninflected(new Word('noflect'), new Word('abtuse')),
new Irregular(
new Rule('amaze', 'amazable'),
new Rule('phone', 'phonezes')
)
)
);
Tableize
========

Converts ``ModelName`` to ``model_name``:

.. code-block:: php
echo Inflector::tableize('ModelName'); // model_name
echo $inflector->tableize('ModelName'); // model_name
Classify
========
Expand All @@ -36,7 +84,7 @@ Converts ``model_name`` to ``ModelName``:

.. code-block:: php
echo Inflector::classify('model_name'); // ModelName
echo $inflector->classify('model_name'); // ModelName
Camelize
========
Expand All @@ -45,7 +93,7 @@ This method uses `Classify`_ and then converts the first character to lowercase:

.. code-block:: php
echo Inflector::camelize('model_name'); // modelName
echo $inflector->camelize('model_name'); // modelName
ucwords
=======
Expand All @@ -61,9 +109,9 @@ Here is an example:
$string = 'top-o-the-morning to all_of_you!';
echo Inflector::ucwords($string); // Top-O-The-Morning To All_of_you!
echo $inflector->ucwords($string); // Top-O-The-Morning To All_of_you!
echo Inflector::ucwords($string, '-_ '); // Top-O-The-Morning To All_Of_You!
echo $inflector->ucwords($string, '-_ '); // Top-O-The-Morning To All_Of_You!
Pluralize
=========
Expand All @@ -72,44 +120,14 @@ Returns a word in plural form.

.. code-block:: php
echo Inflector::pluralize('browser'); // browsers
echo $inflector->pluralize('browser'); // browsers
Singularize
===========

.. code-block:: php
echo Inflector::singularize('browsers'); // browser
Rules
=====

Customize the rules for pluralization and singularization:

.. code-block:: php
Inflector::rules('plural', ['/^(inflect)or$/i' => '\1ables']);
Inflector::rules('plural', [
'rules' => ['/^(inflect)ors$/i' => '\1ables'],
'uninflected' => ['dontinflectme'],
'irregular' => ['red' => 'redlings']
]);
The arguments for the ``rules`` method are:

- ``$type`` - The type of inflection, either ``plural`` or ``singular``
- ``$rules`` - An array of rules to be added.
- ``$reset`` - If true, will unset default inflections for all new rules that are being defined in $rules.

Reset
=====

Clears Inflectors inflected value caches, and resets the inflection
rules to the initial values.

.. code-block:: php
Inflector::reset();
echo $inflector->singularize('browsers'); // browser
Slugify
=======
Expand Down
Loading

0 comments on commit 4a0fe1b

Please sign in to comment.