diff --git a/CHANGELOG.md b/CHANGELOG.md index ffa6f15..00006f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 3.0.1 under development -- no changes in this release. +- Enh #131: Throw `InvalidArgumentException` when missed "one" plural key (@vjik) ## 3.0.0 February 17, 2023 diff --git a/composer.json b/composer.json index bd0616a..49060bb 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ }, "require-dev": { "maglnet/composer-require-checker": "^4.2", - "phpunit/phpunit": "^9.5", + "phpunit/phpunit": "^9.6", "rector/rector": "^1.0.0", "roave/infection-static-analysis-plugin": "^1.25", "spatie/phpunit-watcher": "^1.23", diff --git a/src/SimpleMessageFormatter.php b/src/SimpleMessageFormatter.php index 36d1d93..e6f5538 100644 --- a/src/SimpleMessageFormatter.php +++ b/src/SimpleMessageFormatter.php @@ -83,7 +83,11 @@ private static function pluralize(mixed $value, string $options): string $map[$match] = $pluralMatches[3][$index]; } - if (!in_array(self::PLURAL_OTHER, $pluralMatches[1], true)) { + if (!isset($map[self::PLURAL_ONE])) { + throw new InvalidArgumentException('Missing plural key "' . self::PLURAL_ONE . '".'); + } + + if (!isset($map[self::PLURAL_OTHER])) { throw new InvalidArgumentException('Missing plural key "' . self::PLURAL_OTHER . '".'); } diff --git a/tests/SimpleMessageFormatterTest.php b/tests/SimpleMessageFormatterTest.php index 6a8bd70..f43d076 100644 --- a/tests/SimpleMessageFormatterTest.php +++ b/tests/SimpleMessageFormatterTest.php @@ -100,7 +100,7 @@ public function testFormatPluralWithNonInteger(): void $formatter->format('{min, plural, one{character} other{characters}}', ['min' => 'str']); } - public function testFormatPluralWithMissingKey(): void + public function testFormatPluralWithMissingOtherKey(): void { $formatter = new SimpleMessageFormatter(); @@ -109,6 +109,15 @@ public function testFormatPluralWithMissingKey(): void $formatter->format('{min, plural, one{character}}', ['min' => 1]); } + public function testFormatPluralWithMissingOneKey(): void + { + $formatter = new SimpleMessageFormatter(); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Missing plural key "one".'); + $formatter->format('{min, plural, other{characters}}', ['min' => 1]); + } + public function dataFormatPluralWithMissingKeys(): array { return [