Completed
Pull Request — master (#71)
by
unknown
01:22
created

Catalog::getHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Sepia\PoParser;
4
5
use Sepia\PoParser\Catalog\Entry;
6
use Sepia\PoParser\Catalog\Header;
7
8
class Catalog
9
{
10
    /** @var Header */
11
    protected $headers;
12
13
    /** @var array */
14
    protected $entries;
15
16
    /**
17
     * @param Entry[] $entries
18
     */
19
    public function __construct(array $entries = array())
20
    {
21
        $this->entries = array();
22
        $this->headers = new Header();
23
        foreach ($entries as $entry) {
24
            $this->addEntry($entry);
25
        }
26
    }
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
    public function addHeaders(Header $headers)
38
    {
39
        $this->headers = $headers;
40
    }
41
42
    /**
43
     * @param string      $msgid
44
     * @param string|null $msgctxt
45
     */
46
    public function removeEntry($msgid, $msgctxt = null)
47
    {
48
        $key = $this->getEntryHash($msgid, $msgctxt);
49
        $this->removeEntryByKey($key);
50
    }
51
52
    public function removeEntryByKey($key)
53
    {
54
        if (isset($this->entries[$key])) {
55
            unset($this->entries[$key]);
56
        }
57
    }
58
59
    /**
60
     * @return array
61
     */
62
    public function getHeaders()
63
    {
64
        return $this->headers->asArray();
65
    }
66
67
    /**
68
     * @return Header
69
     */
70
    public function getHeader()
71
    {
72
        return $this->headers;
73
    }
74
75
    /**
76
     * @return Entry[]
77
     */
78
    public function getEntries()
79
    {
80
        return $this->entries;
81
    }
82
83
    /**
84
     * @param string      $msgId
85
     * @param string|null $context
86
     *
87
     * @return Entry|null
88
     */
89
    public function getEntry($msgId, $context = null)
90
    {
91
        $key = $this->getEntryHash($msgId, $context);
92
        return $this->getEntryByKey($key);
93
    }
94
95
    public function getEntryByKey($key)
96
    {
97
        if (!isset($this->entries[$key])) {
98
            return null;
99
        }
100
        
101
        return $this->entries[$key];
102
    }
103
104
    /**
105
     * @param string      $msgId
106
     * @param string|null $context
107
     *
108
     * @return string
109
     */
110
    private function getEntryHash($msgId, $context = null)
111
    {
112
        return md5($msgId.$context);
113
    }
114
}
115