Passed
Push — master ( 9b394e...3e9160 )
by Jairo
02:14
created

HttpFieldCollection::fromHttpFieldArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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