Test Failed
Push — master ( 3e9160...6b5548 )
by Jairo
02:47
created

HttpFieldCollection::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Author: jairo.rodriguez <[email protected]>
4
 */
5
6
namespace BFunky\HttpParser\Entity;
7
8
9
use BFunky\HttpParser\Exception\HttpFieldNotFoundOnCollection;
10
11
class HttpFieldCollection
12
{
13
    /**
14
     * @var HttpField[]
15
     */
16
    protected $httpFields;
17
18
    /**
19
     * HttpFieldCollection constructor.
20
     * @param HttpField[] $httpFields
21
     */
22 5
    public function __construct(array $httpFields = [])
23
    {
24 5
        $this->httpFields = [];
25 5
        foreach ($httpFields as $httpField) {
26 1
            $this->httpFields[$httpField->getName()] = $httpField;
27
        }
28 5
    }
29
30
    /**
31
     * @param HttpField $obj
32
     */
33
    public function add(HttpField $obj): void
34
    {
35
        $this->httpFields[$obj->getName()] = $obj;
36
    }
37
38
    /**
39
     * @param string $key
40
     * @throws HttpFieldNotFoundOnCollection
41
     */
42 2
    public function delete(string $key): void
43
    {
44 2
        $this->checkKeyExists($key);
45
        unset($this->httpFields[$key]);
46
    }
47
48
    /**
49
     * @param string $key
50
     * @return HttpField
51
     * @throws HttpFieldNotFoundOnCollection
52
     */
53 1
    public function get(string $key): HttpField
54
    {
55 1
        $this->checkKeyExists($key);
56
        return $this->httpFields[$key];
57
    }
58
59
    /**
60
     * @param $key
61
     * @throws HttpFieldNotFoundOnCollection
62
     */
63 3
    private function checkKeyExists($key): void
64
    {
65 3
        if (!array_key_exists($key, $this->httpFields)) {
66 2
            throw new  HttpFieldNotFoundOnCollection('Field ' . $key . ' not found');
67
        }
68 1
    }
69
70
    /**
71
     * @param array $httpFields
72
     * @return self
73
     */
74 2
    public static function fromHttpFieldArray(array $httpFields): self
75
    {
76 2
        return new self($httpFields);
77
    }
78
}