Skip to content

Instantly share code, notes, and snippets.

@briankip briankip/Pluralizer.php
Last active Jul 4, 2017

Embed
What would you like to do?
from Illuminate\Support
<?php
use Doctrine\Common\Inflector\Inflector;
class Pluralizer
{
/**
* Uncountable word forms.
*
* @var array
*/
public static $uncountable = [
'audio',
'bison',
'chassis',
'compensation',
'coreopsis',
'data',
'deer',
'education',
'emoji',
'equipment',
'evidence',
'feedback',
'fish',
'furniture',
'gold',
'information',
'jedi',
'knowledge',
'love',
'metadata',
'money',
'moose',
'nutrition',
'offspring',
'plankton',
'pokemon',
'police',
'rain',
'rice',
'series',
'sheep',
'species',
'swine',
'traffic',
'wheat',
];
/**
* Get the plural form of an English word.
*
* @param string $value
* @param int $count
* @return string
*/
public static function plural($value, $count = 2)
{
if ((int) $count === 1 || static::uncountable($value)) {
return $value;
}
$plural = Inflector::pluralize($value);
return static::matchCase($plural, $value);
}
/**
* Get the singular form of an English word.
*
* @param string $value
* @return string
*/
public static function singular($value)
{
$singular = Inflector::singularize($value);
return static::matchCase($singular, $value);
}
/**
* Determine if the given value is uncountable.
*
* @param string $value
* @return bool
*/
protected static function uncountable($value)
{
return in_array(strtolower($value), static::$uncountable);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.