Translations::loadTranslations()   B
last analyzed

Complexity

Conditions 8
Paths 7

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 29
rs 8.4444
cc 8
nc 7
nop 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * This file is part of the Yasumi package.
5
 *
6
 * Copyright (c) 2015 - 2020 AzuyaLabs
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @author Sacha Telgenhof <[email protected]>
12
 */
13
14
namespace Yasumi;
15
16
use DirectoryIterator;
17
use InvalidArgumentException;
18
use Yasumi\Exception\UnknownLocaleException;
19
20
/**
21
 * Class Translations.
22
 */
23
class Translations implements TranslationsInterface
24
{
25
    /**
26
     * @var array translations array: ['<holiday key>' => ['<locale>' => 'translation', ...], ... ]
27
     */
28
    public $translations = [];
29
30
    /**
31
     * @var array list of all defined locales
32
     */
33
    private $availableLocales;
34
35
    /**
36
     * Constructor.
37
     *
38
     * @param array $availableLocales list of all defined locales
39
     */
40
    public function __construct(array $availableLocales)
41
    {
42
        $this->availableLocales = $availableLocales;
43
    }
44
45
    /**
46
     * Loads translations from directory.
47
     *
48
     * @param string $directoryPath directory path for translation files
49
     *
50
     * @throws UnknownLocaleException
51
     * @throws InvalidArgumentException
52
     */
53
    public function loadTranslations(string $directoryPath): void
54
    {
55
        if (!\file_exists($directoryPath)) {
56
            throw new InvalidArgumentException('Directory with translations not found');
57
        }
58
59
        $directoryPath = \rtrim($directoryPath, '/\\') . DIRECTORY_SEPARATOR;
60
        $extension = 'php';
61
62
        foreach (new DirectoryIterator($directoryPath) as $file) {
63
            if ($file->isDot() || $file->isDir()) {
64
                continue;
65
            }
66
67
            if ($file->getExtension() !== $extension) {
68
                continue;
69
            }
70
71
            $filename = $file->getFilename();
72
            $key = $file->getBasename('.' . $extension);
73
74
            $translations = require $directoryPath . $filename;
75
76
            if (\is_array($translations)) {
77
                foreach (\array_keys($translations) as $locale) {
78
                    $this->isValidLocale($locale); // Validate the given locale
79
                }
80
81
                $this->translations[$key] = $translations;
82
            }
83
        }
84
    }
85
86
    /**
87
     * Checks whether the given locale is a valid/available locale.
88
     *
89
     * @param string $locale locale the locale to be validated
90
     *
91
     * @return true upon success, otherwise an UnknownLocaleException is thrown
92
     *
93
     * @throws UnknownLocaleException An UnknownLocaleException is thrown if the given locale is not
94
     *                                valid/available.
95
     */
96
    protected function isValidLocale(string $locale): bool
97
    {
98
        if (!\in_array($locale, $this->availableLocales, true)) {
99
            throw new UnknownLocaleException(\sprintf('Locale "%s" is not a valid locale.', $locale));
100
        }
101
102
        return true;
103
    }
104
105
    /**
106
     * Adds translation for holiday in specific locale.
107
     *
108
     * @param string $key holiday key
109
110
111
     * @param string $locale locale
112
     * @param string $translation translation
113
     *
114
     * @throws UnknownLocaleException
115
     */
116
    public function addTranslation(string $key, string $locale, string $translation): void
117
    {
118
        $this->isValidLocale($locale); // Validate the given locale
119
120
        if (!\array_key_exists($key, $this->translations)) {
121
            $this->translations[$key] = [];
122
        }
123
124
        $this->translations[$key][$locale] = $translation;
125
    }
126
127
    /**
128
     * Returns translation for holiday in specific locale.
129
     *
130
     * @param string $key holiday key
131
132
133
     * @param string $locale locale
134
     *
135
     * @return string|null translated holiday name
136
     */
137
    public function getTranslation(string $key, string $locale): ?string
138
    {
139
        if (!\array_key_exists($key, $this->translations)
140
            || !\array_key_exists($locale, $this->translations[$key])) {
141
            return null;
142
        }
143
144
        return $this->translations[$key][$locale];
145
    }
146
147
    /**
148
     * Returns all available translations for holiday.
149
     *
150
     * @param string $key holiday key
151
152
153
     *
154
     * @return array holiday name translations ['<locale>' => '<translation>', ...]
155
     */
156
    public function getTranslations(string $key): array
157
    {
158
        if (!\array_key_exists($key, $this->translations)) {
159
            return [];
160
        }
161
162
        return $this->translations[$key];
163
    }
164
}
165