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
|
|
|
* @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 count() |
108
|
|
|
{ |
109
|
|
|
return count($this->map); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function equals(LanguageMap $languageMap) |
113
|
|
|
{ |
114
|
|
|
if (count($this->map) !== count($languageMap->map)) { |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
foreach ($this->map as $languageTag => $value) { |
119
|
|
|
if (!isset($languageMap[$languageTag])) { |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($value !== $languageMap[$languageTag]) { |
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return true; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|