Passed
Push — master ( 572884...88a5e5 )
by Chema
01:20 queued 11s
created

MutableTranslationMap::collectionType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
use TypedArrays\AbstractTypedArray;
6
7
require getcwd() . '/vendor/autoload.php';
8
9
final class MutableTranslationMap extends AbstractTypedArray
10
{
11
    protected function typeToEnforce(): string
12
    {
13
        return self::SCALAR_STRING;
14
    }
15
16
    protected function collectionType(): string
17
    {
18
        return self::COLLECTION_TYPE_MAP;
19
    }
20
21
    protected function isNullAllowed(): bool
22
    {
23
        return true;
24
    }
25
}
26
27
function renderTranslations(MutableTranslationMap $translations, string $fallback = 'en'): void
28
{
29
    foreach ($translations as $language => $word) {
30
        $translated = $word ?? $translations[$fallback];
31
        echo "{$language} => {$translated}" . PHP_EOL;
32
    }
33
}
34
35
$translations = new MutableTranslationMap([
36
    'en' => 'hello',
37
    'es' => 'hola',
38
    'fr' => null,
39
]);
40
41
renderTranslations($translations);
42
43
$translations['fr'] = 'salut';
44
$translations['de'] = 'hallo';
45
46
echo '***********' . PHP_EOL;
47
renderTranslations($translations);
48