Skip to content

Commit

Permalink
Throw InvalidArgumentException when missed "one" plural key
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Feb 12, 2024
1 parent 38c1117 commit f42dc21
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion src/SimpleMessageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 . '".');
}

Expand Down
11 changes: 10 additions & 1 deletion tests/SimpleMessageFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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 [
Expand Down

0 comments on commit f42dc21

Please sign in to comment.