|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Gettext; |
|
5
|
|
|
|
|
6
|
|
|
use JsonSerializable; |
|
7
|
|
|
use IteratorAggregate; |
|
8
|
|
|
use ArrayIterator; |
|
9
|
|
|
use Countable; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class to manage the headers of translations. |
|
14
|
|
|
*/ |
|
15
|
|
|
class Headers implements JsonSerializable, Countable, IteratorAggregate |
|
16
|
|
|
{ |
|
17
|
|
|
const HEADER_LANGUAGE = 'Language'; |
|
18
|
|
|
const HEADER_PLURAL = 'Plural-Forms'; |
|
19
|
|
|
const HEADER_DOMAIN = 'X-Domain'; |
|
20
|
|
|
|
|
21
|
|
|
protected $headers = []; |
|
22
|
|
|
|
|
23
|
|
|
public function set(string $name, string $value): self |
|
24
|
|
|
{ |
|
25
|
|
|
$this->headers[$name] = trim($value); |
|
26
|
|
|
|
|
27
|
|
|
return $this; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function get(string $name): ?string |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->headers[$name] ?? null; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function delete(string $name): self |
|
36
|
|
|
{ |
|
37
|
|
|
unset($this->headers[$name]); |
|
38
|
|
|
|
|
39
|
|
|
return $this; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function clear(): self |
|
43
|
|
|
{ |
|
44
|
|
|
$this->headers = []; |
|
45
|
|
|
|
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function sort(): self |
|
50
|
|
|
{ |
|
51
|
|
|
ksort($this->headers); |
|
52
|
|
|
|
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function jsonSerialize() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->toArray(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function getIterator() |
|
62
|
|
|
{ |
|
63
|
|
|
return new ArrayIterator($this->toArray()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function count(): int |
|
67
|
|
|
{ |
|
68
|
|
|
return count($this->headers); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function setLanguage(string $language): self |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->set(self::HEADER_LANGUAGE, $language); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function getLanguage(): ?string |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->get(self::HEADER_LANGUAGE); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function setDomain(string $domain): self |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->set(self::HEADER_DOMAIN, $domain); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getDomain(): ?string |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->get(self::HEADER_DOMAIN); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function setPluralForm(int $count, string $rule): self |
|
92
|
|
|
{ |
|
93
|
|
|
if (preg_match('/[a-z]/i', str_replace('n', '', $rule))) { |
|
94
|
|
|
throw new InvalidArgumentException(sprintf('Invalid Plural form: "%s"', $rule)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $this->set(self::HEADER_PLURAL, sprintf('nplurals=%d; plural=%s;', $count, $rule)); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Returns the parsed plural definition. |
|
102
|
|
|
* |
|
103
|
|
|
* @param null|array [count, rule] |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getPluralForm(): ?array |
|
106
|
|
|
{ |
|
107
|
|
|
$header = $this->get(self::HEADER_PLURAL); |
|
108
|
|
|
|
|
109
|
|
|
if (!empty($header) && preg_match('/^nplurals\s*=\s*(\d+)\s*;\s*plural\s*=\s*([^;]+)\s*;$/', $header, $matches)) { |
|
110
|
|
|
return [intval($matches[1]), $matches[2]]; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return null; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
public function toArray(): array |
|
117
|
|
|
{ |
|
118
|
|
|
return $this->headers; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|