Skip to content

Commit

Permalink
Add Inflector::is_uncountable()
Browse files Browse the repository at this point in the history
  • Loading branch information
pdscopes authored and olvlvl committed May 21, 2019
1 parent 16acb5d commit 8ecffff
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
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()));
}
}

0 comments on commit 8ecffff

Please sign in to comment.