Completed
Pull Request — master (#26)
by Christian
02:45
created

LanguageMap   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 0
dl 0
loc 107
rs 10
c 0
b 0
f 0

8 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
B 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
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
     * @param array $map
30
     *
31
     * @return self
32
     */
33
    public static function create(array $map)
34
    {
35
        $languageMap = new self();
36
        $languageMap->map = $map;
37
38
        return $languageMap;
39
    }
40
41
    /**
42
     * Creates a new language map based on the current map including the given entry.
43
     *
44
     * An existing entry will be overridden if the given language tag is already
45
     * present in this language map.
46
     *
47
     * @param string $languageTag
48
     * @param string $value
49
     *
50
     * @return self
51
     */
52
    public function withEntry($languageTag, $value)
53
    {
54
        $languageMap = clone $this;
55
        $languageMap->map[$languageTag] = $value;
56
57
        return $languageMap;
58
    }
59
60
    /**
61
     * Returns an unordered list of all language tags being used as keys
62
     * in this language map.
63
     *
64
     * @return string[]
65
     */
66
    public function languageTags()
67
    {
68
        return array_keys($this->map);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function offsetExists($languageTag)
75
    {
76
        return isset($this->map[$languageTag]);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function offsetGet($languageTag)
83
    {
84
        if (!isset($this->map[$languageTag])) {
85
            throw new \InvalidArgumentException(sprintf('The requested language tag "%s" does not exist in this language map.', $languageTag));
86
        }
87
88
        return $this->map[$languageTag];
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function offsetSet($languageTag, $value)
95
    {
96
        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.');
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function offsetUnset($languageTag)
103
    {
104
        throw new \LogicException('Entries of a language map cannot be removed.');
105
    }
106
107
    public function equals(LanguageMap $languageMap)
108
    {
109
        if (count($this->map) !== count($languageMap->map)) {
110
            return false;
111
        }
112
113
        foreach ($this->map as $languageTag => $value) {
114
            if (!isset($languageMap[$languageTag])) {
115
                return false;
116
            }
117
118
            if ($value !== $languageMap[$languageTag]) {
119
                return false;
120
            }
121
        }
122
123
        return true;
124
    }
125
}
126