CategorySource   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 7
dl 0
loc 51
ccs 11
cts 11
cp 1
rs 10
c 1
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A writeMessages() 0 3 1
A getName() 0 3 1
A readMessages() 0 3 1
A __construct() 0 8 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\TranslatorExtractor;
6
7
use RuntimeException;
8
use Yiisoft\Translator\MessageReaderInterface;
9
use Yiisoft\Translator\MessageWriterInterface;
10
11
/**
12
 * Represents message category for reading and writing messages.
13
 */
14
final class CategorySource
15
{
16
    /**
17
     * @param string $name Category name.
18
     */
19 22
    public function __construct(
20
        private string $name,
21
        private MessageReaderInterface $reader,
22
        private MessageWriterInterface $writer
23
    ) {
24 22
        if (!preg_match('/^[a-z0-9_-]+$/i', $name)) {
25 1
            throw new RuntimeException(
26 1
                'Category name is invalid. Only letters, numbers, dash, and underscore are allowed.'
27 1
            );
28
        }
29
    }
30
31
    /**
32
     * @return string Category name.
33
     */
34 18
    public function getName(): string
35
    {
36 18
        return $this->name;
37
    }
38
39
    /**
40
     * @param string $category Category of messages to get.
41
     * @param string $locale Locale of messages to get.
42
     *
43
     * @psalm-return array<string, array<string, string>>
44
     *
45
     * @return array All messages from category. The format is
46
     * the same as in {@see \Yiisoft\Translator\MessageReaderInterface::getMessages()}.
47
     */
48 14
    public function readMessages(string $category, string $locale): array
49
    {
50 14
        return $this->reader->getMessages($category, $locale);
51
    }
52
53
    /**
54
     * @param string $category Category to write messages to.
55
     * @param string $locale Locale to write messages for.
56
     * @param array $messages Messages to write.
57
     *
58
     * @psalm-param array<string, array<string, string>> $messages
59
     *
60
     * @see \Yiisoft\Translator\MessageWriterInterface::write
61
     */
62 11
    public function writeMessages(string $category, string $locale, array $messages): void
63
    {
64 11
        $this->writer->write($category, $locale, $messages);
65
    }
66
}
67