Completed
Pull Request — master (#68)
by Raúl
02:32 queued 01:17
created

Catalog::getEntries()   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
7
class Catalog
8
{
9
    /** @var array */
10
    protected $headers;
11
12
    /** @var array */
13
    protected $entries;
14
15
    /**
16
     * @param Entry[] $entries
17
     */
18
    public function __construct(array $entries = array())
19
    {
20
        foreach ($entries as $entry) {
21
            $this->addEntry($entry);
22
        }
23
    }
24
25
    public function addEntry(Entry $entry)
26
    {
27
        $key = $this->getEntryHash(
28
            $entry->getMsgId(),
29
            $entry->getMsgCtxt()
30
        );
31
        $this->entries[$key] = $entry;
32
    }
33
34
    public function addHeaders(array $headers)
35
    {
36
        $this->headers = $headers;
37
    }
38
39
    /**
40
     * @return array
41
     */
42
    public function getHeaders()
43
    {
44
        return $this->headers;
45
    }
46
47
    /**
48
     * @return Entry[]
49
     */
50
    public function getEntries()
51
    {
52
        return $this->entries;
53
    }
54
55
    /**
56
     * @param string      $msgId
57
     * @param string|null $context
58
     *
59
     * @return Entry|null
60
     */
61
    public function getEntry($msgId, $context = null)
62
    {
63
        $key = $this->getEntryHash($msgId, $context);
64
        if (!isset($this->entries[$key])) {
65
            return null;
66
        }
67
68
        return $this->entries[$key];
69
    }
70
71
    /**
72
     * @param string      $msgId
73
     * @param string|null $context
74
     *
75
     * @return string
76
     */
77
    private function getEntryHash($msgId, $context = null)
78
    {
79
        return md5($msgId.$context);
80
    }
81
}
82