Thomas Calvet
Contributed by Thomas Calvet in #35092

Symfony Inflector component converts words between their singular and plural forms (for now, only in English). It's used in several parts of Symfony to generate singular property names from plural method names and viceversa ($foo <-> setFoos(), $foos <-> addFoo()).

In Symfony 5.1 we've deprecated this component and moved its logic into the String component, which provides the best experience to work with strings in PHP applications.

The new inflector is available via the EnglishInflector class (which reflects the intention to expand the inflector to other languages in the future). The interface remains the same:

1
2
3
4
5
6
7
8
9
namespace Symfony\Component\String\Inflector;

interface InflectorInterface
{
    // the returned values are arrays because in some cases, there
    // are multiple valid singulars/plurals for a given word
    public function singularize(string $plural): array;
    public function pluralize(string $singular): array;
}

This is how you can use it in your applications:

1
2
3
4
5
6
7
8
9
10
11
use Symfony\Component\String\Inflector\EnglishInflector;

$inflector = new EnglishInflector();

$result = $inflector->singularize('teeth');   // ['tooth']
$result = $inflector->singularize('radii');   // ['radius']
$result = $inflector->singularize('leaves');  // ['leaf', 'leave', 'leaff']

$result = $inflector->pluralize('bacterium'); // ['bacteria']
$result = $inflector->pluralize('news');      // ['news']
$result = $inflector->pluralize('person');    // ['persons', 'people']
Published in #Living on the edge