| 1 |
Introduction |
| 2 |
============ |
| 3 |
|
| 4 |
The Doctrine Inflector has methods for inflecting text. The features include pluralization, |
| 5 |
singularization, converting between camelCase and under_score and capitalizing |
| 6 |
words. |
| 7 |
|
| 8 |
Installation |
| 9 |
============ |
| 10 |
|
| 11 |
You can install the Inflector with composer: |
| 12 |
|
| 13 |
.. code-block:: console |
| 14 |
|
| 15 |
$ composer require doctrine/inflector |
| 16 |
|
| 17 |
Usage |
| 18 |
===== |
| 19 |
|
| 20 |
Using the inflector is easy, you can create a new ``Doctrine\Inflector\Inflector`` instance by using |
| 21 |
the ``Doctrine\Inflector\InflectorFactory`` class: |
| 22 |
|
| 23 |
.. code-block:: php |
| 24 |
|
| 25 |
use Doctrine\Inflector\InflectorFactory; |
| 26 |
|
| 27 |
$inflector = InflectorFactory::create()->build(); |
| 28 |
|
| 29 |
By default it will create an English inflector. If you want to use another language, just pass the language |
| 30 |
you want to create an inflector for to the ``createForLanguage()`` method: |
| 31 |
|
| 32 |
.. code-block:: php |
| 33 |
|
| 34 |
use Doctrine\Inflector\InflectorFactory; |
| 35 |
use Doctrine\Inflector\Language; |
| 36 |
|
| 37 |
$inflector = InflectorFactory::createForLanguage(Language::SPANISH)->build(); |
| 38 |
|
| 39 |
The supported languages are as follows: |
| 40 |
|
| 41 |
- ``Language::ENGLISH`` |
| 42 |
- ``Language::ESPERANTO`` |
| 43 |
- ``Language::FRENCH`` |
| 44 |
- ``Language::NORWEGIAN_BOKMAL`` |
| 45 |
- ``Language::PORTUGUESE`` |
| 46 |
- ``Language::SPANISH`` |
| 47 |
- ``Language::TURKISH`` |
| 48 |
|
| 49 |
If you want to manually construct the inflector instead of using a factory, you can do so like this: |
| 50 |
|
| 51 |
.. code-block:: php |
| 52 |
|
| 53 |
use Doctrine\Inflector\CachedWordInflector; |
| 54 |
use Doctrine\Inflector\RulesetInflector; |
| 55 |
use Doctrine\Inflector\Rules\English; |
| 56 |
|
| 57 |
$inflector = new Inflector( |
| 58 |
new CachedWordInflector(new RulesetInflector( |
| 59 |
English\Rules::getSingularRuleset() |
| 60 |
)), |
| 61 |
new CachedWordInflector(new RulesetInflector( |
| 62 |
English\Rules::getPluralRuleset() |
| 63 |
)) |
| 64 |
); |
| 65 |
|
| 66 |
Adding Languages |
| 67 |
---------------- |
| 68 |
|
| 69 |
If you are interested in adding support for your language, take a look at the other languages defined in the |
| 70 |
``Doctrine\Inflector\Rules`` namespace and the tests located in ``Doctrine\Tests\Inflector\Rules``. You can copy |
| 71 |
one of the languages and update the rules for your language. |
| 72 |
|
| 73 |
Once you have done this, send a pull request to the ``doctrine/inflector`` repository with the additions. |
| 74 |
|
| 75 |
Custom Setup |
| 76 |
============ |
| 77 |
|
| 78 |
If you want to setup custom singular and plural rules, you can configure these in the factory: |
| 79 |
|
| 80 |
.. code-block:: php |
| 81 |
|
| 82 |
use Doctrine\Inflector\InflectorFactory; |
| 83 |
use Doctrine\Inflector\Rules\Pattern; |
| 84 |
use Doctrine\Inflector\Rules\Patterns; |
| 85 |
use Doctrine\Inflector\Rules\Ruleset; |
| 86 |
use Doctrine\Inflector\Rules\Substitution; |
| 87 |
use Doctrine\Inflector\Rules\Substitutions; |
| 88 |
use Doctrine\Inflector\Rules\Transformation; |
| 89 |
use Doctrine\Inflector\Rules\Transformations; |
| 90 |
use Doctrine\Inflector\Rules\Word; |
| 91 |
|
| 92 |
$inflector = InflectorFactory::create() |
| 93 |
->withSingularRules( |
| 94 |
new Ruleset( |
| 95 |
new Transformations( |
| 96 |
new Transformation(new Pattern('/^(bil)er$/i'), '\1'), |
| 97 |
new Transformation(new Pattern('/^(inflec|contribu)tors$/i'), '\1ta') |
| 98 |
), |
| 99 |
new Patterns(new Pattern('singulars')), |
| 100 |
new Substitutions(new Substitution(new Word('spins'), new Word('spinor'))) |
| 101 |
) |
| 102 |
) |
| 103 |
->withPluralRules( |
| 104 |
new Ruleset( |
| 105 |
new Transformations( |
| 106 |
new Transformation(new Pattern('^(bil)er$'), '\1'), |
| 107 |
new Transformation(new Pattern('^(inflec|contribu)tors$'), '\1ta') |
| 108 |
), |
| 109 |
new Patterns(new Pattern('noflect'), new Pattern('abtuse')), |
| 110 |
new Substitutions( |
| 111 |
new Substitution(new Word('amaze'), new Word('amazable')), |
| 112 |
new Substitution(new Word('phone'), new Word('phonezes')) |
| 113 |
) |
| 114 |
) |
| 115 |
) |
| 116 |
->build(); |
| 117 |
|
| 118 |
No operation inflector |
| 119 |
---------------------- |
| 120 |
|
| 121 |
The ``Doctrine\Inflector\NoopWordInflector`` may be used to configure an inflector that doesn't perform any operation for |
| 122 |
pluralization and/or singularization. If will simply return the input as output. |
| 123 |
|
| 124 |
This is an implementation of the `Null Object design pattern <https://sourcemaking.com/design_patterns/null_object>`_. |
| 125 |
|
| 126 |
.. code-block:: php |
| 127 |
|
| 128 |
use Doctrine\Inflector\Inflector; |
| 129 |
use Doctrine\Inflector\NoopWordInflector; |
| 130 |
|
| 131 |
$inflector = new Inflector(new NoopWordInflector(), new NoopWordInflector()); |
| 132 |
|
| 133 |
Tableize |
| 134 |
======== |
| 135 |
|
| 136 |
Converts ``ModelName`` to ``model_name``: |
| 137 |
|
| 138 |
.. code-block:: php |
| 139 |
|
| 140 |
echo $inflector->tableize('ModelName'); // model_name |
| 141 |
|
| 142 |
Classify |
| 143 |
======== |
| 144 |
|
| 145 |
Converts ``model_name`` to ``ModelName``: |
| 146 |
|
| 147 |
.. code-block:: php |
| 148 |
|
| 149 |
echo $inflector->classify('model_name'); // ModelName |
| 150 |
|
| 151 |
Camelize |
| 152 |
======== |
| 153 |
|
| 154 |
This method uses `Classify`_ and then converts the first character to lowercase: |
| 155 |
|
| 156 |
.. code-block:: php |
| 157 |
|
| 158 |
echo $inflector->camelize('model_name'); // modelName |
| 159 |
|
| 160 |
Capitalize |
| 161 |
========== |
| 162 |
|
| 163 |
Takes a string and capitalizes all of the words, like PHP's built-in |
| 164 |
``ucwords`` function. This extends that behavior, however, by allowing the |
| 165 |
word delimiters to be configured, rather than only separating on |
| 166 |
whitespace. |
| 167 |
|
| 168 |
Here is an example: |
| 169 |
|
| 170 |
.. code-block:: php |
| 171 |
|
| 172 |
$string = 'top-o-the-morning to all_of_you!'; |
| 173 |
|
| 174 |
echo $inflector->capitalize($string); // Top-O-The-Morning To All_of_you! |
| 175 |
|
| 176 |
echo $inflector->capitalize($string, '-_ '); // Top-O-The-Morning To All_Of_You! |
| 177 |
|
| 178 |
Pluralize |
| 179 |
========= |
| 180 |
|
| 181 |
Returns a word in plural form. |
| 182 |
|
| 183 |
.. code-block:: php |
| 184 |
|
| 185 |
echo $inflector->pluralize('browser'); // browsers |
| 186 |
|
| 187 |
Singularize |
| 188 |
=========== |
| 189 |
|
| 190 |
Returns a word in singular form. |
| 191 |
|
| 192 |
.. code-block:: php |
| 193 |
|
| 194 |
echo $inflector->singularize('browsers'); // browser |
| 195 |
|
| 196 |
Urlize |
| 197 |
====== |
| 198 |
|
| 199 |
Generate a URL friendly string from a string of text: |
| 200 |
|
| 201 |
.. code-block:: php |
| 202 |
|
| 203 |
echo $inflector->urlize('My first blog post'); // my-first-blog-post |
| 204 |
|
| 205 |
Unaccent |
| 206 |
======== |
| 207 |
|
| 208 |
You can unaccent a string of text using the ``unaccent()`` method: |
| 209 |
|
| 210 |
.. code-block:: php |
| 211 |
|
| 212 |
echo $inflector->unaccent('año'); // ano |
| 213 |
|
| 214 |
Legacy API |
| 215 |
========== |
| 216 |
|
| 217 |
The API present in Inflector 1.x is still available, but will be deprecated in a future release and dropped for 3.0. |
| 218 |
Support for languages other than English is available in the 2.0 API only. |
| 219 |
|
| 220 |
Acknowledgements |
| 221 |
================ |
| 222 |
|
| 223 |
The language rules in this library have been adapted from several different sources, including but not limited to: |
| 224 |
|
| 225 |
- `Ruby On Rails Inflector <http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html>`_ |
| 226 |
- `ICanBoogie Inflector <https://github.com/ICanBoogie/Inflector>`_ |
| 227 |
- `CakePHP Inflector <https://book.cakephp.org/3.0/en/core-libraries/inflector.html>`_ |