LanguageMap   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 0
dl 0
loc 103
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 7 1
A withEntry() 0 7 1
A languageTags() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 8 2
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A count() 0 4 1
A equals() 0 18 5
1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Xabbuh\XApi\Model;
13
14
/**
15
 * Read-only dictionary mapping RFC 5646 language tags to translated strings.
16
 *
17
 * @author Christian Flothmann <[email protected]>
18
 */
19
final class LanguageMap implements \ArrayAccess, \Countable
20
{
21
    private $map;
22
23
    /**
24
     * Creates a language map from the given dictionary.
25
     *
26
     * Keys are RFC 5646 language tags and each value is a string in the
27
     * language specified by the key.
28
     */
29
    public static function create(array $map): self
30
    {
31
        $languageMap = new self();
32
        $languageMap->map = $map;
33
34
        return $languageMap;
35
    }
36
37
    /**
38
     * Creates a new language map based on the current map including the given entry.
39
     *
40
     * An existing entry will be overridden if the given language tag is already
41
     * present in this language map.
42
     */
43
    public function withEntry(string $languageTag, string $value): self
44
    {
45
        $languageMap = clone $this;
46
        $languageMap->map[$languageTag] = $value;
47
48
        return $languageMap;
49
    }
50
51
    /**
52
     * Returns an unordered list of all language tags being used as keys
53
     * in this language map.
54
     *
55
     * @return string[]
56
     */
57
    public function languageTags(): array
58
    {
59
        return array_keys($this->map);
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function offsetExists($languageTag): bool
66
    {
67
        return isset($this->map[$languageTag]);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function offsetGet($languageTag): string
74
    {
75
        if (!isset($this->map[$languageTag])) {
76
            throw new \InvalidArgumentException(sprintf('The requested language tag "%s" does not exist in this language map.', $languageTag));
77
        }
78
79
        return $this->map[$languageTag];
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function offsetSet($languageTag, $value): void
86
    {
87
        throw new \LogicException('The values of a language map cannot be modified. Use withEntry() instead to retrieve a new language map with the added or modified value.');
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function offsetUnset($languageTag): void
94
    {
95
        throw new \LogicException('Entries of a language map cannot be removed.');
96
    }
97
98
    public function count(): int
99
    {
100
        return count($this->map);
101
    }
102
103
    public function equals(LanguageMap $languageMap): bool
104
    {
105
        if (count($this->map) !== count($languageMap->map)) {
106
            return false;
107
        }
108
109
        foreach ($this->map as $languageTag => $value) {
110
            if (!isset($languageMap[$languageTag])) {
111
                return false;
112
            }
113
114
            if ($value !== $languageMap[$languageTag]) {
115
                return false;
116
            }
117
        }
118
119
        return true;
120
    }
121
}
122