From 8ecffff745f6b835ae62d8abaf54a0e56385ff32 Mon Sep 17 00:00:00 2001 From: Peter Scopes Date: Tue, 21 May 2019 20:52:01 +0200 Subject: [PATCH] Add Inflector::is_uncountable() --- README.md | 6 ++++++ lib/inflector.php | 24 +++++++++++++++++++++++- tests/InflectorTest.php | 10 ++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9ac570c..87e323a 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/inflector.php b/lib/inflector.php index 7e00e0e..8984388 100644 --- a/lib/inflector.php +++ b/lib/inflector.php @@ -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; } @@ -458,4 +458,26 @@ public function ordinalize($number) { return $number . $this->ordinal($number); } + + /** + * Returns true if the word is uncountable, false otherwise. + * + *
+	 * $this->is_uncountable('advice');    // true
+	 * $this->is_uncountable('weather');   // true
+	 * $this->is_uncountable('cat');       // false
+	 * 
+ * + * @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]]); + } } diff --git a/tests/InflectorTest.php b/tests/InflectorTest.php index 7ed4eeb..3322fd1 100755 --- a/tests/InflectorTest.php +++ b/tests/InflectorTest.php @@ -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())); + } }