1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sepia\PoParser\Catalog; |
4
|
|
|
|
5
|
|
|
class CatalogArray implements Catalog |
6
|
|
|
{ |
7
|
|
|
/** @var Header */ |
8
|
|
|
protected $headers; |
9
|
|
|
|
10
|
|
|
/** @var array */ |
11
|
|
|
protected $entries; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param Entry[] $entries |
15
|
|
|
*/ |
16
|
|
|
public function __construct(array $entries = array()) |
17
|
|
|
{ |
18
|
|
|
$this->entries = array(); |
19
|
|
|
$this->headers = new Header(); |
20
|
|
|
foreach ($entries as $entry) { |
21
|
|
|
$this->addEntry($entry); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function addEntry(Entry $entry) |
29
|
|
|
{ |
30
|
|
|
$key = $this->getEntryHash( |
31
|
|
|
$entry->getMsgId(), |
32
|
|
|
$entry->getMsgCtxt() |
33
|
|
|
); |
34
|
|
|
$this->entries[$key] = $entry; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function addHeaders(Header $headers) |
41
|
|
|
{ |
42
|
|
|
$this->headers = $headers; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
|
|
public function removeEntry($msgid, $msgctxt = null) |
49
|
|
|
{ |
50
|
|
|
$key = $this->getEntryHash($msgid, $msgctxt); |
51
|
|
|
if (isset($this->entries[$key])) { |
52
|
|
|
unset($this->entries[$key]); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function getHeaders() |
60
|
|
|
{ |
61
|
|
|
return $this->headers->asArray(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function getHeader() |
68
|
|
|
{ |
69
|
|
|
return $this->headers; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function getEntries() |
76
|
|
|
{ |
77
|
|
|
return $this->entries; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function getEntry($msgId, $context = null) |
84
|
|
|
{ |
85
|
|
|
$key = $this->getEntryHash($msgId, $context); |
86
|
|
|
if (!isset($this->entries[$key])) { |
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $this->entries[$key]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $msgId |
95
|
|
|
* @param string|null $context |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
private function getEntryHash($msgId, $context = null) |
100
|
|
|
{ |
101
|
|
|
return \md5($msgId.$context); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|