Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/contracts/Repository/Values/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, scalar|null>
*/
abstract public function getValues(): array;
}
10 changes: 10 additions & 0 deletions src/contracts/Repository/Values/Translation/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
13 changes: 12 additions & 1 deletion src/contracts/Repository/Values/Translation/Plural.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down
56 changes: 34 additions & 22 deletions tests/lib/Repository/Values/Translation/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,56 @@
final class MessageTest extends TestCase
{
/**
* @dataProvider getDataForTestStringable
* @dataProvider getDataForTestMessage
*
* @param array<string, scalar|null> $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<string, scalar|null> $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<string, array{\Ibexa\Contracts\Core\Repository\Values\Translation\Message, string}>
* @return iterable<string, array{string, array<string, scalar|null>, 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',
];
}
Expand Down
66 changes: 41 additions & 25 deletions tests/lib/Repository/Values/Translation/PluralTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,62 @@
final class PluralTest extends TestCase
{
/**
* @dataProvider getDataForTestStringable
* @dataProvider getDataForTestPlural
*
* @param array<string, scalar|null> $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<string, array{\Ibexa\Contracts\Core\Repository\Values\Translation\Plural, string}>
* @dataProvider getDataForTestPlural
*
* @param array<string, scalar|null> $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, array{string, string, array<string, scalar|null>, 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',
];
}
Expand Down
Loading