1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sepia\PoParser\Catalog; |
4
|
|
|
|
5
|
|
|
class Headers |
6
|
|
|
{ |
7
|
|
|
/** @var Header[] */ |
8
|
|
|
protected $headers; |
9
|
|
|
|
10
|
|
|
/** @var int|null */ |
11
|
|
|
protected $nPlurals; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Headers constructor. |
15
|
|
|
* |
16
|
|
|
* @param Header[] $headers |
17
|
|
|
*/ |
18
|
|
|
public function __construct(array $headers = array()) |
19
|
|
|
{ |
20
|
|
|
foreach ($headers as $header) { |
21
|
|
|
$this->headers[$header->getId()] = $header; |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $headerKey |
27
|
|
|
* |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
|
|
public function has($headerKey) |
31
|
|
|
{ |
32
|
|
|
return isset($this->headers[$headerKey]); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $headerKey |
37
|
|
|
* |
38
|
|
|
* @return Header|null |
39
|
|
|
*/ |
40
|
|
|
public function get($headerKey) |
41
|
|
|
{ |
42
|
|
|
if (!$this->has($headerKey)) { |
43
|
|
|
return null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $this->headers[$headerKey]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return Header[] |
51
|
|
|
*/ |
52
|
|
|
public function all() |
53
|
|
|
{ |
54
|
|
|
return $this->headers; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Header $header |
59
|
|
|
*/ |
60
|
|
|
public function set(Header $header) |
61
|
|
|
{ |
62
|
|
|
$this->headers[$header->getId()] = $header; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Remove a Header. |
67
|
|
|
* |
68
|
|
|
* @param string $headerKey |
69
|
|
|
*/ |
70
|
|
|
public function remove($headerKey) |
71
|
|
|
{ |
72
|
|
|
if (!$this->has($headerKey)) { |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
unset($this->headers[$headerKey]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return int |
81
|
|
|
*/ |
82
|
|
|
public function getPluralFormsCount() |
83
|
|
|
{ |
84
|
|
|
if ($this->nPlurals !== null) { |
85
|
|
|
return $this->nPlurals; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$key = 'Plural-Forms'; |
89
|
|
|
if (!$this->has($key)) { |
90
|
|
|
$this->nPlurals = 0; |
91
|
|
|
return $this->nPlurals; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$header = $this->get('Plural-Forms'); |
95
|
|
|
|
96
|
|
|
$matches = array(); |
97
|
|
|
if (preg_match('/nplurals=([0-9]+)/', $header->getValue(), $matches) !== 1) { |
98
|
|
|
$this->nPlurals = 0; |
99
|
|
|
return $this->nPlurals; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$this->nPlurals = isset($matches[1]) ? (int)$matches[1] : 0; |
103
|
|
|
|
104
|
|
|
return $this->nPlurals; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $headerName |
109
|
|
|
* |
110
|
|
|
* @return string|null |
111
|
|
|
*/ |
112
|
|
|
/* protected function getHeaderValue($headerName) |
113
|
|
|
{ |
114
|
|
|
$header = array_values(array_filter( |
115
|
|
|
$this->headers, |
116
|
|
|
function ($string) use ($headerName) { |
117
|
|
|
return preg_match('/'.$headerName.':(.*)/i', $string) == 1; |
118
|
|
|
} |
119
|
|
|
)); |
120
|
|
|
|
121
|
|
|
return count($header) ? $header[0] : null; |
122
|
|
|
} |
123
|
|
|
*/ |
124
|
|
|
} |
125
|
|
|
|