Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Inflector::is_uncountable() #32

Merged
merged 1 commit into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ $inflector->ordinalize(1002); // "1002nd"
$inflector->ordinalize(1003); // "1003rd"
$inflector->ordinalize(-11); // "-11th"
$inflector->ordinalize(-1021); // "-1021st"

# uncountable

$inflector->is_uncountable("advice"); // true
$inflector->is_uncountable("weather"); // true
$inflector->is_uncountable("cat"); // false
```

Helpers makes it easy to use default locale inflections.
Expand Down
24 changes: 23 additions & 1 deletion lib/inflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static public function get($locale = self::DEFAULT_LOCALE)
*
* @param Inflections $inflections
*/
protected function __construct(Inflections $inflections = null)
public function __construct(Inflections $inflections = null)
{
$this->inflections = $inflections ?: new Inflections;
}
Expand Down Expand Up @@ -458,4 +458,26 @@ public function ordinalize($number)
{
return $number . $this->ordinal($number);
}

/**
* Returns true if the word is uncountable, false otherwise.
*
* <pre>
* $this->is_uncountable('advice'); // true
* $this->is_uncountable('weather'); // true
* $this->is_uncountable('cat'); // false
* </pre>
*
* @param string $word
*
* @return bool
*/
public function is_uncountable($word)
{
$rc = (string) $word;

return $rc
&& preg_match('/\b[[:word:]]+\Z/u', downcase($rc), $matches)
&& isset($this->inflections->uncountables[$matches[0]]);
}
}
10 changes: 10 additions & 0 deletions tests/InflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,14 @@ public function test_titleize_accentuated_characters()
{
$this->assertEquals("L'été Aux Âmes Inouïes", self::$inflector->titleize("l'été_aux_âmes_inouïes"));
}

public function test_is_uncountable()
{
$inflections = new Inflections();
$inflections->uncountable($uncountable = uniqid());
$inflector = new Inflector($inflections);

$this->assertTrue($inflector->is_uncountable($uncountable));
$this->assertFalse($inflector->is_uncountable(uniqid()));
}
}