Completed
Push — develop ( b39607 )
by Raúl
16s
created

Header::getPluralFormsCount()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.6737
c 0
b 0
f 0
cc 5
eloc 13
nc 5
nop 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 = null;
12
13
    public function __construct(array $headers = array())
14
    {
15
        $this->headers = $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
    /**
42
     * @return array
43
     */
44
    public function asArray()
45
    {
46
        return $this->headers;
47
    }
48
49
    /**
50
     * @param string $headerName
51
     *
52
     * @return string|null
53
     */
54
    protected function getHeaderValue($headerName)
55
    {
56
        $header = array_values(array_filter(
57
            $this->headers,
58
            function ($string) use ($headerName) {
59
                return preg_match('/'.$headerName.':(.*)/i', $string) == 1;
60
            }
61
        ));
62
63
        return count($header) ? $header[0] : null;
64
    }
65
}
66