Completed
Push — master ( b39607...5d3f42 )
by Raúl
9s
created

Header::asArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sepia\PoParser\Catalog;
4
5
class Header
6
{
7
    /** @var array */
8
    protected $headers;
9
10
    /** @var int|null */
11
    protected $nPlurals;
12
13
    public function __construct(array $headers = array())
14
    {
15
        $this->setHeaders($headers);
16
    }
17
18
    public function getPluralFormsCount()
19
    {
20
        if ($this->nPlurals !== null) {
21
            return $this->nPlurals;
22
        }
23
24
        $header = $this->getHeaderValue('Plural-Forms');
25
        if ($header === null) {
26
            $this->nPlurals = 0;
27
            return $this->nPlurals;
28
        }
29
30
        $matches = array();
31
        if (preg_match('/nplurals=([0-9]+)/', $header, $matches) !== 1) {
32
            $this->nPlurals = 0;
33
            return $this->nPlurals;
34
        }
35
36
        $this->nPlurals = isset($matches[1]) ? (int)$matches[1] : 0;
37
38
        return $this->nPlurals;
39
    }
40
41
    public function setHeaders(array $headers)
42
    {
43
        $this->headers = $headers;
44
    }
45
46
    /**
47
     * @return array
48
     */
49
    public function asArray()
50
    {
51
        return $this->headers;
52
    }
53
54
    /**
55
     * @param string $headerName
56
     *
57
     * @return string|null
58
     */
59
    protected function getHeaderValue($headerName)
60
    {
61
        $header = array_values(array_filter(
62
            $this->headers,
63
            function ($string) use ($headerName) {
64
                return preg_match('/'.$headerName.':(.*)/i', $string) == 1;
65
            }
66
        ));
67
68
        return count($header) ? $header[0] : null;
69
    }
70
}
71