New in Symfony 5.1: Deprecated the Inflector component
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
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']
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.