Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doctrine Inflector 2.0 #90

Merged
merged 1 commit into from
Jun 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",

This comment was marked as resolved.

This comment was marked as resolved.

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

98 changes: 58 additions & 40 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,13 +93,13 @@ 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
=======
capitalize
==========

Takes a string and capitalizes all of the words, like PHP's built-in
ucwords function. This extends that behavior, however, by allowing the
``ucwords`` function. This extends that behavior, however, by allowing the
word delimiters to be configured, rather than only separating on
whitespace.

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->capitalize($string); // Top-O-The-Morning To All_of_you!

echo Inflector::ucwords($string, '-_ '); // Top-O-The-Morning To All_Of_You!
echo $inflector->capitalize($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