1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Gettext; |
4
|
|
|
|
5
|
|
|
use Gettext\Languages\Language; |
6
|
|
|
use BadMethodCallException; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use IteratorAggregate; |
9
|
|
|
use ArrayIterator; |
10
|
|
|
use Countable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class to manage a collection of translations under the same domain. |
14
|
|
|
*/ |
15
|
|
|
class Translations implements Countable, IteratorAggregate |
16
|
|
|
{ |
17
|
|
|
protected $translations = []; |
18
|
|
|
protected $headers; |
19
|
|
|
|
20
|
|
|
public function __construct(string $domain = null) |
21
|
|
|
{ |
22
|
|
|
$this->headers = new Headers(); |
23
|
|
|
|
24
|
|
|
if (isset($domain)) { |
25
|
|
|
$this->setDomain($domain); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function __clone() |
30
|
|
|
{ |
31
|
|
|
foreach ($this->translations as $id => $translation) { |
32
|
|
|
$this->translations[$id] = clone $translation; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->headers = clone $this->headers; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getIterator() |
39
|
|
|
{ |
40
|
|
|
return new ArrayIterator($this->toArray()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function count(): int |
44
|
|
|
{ |
45
|
|
|
return count($this->translations); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function getHeaders(): Headers |
49
|
|
|
{ |
50
|
|
|
return $this->headers; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function add(Translation $translation): self |
54
|
|
|
{ |
55
|
|
|
$id = $translation->getId(); |
56
|
|
|
|
57
|
|
|
$this->translations[$id] = $translation; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function remove(Translation $translation): self |
63
|
|
|
{ |
64
|
|
|
$key = array_search($translation, $this->translations); |
65
|
|
|
|
66
|
|
|
if ($key !== false) { |
67
|
|
|
unset($this->translations[$key]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function setDomain(string $domain): self |
74
|
|
|
{ |
75
|
|
|
$this->getHeaders()->setDomain($domain); |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getDomain(): ?string |
81
|
|
|
{ |
82
|
|
|
return $this->getHeaders()->getDomain(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function setLanguage(string $language): self |
86
|
|
|
{ |
87
|
|
|
$info = Language::getById($language); |
88
|
|
|
|
89
|
|
|
if (empty($info)) { |
90
|
|
|
throw new InvalidArgumentException(sprintf('The language "%s" is not valid', $language)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->getHeaders() |
94
|
|
|
->setLanguage($language) |
95
|
|
|
->setPluralForms(count($info->categories), $info->formula); |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getLanguage(): ?string |
101
|
|
|
{ |
102
|
|
|
return $this->getHeaders()->getLanguage(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function find(?string $context, string $original): ?Translation |
106
|
|
|
{ |
107
|
|
|
foreach ($this->translations as $translation) { |
108
|
|
|
if ($translation->getContext() === $context && $translation->getOriginal() === $original) { |
109
|
|
|
return $translation; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return null; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function toArray(): array |
117
|
|
|
{ |
118
|
|
|
return array_values($this->translations); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|