Header   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 23
c 2
b 0
f 0
dl 0
loc 64
rs 10
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeaderValue() 0 10 2
A getPluralFormsCount() 0 21 5
A __construct() 0 3 1
A asArray() 0 3 1
A setHeaders() 0 3 1
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