Passed
Pull Request — 4.0 (#55)
by
unknown
02:45
created

LanguagesPartitioned::arrayToEntryAlpha2()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
nc 1
nop 1
dl 0
loc 10
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sokil\IsoCodes\Database;
6
7
use Sokil\IsoCodes\AbstractPartitionedDatabase;
8
use Sokil\IsoCodes\Database\Languages\Language;
9
use Sokil\IsoCodes\Database\LanguagesAlpha2\Language as LanguageAlpha2;
10
11
class LanguagesPartitioned extends AbstractPartitionedDatabase implements LanguagesInterface
12
{
13
    /**
14
     * ISO Standard Number
15
     *
16
     * @psalm-pure
17
     */
18
    public static function getISONumber(): string
19
    {
20
        return '639-3';
21
    }
22
23
    /**
24
     * @param array<string, string> $entry
25
     */
26
    protected function arrayToEntry(array $entry): Language
27
    {
28
        return new Language(
29
            $this->translationDriver,
30
            $entry['name'],
31
            $entry['alpha_3'],
32
            $entry['scope'],
33
            $entry['type'],
34
            !empty($entry['inverted_name']) ? $entry['inverted_name'] : null,
35
            !empty($entry['alpha_2']) ? $entry['alpha_2'] : null
36
        );
37
    }
38
39
    /**
40
     * @param array<string, string> $entry
41
     */
42
    protected function arrayToEntryAlpha2(array $entry): LanguageAlpha2
43
    {
44
        return new LanguageAlpha2(
45
            $this->translationDriver,
46
            $entry['name'],
47
            $entry['alpha_2'],
48
            $entry['alpha_3'],
49
            $entry['scope'],
50
            $entry['type'],
51
            !empty($entry['inverted_name']) ? $entry['inverted_name'] : null
52
        );
53
    }
54
55
    public function getByAlpha2(string $alpha2): ?LanguageAlpha2
56
    {
57
        $language = null;
58
59
        foreach ($this->loadFromJSONFile('/alpha2/' . $alpha2[0]) as $languageRaw) {
60
            if ($languageRaw['alpha_2'] === $alpha2) {
61
                $language = $this->arrayToEntryAlpha2($languageRaw);
62
            }
63
        }
64
65
        return $language;
66
    }
67
68
    public function getByAlpha3(string $alpha3): ?Language
69
    {
70
        $language = null;
71
72
        foreach ($this->loadFromJSONFile('/alpha3/' . substr($alpha3, 0, 2)) as $languageRaw) {
73
            if ($languageRaw['alpha_3'] === $alpha3) {
74
                $language = $this->arrayToEntry($languageRaw);
75
            }
76
        }
77
78
        return $language;
79
    }
80
}
81