Test Failed
Push — master ( 9400f7...aee647 )
by Jairo
02:19
created

HttpFieldCollection::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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