Skip to content

Commit

Permalink
Add write method to category source (#95)
Browse files Browse the repository at this point in the history
* Add write method to category source

* Apply fixes from StyleCI

* [ci-review] Apply changes from Rector action.

* Add changelog

* Add BC layer

* Add test

* Apply fixes from StyleCI

* Resort arguments and add a test

* Fix psalm

* Apply review suggestion

* Check exception code

* Update src/CategorySource.php

Co-authored-by: Rustam Mamadaminov <rmamdaminov@gmail.com>

Co-authored-by: StyleCI Bot <bot@styleci.io>
Co-authored-by: rector-bot <rector@yiiframework.com>
Co-authored-by: Rustam Mamadaminov <rmamdaminov@gmail.com>
Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
  • Loading branch information
5 people committed Nov 27, 2022
1 parent 7c8e7d0 commit d555136
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## 2.2.0 under development

- New #94: Add `getMessages()` method to `CategorySource` (@xepozz)
- New #95: Add `write()` method to `CategorySource` (@xepozz)

## 2.1.1 November 23, 2022

Expand Down
1 change: 1 addition & 0 deletions config/common.php
Expand Up @@ -19,6 +19,7 @@
// return new \Yiisoft\Translator\CategorySource(
// $params['yiisoft/translator']['defaultCategory'],
// $messageSource,
// $messageSource,
// $messageFormatter,
// );
// },
Expand Down
37 changes: 36 additions & 1 deletion src/CategorySource.php
Expand Up @@ -17,15 +17,18 @@ final class CategorySource
* @param string $name Category name.
* @param MessageReaderInterface $reader Message reader to get messages from for this category.
* @param MessageFormatterInterface|null $formatter Message formatter to format messages with for this category.
* @param MessageWriterInterface|null $writer Message writer to write messages for this category.
*/
public function __construct(
string $name,
private MessageReaderInterface $reader,
private ?MessageFormatterInterface $formatter = null
private ?MessageFormatterInterface $formatter = null,
private ?MessageWriterInterface $writer = null,
) {
if (!preg_match('/^[a-z0-9_-]+$/i', $name)) {
throw new RuntimeException('Category name is invalid. Only letters and numbers are allowed.');
}

$this->name = $name;
}

Expand Down Expand Up @@ -80,6 +83,38 @@ public function getMessages(string $locale): array
return $this->reader->getMessages($this->name, $locale);
}

/**
* Writes a set of messages for a specified category and locale.
*
* @psalm-param array<string, array<string, string>> $messages
*
* @param string $locale Locale to write messages for.
* @param array $messages A set of messages to write. The format is the following:
* ```php
* [
* 'key1' => [
* 'message' => 'translation1',
* // Extra metadata that writer may use:
* 'comment' => 'Translate carefully!',
* ],
* 'key2' => [
* 'message' => 'translation2',
* // Extra metadata that writer may use:
* 'comment' => 'Translate carefully!',
* ],
* ]
* ```
*
* @throws UnwritableCategorySourceException When $write is not configured, or it's impossible to write the messages into the source.
*/
public function write(string $locale, array $messages): void
{
if ($this->writer === null) {
throw new UnwritableCategorySourceException($this->name);
}
$this->writer->write($this->name, $locale, $messages);
}

/**
* Format the message given parameters and locale.
*
Expand Down
20 changes: 20 additions & 0 deletions src/UnwritableCategorySourceException.php
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Translator;

use Exception;
use Throwable;

final class UnwritableCategorySourceException extends Exception
{
public function __construct(string $categoryName, int $code = 0, ?Throwable $previous = null)
{
parent::__construct(
sprintf('The category source "%s" does not support writing.', $categoryName),
$code,
$previous
);
}
}
29 changes: 29 additions & 0 deletions tests/CategoryTest.php
Expand Up @@ -9,8 +9,10 @@
use Yiisoft\Translator\CategorySource;
use Yiisoft\Translator\MessageFormatterInterface;
use Yiisoft\Translator\MessageReaderInterface;
use Yiisoft\Translator\MessageWriterInterface;
use Yiisoft\Translator\NullMessageFormatter;
use Yiisoft\Translator\SimpleMessageFormatter;
use Yiisoft\Translator\UnwritableCategorySourceException;

final class CategoryTest extends TestCase
{
Expand All @@ -34,6 +36,33 @@ public function testNameException(): void
);
}

public function testWriterAbsence(): void
{
$category = new CategorySource(
'app',
$this->createMessageReader(),
$this->createMessageFormatter(),
);

$this->expectException(UnwritableCategorySourceException::class);
$this->expectExceptionCode(0);
$this->expectExceptionMessage('The category source "app" does not support writing.');
$category->write('en', []);
}

public function testWriterAvailable(): void
{
$category = new CategorySource(
'app',
$this->createMessageReader(),
$this->createMessageFormatter(),
$this->createMock(MessageWriterInterface::class)
);

$category->write('en', []);
$this->expectNotToPerformAssertions();
}

public function dataWithoutFormatter(): array
{
return [
Expand Down

0 comments on commit d555136

Please sign in to comment.