Completed
Push — feature/5.0.1 ( 559a15...7b9e0a )
by Raúl
01:17
created

Catalog::addEntry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
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
        if (isset($this->entries[$key])) {
50
            unset($this->entries[$key]);
51
        }
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function getHeaders()
58
    {
59
        return $this->headers->asArray();
60
    }
61
62
    /**
63
     * @return Header
64
     */
65
    public function getHeader()
66
    {
67
        return $this->headers;
68
    }
69
70
    /**
71
     * @return Entry[]
72
     */
73
    public function getEntries()
74
    {
75
        return $this->entries;
76
    }
77
78
    /**
79
     * @param string      $msgId
80
     * @param string|null $context
81
     *
82
     * @return Entry|null
83
     */
84
    public function getEntry($msgId, $context = null)
85
    {
86
        $key = $this->getEntryHash($msgId, $context);
87
        if (!isset($this->entries[$key])) {
88
            return null;
89
        }
90
91
        return $this->entries[$key];
92
    }
93
94
    /**
95
     * @param string      $msgId
96
     * @param string|null $context
97
     *
98
     * @return string
99
     */
100
    private function getEntryHash($msgId, $context = null)
101
    {
102
        return md5($msgId.$context);
103
    }
104
}
105