Passed
Push — feature/6 ( be06b0 )
by Raúl
02:00
created

Headers::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
     * @return int
67
     */
68
    public function getPluralFormsCount()
69
    {
70
        if ($this->nPlurals !== null) {
71
            return $this->nPlurals;
72
        }
73
74
        $key = 'Plural-Forms';
75
        if (!$this->has($key)) {
76
            $this->nPlurals = 0;
77
            return $this->nPlurals;
78
        }
79
80
        $header = $this->get('Plural-Forms');
81
82
        $matches = array();
83
        if (preg_match('/nplurals=([0-9]+)/', $header->getValue(), $matches) !== 1) {
84
            $this->nPlurals = 0;
85
            return $this->nPlurals;
86
        }
87
88
        $this->nPlurals = isset($matches[1]) ? (int)$matches[1] : 0;
89
90
        return $this->nPlurals;
91
    }
92
93
    /**
94
     * @param string $headerName
95
     *
96
     * @return string|null
97
     */
98
/*    protected function getHeaderValue($headerName)
99
    {
100
        $header = array_values(array_filter(
101
            $this->headers,
102
            function ($string) use ($headerName) {
103
                return preg_match('/'.$headerName.':(.*)/i', $string) == 1;
104
            }
105
        ));
106
107
        return count($header) ? $header[0] : null;
108
    }
109
*/
110
}
111