Introduction
The Doctrine Inflector has methods for inflecting text. The features include pluralization, singularization, converting between camelCase and under_score and capitalizing words.
Usage
Using the inflector is easy, you can create a new Doctrine\Inflector\Inflector
instance by using
the Doctrine\Inflector\InflectorFactory
class:
By default it will create an English inflector. If you want to use another language, just pass the language
you want to create an inflector for to the createForLanguage()
method:
The supported languages are as follows:
Language::ENGLISH
Language::FRENCH
Language::NORWEGIAN_BOKMAL
Language::PORTUGUESE
Language::SPANISH
Language::TURKISH
If you want to manually construct the inflector instead of using a factory, you can do so like this:
1 use Doctrine\Inflector\CachedWordInflector;
use Doctrine\Inflector\RulesetInflector;
use Doctrine\Inflector\Rules\English;
$inflector = new Inflector(
new CachedWordInflector(new RulesetInflector(
English\Rules::getSingularRuleset()
)),
new CachedWordInflector(new RulesetInflector(
English\Rules::getPluralRuleset()
))
);
2
3
4
5
6
7
8
9
10
11
12
Adding Languages
If you are interested in adding support for your language, take a look at the other languages defined in the
Doctrine\Inflector\Rules
namespace and the tests located in Doctrine\Tests\Inflector\Rules
. You can copy
one of the languages and update the rules for your language.
Once you have done this, send a pull request to the doctrine/inflector
repository with the additions.
Custom Setup
If you want to setup custom singular and plural rules, you can configure these in the factory:
1 use Doctrine\Inflector\InflectorFactory;
use Doctrine\Inflector\Rules\Pattern;
use Doctrine\Inflector\Rules\Patterns;
use Doctrine\Inflector\Rules\Ruleset;
use Doctrine\Inflector\Rules\Substitution;
use Doctrine\Inflector\Rules\Substitutions;
use Doctrine\Inflector\Rules\Transformation;
use Doctrine\Inflector\Rules\Transformations;
use Doctrine\Inflector\Rules\Word;
$inflector = InflectorFactory::create()
->withSingularRules(
new Ruleset(
new Transformations(
new Transformation(new Pattern('/^(bil)er$/i'), '\1'),
new Transformation(new Pattern('/^(inflec|contribu)tors$/i'), '\1ta')
),
new Patterns(new Pattern('singulars')),
new Substitutions(new Substitution(new Word('spins'), new Word('spinor')))
)
)
->withPluralRules(
new Ruleset(
new Transformations(
new Transformation(new Pattern('^(bil)er$'), '\1'),
new Transformation(new Pattern('^(inflec|contribu)tors$'), '\1ta')
),
new Patterns(new Pattern('noflect'), new Pattern('abtuse')),
new Substitutions(
new Substitution(new Word('amaze'), new Word('amazable')),
new Substitution(new Word('phone'), new Word('phonezes'))
)
)
)
->build();
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
No operation inflector
The Doctrine\Inflector\NoopWordInflector
may be used to configure an inflector that doesn't perform any operation for
pluralization and/or singularization. If will simply return the input as output.
This is an implementation of the Null Object design pattern.
Camelize
This method uses Classify and then converts the first character to lowercase:
1 echo $inflector->camelize('model_name'); // modelName
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
word delimiters to be configured, rather than only separating on
whitespace.
Here is an example:
Urlize
Generate a URL friendly string from a string of text:
1 echo $inflector->urlize('My first blog post'); // my-first-blog-post
Unaccent
You can unaccent a string of text using the unaccent()
method:
1 echo $inflector->unaccent('año'); // ano
Legacy API
The API present in Inflector 1.x is still available, but will be deprecated in a future release and dropped for 3.0. Support for languages other than English is available in the 2.0 API only.
Acknowledgements
The language rules in this library have been adapted from several different sources, including but not limited to: