-
Notifications
You must be signed in to change notification settings - Fork 0
The Inflector
The Inflector class (FOF30\Inflector\Inflector) is used by FOF whenever it wants to convert words between singular and
plural forms, snake-case to camel-case, or convert model names to suitable table names. It uses a fairly precise
approximation of the English language pluralisation rules to perform its work. As such, we strongly recommend you to use
view and table names in English. It will not only help you keep your sanity with the way the Inflector works but it will
also help you work together with other developers who may not understand your language.
Unlike FOF 2, the Inflector is no longer a magic, global, static class. The Inflector is provided as a service by the Container. This means that each component can use its own custom inflection rules without having a potentially adverse effect to other components running on the site. Furthermore it allows you to provide your own Inflector if the one shipped with FOF does not satisfy you, again without the risk of messing up other components.
Sometimes you may run into words which do not inflect correctly. This is expected, mostly with words which have abnormal
inflection rules such as portmanteaus, reborrowed words, foreign words, irregular inflections and so on. For example,
the singular of "paparazzi" is "paparazzo" since this originates from a surname in a classic Italian film.
You can add irregular word pairs to the Inflector using the addWord method. For example:
$container->inflector->addWord('paparazzo', 'paparazzi');The best place to add inflections is in the constructor of your customised Container. For instance, you could have:
namespace Acme\Example\Site\Container;
class Container extends FOF30\Container\Container
{
public function __construct(array $values = array())
{
parent::__construct($values);
$this->inflector->addWord('paparazzo', 'paparazzi');
}
}This way everything using your component -and by necessity your customised container- will have a customised Inflector.
While you're using the Inflector it caches singular-plural pairs in its cache. If a pair is found in the cache the
Inflector will not go through custom pairs or its internal inflection rules; it will return the value from its cache. If
you add a custom word pair after it's cached you need to clear the Inflector's cache first. You can do so with the
deleteCache method.
Important We strongly advise against clearing the cache. If you find yourself in need to do this you're doing something wrong! Instead, you should be adding your word pairs in the Container's constructor as demonstrated above. Keep this in mind when refactoring old code.
The Inflector can be used in your own code. It has some useful features when dealing with inflections and snake-cased / camel-cased words.
You can see if a word is singular:
return $container->inflector->isSingular('donut'); // returns trueLikewise you can see if it is plural:
return $container->inflector->isPlural('donut'); // returns falseOf course you can convert a word from its singular to its plural form:
return $container->inflector->pluralize('donut'); // returns "donuts"And vice versa:
return $container->inflector->singularize('donuts'); // returns "donut"You can convert "snake_case_words" to "CamelCaseWords":
return $container->inflector->camelize("foo_bar_baz"); // returns "FooBarBaz"Or to "camelBackedWords" (like CamelCase with the first character being lowercase):
return $container->inflector->variablize("foo_bar_baz"); // returns "FooBarBaz"And an array of plain words to camel case:
return $container->inflector->implode(["foo", "bar", "baz"]); // returns "FooBarBaz"Conversely, you can convert camel case to snake case:
return $container->inflector->underscore("fooBarBaz"); // returns "foo_bar_baz"Or to an array of plain words:
return $container->inflector->explode("fooBarBaz"); // returns ["foo", "bar", "baz"]Or get a numbered part of a CamelCased string:
return $container->inflector->getPart("fooBarBaz", 2); // returns "bar"
return $container->inflector->getPart("fooBarBaz", -1); // returns "baz"
return $container->inflector->getPart("fooBarBaz", 123); // returns null
return $container->inflector->getPart("fooBarBaz", 123, 'ook'); // returns "ook"You can also convert snake case to a string of plain words:
return $container->inflector->humanize("foo_bar_baz"); // returns "foo bar baz"FOF (Framework on Framework) and its documentation are Copyright © 2010-2020 Nicholas K. Dionysopoulos / Akeeba Ltd.
FOF is Open Source Software, distributed under the GNU General Public License, version 2 of the license, or (at your option) any later version.
The FOF Wiki content is provided under the GNU Free Documentation License, version 1.3 of the license, or (at your option) any later version.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license can be found on the GNU site.