From dc6195effa2da0897a4858665036e2aaaca10908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Bia=C5=82czak?= Date: Mon, 3 Aug 2026 13:19:40 +0200 Subject: [PATCH] IBX-11959: Added message template accessors to Translation value objects --- .../Repository/Values/Translation.php | 9 +++ .../Repository/Values/Translation/Message.php | 10 +++ .../Repository/Values/Translation/Plural.php | 13 +++- .../Values/Translation/MessageTest.php | 56 +++++++++------- .../Values/Translation/PluralTest.php | 66 ++++++++++++------- 5 files changed, 106 insertions(+), 48 deletions(-) diff --git a/src/contracts/Repository/Values/Translation.php b/src/contracts/Repository/Values/Translation.php index 45fbc6d4c8..d755ac25bb 100644 --- a/src/contracts/Repository/Values/Translation.php +++ b/src/contracts/Repository/Values/Translation.php @@ -18,4 +18,13 @@ */ abstract class Translation extends ValueObject implements Stringable { + /** + * The message template to translate, with %placeholder% parameters. + */ + abstract public function getMessageTemplate(): string; + + /** + * @return array + */ + abstract public function getValues(): array; } diff --git a/src/contracts/Repository/Values/Translation/Message.php b/src/contracts/Repository/Values/Translation/Message.php index 3db939abbc..cd03126837 100644 --- a/src/contracts/Repository/Values/Translation/Message.php +++ b/src/contracts/Repository/Values/Translation/Message.php @@ -47,6 +47,16 @@ public function __construct(string $message, array $values = []) parent::__construct(); } + public function getMessageTemplate(): string + { + return $this->message; + } + + public function getValues(): array + { + return $this->values; + } + #[\Override] public function __toString(): string { diff --git a/src/contracts/Repository/Values/Translation/Plural.php b/src/contracts/Repository/Values/Translation/Plural.php index 4bfbe2be84..013d5959d3 100644 --- a/src/contracts/Repository/Values/Translation/Plural.php +++ b/src/contracts/Repository/Values/Translation/Plural.php @@ -9,6 +9,7 @@ namespace Ibexa\Contracts\Core\Repository\Values\Translation; use Ibexa\Contracts\Core\Repository\Values\Translation; +use Override; /** * Class for translatable messages, which may contain plural forms. @@ -68,7 +69,17 @@ public function __construct(string $singular, string $plural, array $values) parent::__construct(); } - #[\Override] + public function getMessageTemplate(): string + { + return $this->plural; + } + + public function getValues(): array + { + return $this->values; + } + + #[Override] public function __toString(): string { $firstValue = !empty($this->values) ? current(array_values($this->values)) : null; diff --git a/tests/lib/Repository/Values/Translation/MessageTest.php b/tests/lib/Repository/Values/Translation/MessageTest.php index 757158e2e0..cfd2b52f09 100644 --- a/tests/lib/Repository/Values/Translation/MessageTest.php +++ b/tests/lib/Repository/Values/Translation/MessageTest.php @@ -17,44 +17,56 @@ final class MessageTest extends TestCase { /** - * @dataProvider getDataForTestStringable + * @dataProvider getDataForTestMessage + * + * @param array $values */ - public function testStringable(Message $message, string $expectedString): void + public function testStringable( + string $message, + array $values, + string $expectedString + ): void { + self::assertSame($expectedString, (string)new Message($message, $values)); + } + + /** + * @dataProvider getDataForTestMessage + * + * @param array $values + */ + public function testGetters(string $message, array $values): void { - self::assertSame($expectedString, (string)$message); + $translation = new Message($message, $values); + + self::assertSame($message, $translation->getMessageTemplate()); + self::assertSame($values, $translation->getValues()); } /** - * @return iterable + * @return iterable, string}> */ - public static function getDataForTestStringable(): iterable + public static function getDataForTestMessage(): iterable { yield 'message with substitution values' => [ - new Message( - 'Anna has some oranges in %object%', - [ - '%object%' => 'a basket', - ] - ), + 'Anna has some oranges in %object%', + [ + '%object%' => 'a basket', + ], 'Anna has some oranges in a basket', ]; yield 'message with multiple substitution values' => [ - new Message( - '%first_name% has some data in %storage_type%', - [ - '%first_name%' => 'Anna', - '%storage_type%' => 'her database', - ] - ), + '%first_name% has some data in %storage_type%', + [ + '%first_name%' => 'Anna', + '%storage_type%' => 'her database', + ], 'Anna has some data in her database', ]; yield 'message with no substitution values' => [ - new Message( - 'This value is not correct', - [] - ), + 'This value is not correct', + [], 'This value is not correct', ]; } diff --git a/tests/lib/Repository/Values/Translation/PluralTest.php b/tests/lib/Repository/Values/Translation/PluralTest.php index b1158f0cf7..26acfd14f3 100644 --- a/tests/lib/Repository/Values/Translation/PluralTest.php +++ b/tests/lib/Repository/Values/Translation/PluralTest.php @@ -17,46 +17,62 @@ final class PluralTest extends TestCase { /** - * @dataProvider getDataForTestStringable + * @dataProvider getDataForTestPlural + * + * @param array $values */ - public function testStringable(Plural $message, string $expectedString): void - { - self::assertSame($expectedString, (string)$message); + public function testStringable( + string $singular, + string $plural, + array $values, + string $expectedString + ): void { + self::assertSame($expectedString, (string)new Plural($singular, $plural, $values)); } /** - * @return iterable + * @dataProvider getDataForTestPlural + * + * @param array $values */ - public static function getDataForTestStringable(): iterable + public function testGetters( + string $singular, + string $plural, + array $values + ): void { + $translation = new Plural($singular, $plural, $values); + + self::assertSame($plural, $translation->getMessageTemplate()); + self::assertSame($values, $translation->getValues()); + } + + /** + * @return iterable, string}> + */ + public static function getDataForTestPlural(): iterable { yield 'singular form' => [ - new Plural( - 'John has %apple_count% apple', - 'John has %apple_count% apples', - [ - '%apple_count%' => 1, - ] - ), + 'John has %apple_count% apple', + 'John has %apple_count% apples', + [ + '%apple_count%' => 1, + ], 'John has 1 apple', ]; yield 'plural form' => [ - new Plural( - 'John has %apple_count% apple', - 'John has %apple_count% apples', - [ - '%apple_count%' => 2, - ] - ), + 'John has %apple_count% apple', + 'John has %apple_count% apples', + [ + '%apple_count%' => 2, + ], 'John has 2 apples', ]; yield 'no substitution values' => [ - new Plural( - 'John has some apples', - 'John has a lot of apples', - [] - ), + 'John has some apples', + 'John has a lot of apples', + [], 'John has a lot of apples', ]; }