Passed
Pull Request — master (#95)
by Dmitriy
03:12 queued 35s
created

CategorySource::write()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 10
cc 2
nc 2
nop 2
crap 2.0625
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Translator;
6
7
use RuntimeException;
8
9
/**
10
 * Represents message category.
11
 */
12
final class CategorySource
13
{
14
    private string $name;
15
16
    /**
17
     * @param string $name Category name.
18
     * @param MessageReaderInterface $reader Message reader to get messages from for this category.
19
     * @param MessageFormatterInterface|null $formatter Message formatter to format messages with for this category.
20
     * @param MessageWriterInterface|null $writer Message writer to write messages for this category.
21
     */
22 59
    public function __construct(
23
        string $name,
24
        private MessageReaderInterface $reader,
25
        private ?MessageFormatterInterface $formatter = null,
26
        private null|MessageWriterInterface $writer = null,
27
    ) {
28 59
        if (!preg_match('/^[a-z0-9_-]+$/i', $name)) {
29 1
            throw new RuntimeException('Category name is invalid. Only letters and numbers are allowed.');
30
        }
31
32 58
        $this->name = $name;
33
    }
34
35
    /**
36
     * @return string Category name.
37
     */
38 56
    public function getName(): string
39
    {
40 56
        return $this->name;
41
    }
42
43
    /**
44
     * Get a message with ID, locale and parameters specified.
45
     *
46
     * @param string $id Message ID.
47
     * @param string $locale Locale to get message for.
48
     * @param array $parameters Message parameters.
49
     *
50
     * @return string|null Message string or null if message was not found.
51
     */
52 53
    public function getMessage(string $id, string $locale, array $parameters = []): ?string
53
    {
54 53
        return $this->reader->getMessage($id, $this->name, $locale, $parameters);
55
    }
56
57
    /**
58
     * Writes a set of messages for a specified category and locale.
59
     *
60
     * @psalm-param array<string, array<string, string>> $messages
61
     *
62
     * @param array $messages A set of messages to write. The format is the following:
63
     *
64
     * ```php
65
     * [
66
     *   'key1' => [
67
     *     'message' => 'translation1',
68
     *     // Extra metadata that writer may use:
69
     *     'comment' => 'Translate carefully!',
70
     *   ],
71
     *   'key2' => [
72
     *     'message' => 'translation2',
73
     *     // Extra metadata that writer may use:
74
     *     'comment' => 'Translate carefully!',
75
     *   ],
76
     * ]
77
     * ```
78
     * @param string $locale Locale to write messages for.
79
     *
80
     * @throws UnwritableCategorySourceException When $write is not configured or it's impossible to write the messages into the source.
81
     */
82 1
    public function write(array $messages, string $locale): void
83
    {
84 1
        if ($this->writer === null) {
85 1
            throw new UnwritableCategorySourceException($this->name);
86
        }
87
        $this->writer->write($this->name, $locale, $messages);
88
    }
89
90
    /**
91
     * Format the message given parameters and locale.
92
     *
93
     * @param string $message Message to be formatted.
94
     * @param array $parameters Parameters to use.
95
     * @param string $locale Locale to use. Usually affects formatting numbers, dates etc.
96
     * @param MessageFormatterInterface $defaultFormatter Message formatter that will be used if formatter not specified
97
     * in message category.
98
     *
99
     * @return string Formatted message.
100
     */
101 55
    public function format(
102
        string $message,
103
        array $parameters,
104
        string $locale,
105
        MessageFormatterInterface $defaultFormatter
106
    ): string {
107 55
        return ($this->formatter ?? $defaultFormatter)->format($message, $parameters, $locale);
108
    }
109
}
110