diff --git a/Tests/InflectorTest.php b/Tests/InflectorTest.php deleted file mode 100644 index fd708013..00000000 --- a/Tests/InflectorTest.php +++ /dev/null @@ -1,249 +0,0 @@ -inflector = new Inflector(); - } - - /** - * @testdox A rule cannot be added to the inflector if it is of an unsupported type - */ - public function testAddRuleException() - { - $this->expectException(\InvalidArgumentException::class); - - /** @noinspection PhpParamsInspection */ - TestHelper::invoke($this->inflector, 'addRule', new \stdClass(), 'singular'); - } - - /** - * @testdox A countable rule can be added to the inflector - */ - public function testAddCountableRule() - { - // Add string. - $this->inflector->addCountableRule('foo'); - - $countable = TestHelper::getValue($this->inflector, 'countable'); - - $this->assertContains( - 'foo', - $countable['rules'], - 'Checks a countable rule was added.' - ); - - // Add array. - $this->inflector->addCountableRule(['goo', 'car']); - - $countable = TestHelper::getValue($this->inflector, 'countable'); - - $this->assertContains( - 'car', - $countable['rules'], - 'Checks a countable rule was added by array.' - ); - } - - /** - * @testdox A string is checked to determine if it a countable word - * - * @param string $input A string. - * @param boolean $expected The expected result of the function call. - */ - #[DataProvider('seedIsCountable')] - public function testIsCountable(string $input, bool $expected) - { - $this->assertEquals( - $expected, - $this->inflector->isCountable($input) - ); - } - - /** - * @testdox A string is checked to determine if it is in plural form - * - * @param string $singular The singular form of a word. - * @param string $plural The plural form of a word. - */ - #[DataProvider('seedSinglePlural')] - public function testIsPlural(string $singular, string $plural) - { - $this->assertTrue( - $this->inflector->isPlural($plural), - "'$plural' should be reported as plural" - ); - - if ($singular !== $plural) { - $this->assertFalse( - $this->inflector->isPlural($singular), - "'$singular' should not be reported as a plural form in comparison to '$plural'" - ); - } - } - - /** - * @testdox A string is checked to determine if it is in singular form - * - * @param string $singular The singular form of a word. - * @param string $plural The plural form of a word. - */ - #[DataProvider('seedSinglePlural')] - public function testIsSingular(string $singular, string $plural) - { - $this->assertTrue( - $this->inflector->isSingular($singular), - "'$singular' should be reported as singular" - ); - - if ($singular !== $plural) { - $this->assertFalse( - $this->inflector->isSingular($plural), - "'$plural' should not be reported as a singular form in comparison to '$singular'" - ); - } - } - - /** - * @testdox A string is converted to its plural form - * - * @param string $singular The singular form of a word. - * @param string $plural The plural form of a word. - */ - #[DataProvider('seedSinglePlural')] - public function testPluralize(string $singular, string $plural) - { - $this->assertSame( - $plural, - $this->inflector->pluralize($singular), - "'$plural' should be the plural form of '$singular'" - ); - } - - /** - * @testdox A string that is already plural is returned in the same form - */ - public function testPluralizeAlreadyPlural() - { - $this->assertSame( - 'glasses', - $this->inflector->pluralize('glasses'), - "'glasses' should not be pluralised'" - ); - } - - /** - * @testdox A string is converted to its singular form - * - * @param string $singular The singular form of a word. - * @param string $plural The plural form of a word. - */ - #[DataProvider('seedSinglePlural')] - public function testSingularize(string $singular, string $plural) - { - $this->assertSame( - $singular, - $this->inflector->singularize($plural), - "'$singular' should be the singular form of '$plural'" - ); - } - - /** - * @testdox A string that is already singular is returned in the same form - */ - public function testSingularizeAlreadySingular() - { - $this->assertSame( - 'glass', - $this->inflector->singularize('glass'), - "'glass' should not be singularised'" - ); - } -} diff --git a/composer.json b/composer.json index 5ddf279e..55a3b4e7 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,6 @@ "symfony/polyfill-mbstring": "^1.31.0" }, "require-dev": { - "doctrine/inflector": "^2.0.10", "joomla/test": "^4.0", "phpunit/phpunit": "^12.5 || ^13.0", "squizlabs/php_codesniffer": "^4.0", @@ -19,8 +18,7 @@ "phpstan/phpstan-deprecation-rules": "^2.0.3" }, "suggest": { - "ext-mbstring": "For improved processing", - "doctrine/inflector": "To use the string inflector" + "ext-mbstring": "For improved processing" }, "autoload": { "psr-4": { diff --git a/docs/index.md b/docs/index.md index c87f1b49..b0b9f85c 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1 +1,2 @@ * [Overview](overview.md) +* [Updating from v4 to v5](v4-to-v5-update.md) diff --git a/docs/v4-to-v5-update.md b/docs/v4-to-v5-update.md new file mode 100644 index 00000000..3a94f637 --- /dev/null +++ b/docs/v4-to-v5-update.md @@ -0,0 +1,31 @@ +## Updating from v4 to v5 + +The following changes were made to the String package between version 4 and version 5. + +### The `Inflector` class has been removed + +`Joomla\String\Inflector`, deprecated in 4.x, has been removed. Use the `doctrine/inflector` package +directly: + +```php +use Doctrine\Inflector\InflectorFactory; + +$inflector = InflectorFactory::create()->build(); + +$inflector->pluralize('house'); // 'houses' +$inflector->singularize('houses'); // 'house' +$inflector->tableize('BlogPost'); // 'blog_post' +``` + +| 4.x | Replacement | +|---|---| +| `Inflector::pluralize()`, `singularize()`, `tableize()`, `classify()`, `camelize()` | The method of the same name on a `Doctrine\Inflector\Inflector` | +| `Inflector::isPlural($word)` | `$inflector->pluralize($inflector->singularize($word)) === $word` | +| `Inflector::isSingular($word)` | `$inflector->singularize($word) === $word` | +| `Inflector::ucwords()` | PHP's `ucwords($string, " \n\t\r\0\x0B-")` | +| `Inflector::addCountableRule()`, `isCountable()` | Keep your own list of countable words | +| `Inflector::rules()`, `reset()` | Had no effect. Use `InflectorFactory::create()->withSingularRules()` / `withPluralRules()` | + +### Dependency changes + +`doctrine/inflector` is no longer suggested and no longer a development dependency. diff --git a/src/Inflector.php b/src/Inflector.php deleted file mode 100644 index 4f99f0fb..00000000 --- a/src/Inflector.php +++ /dev/null @@ -1,198 +0,0 @@ - [ - 'id', - 'hits', - 'clicks', - ], - ]; - - /** - * Adds inflection regex rules to the inflector. - * - * @param mixed $data A string or an array of strings or regex rules to add. - * @param string $ruleType The rule type: singular | plural | countable - * - * @return void - * - * @since 1.0 - * @throws \InvalidArgumentException - */ - private function addRule($data, string $ruleType) - { - if (\is_string($data)) { - $data = [$data]; - } elseif (!\is_array($data)) { - throw new \InvalidArgumentException('Invalid inflector rule data.'); - } elseif (!\in_array($ruleType, ['singular', 'plural', 'countable'])) { - throw new \InvalidArgumentException('Unsupported rule type.'); - } - - if ($ruleType === 'countable') { - foreach ($data as $rule) { - // Ensure a string is pushed. - array_push(self::$countable['rules'], (string) $rule); - } - } else { - static::rules($ruleType, $data); - } - } - - /** - * Adds a countable word. - * - * @param mixed $data A string or an array of strings to add. - * - * @return $this - * - * @since 1.0 - */ - public function addCountableRule($data) - { - $this->addRule($data, 'countable'); - - return $this; - } - - /** - * Checks if a word is countable. - * - * @param string $word The string input. - * - * @return boolean True if word is countable, false otherwise. - * - * @since 1.0 - */ - public function isCountable($word) - { - return \in_array($word, self::$countable['rules']); - } - - /** - * Checks if a word is in a plural form. - * - * @param string $word The string input. - * - * @return boolean True if word is plural, false if not. - * - * @since 1.0 - */ - public function isPlural($word) - { - return static::pluralize(static::singularize($word)) === $word; - } - - /** - * Checks if a word is in a singular form. - * - * @param string $word The string input. - * - * @return boolean True if word is singular, false if not. - * - * @since 1.0 - */ - public function isSingular($word) - { - return static::singularize($word) === $word; - } - - /** - * Proxy for Inflector::tableize() - */ - public static function tableize(string $word): string - { - $inflector = InflectorFactory::create()->build(); - - return $inflector->tableize($word); - } - - /** - * Proxy for Inflector::classify() - */ - public static function classify(string $word): string - { - $inflector = InflectorFactory::create()->build(); - - return $inflector->classify($word); - } - - /** - * Proxy for Inflector::camelize() - */ - public static function camelize(string $word): string - { - $inflector = InflectorFactory::create()->build(); - - return $inflector->camelize($word); - } - - /** - * Proxy for Inflector::ucwords() - */ - public static function ucwords(string $string, string $delimiters = " \n\t\r\0\x0B-"): string - { - return ucwords($string, $delimiters); - } - - /** - * Empty method to suffice the former interface - */ - public static function reset(): void - { - } - - /** - * Empty method to suffice the former interface - */ - public static function rules(string $type, iterable $rules, bool $reset = false): void - { - } - - /** - * Proxy for Inflector::pluralize() - */ - public static function pluralize(string $word): string - { - $inflector = InflectorFactory::create()->build(); - - return $inflector->pluralize($word); - } - - /** - * Proxy for Inflector::singularize() - */ - public static function singularize(string $word): string - { - $inflector = InflectorFactory::create()->build(); - - return $inflector->singularize($word); - } -}