Completed
Push — master ( a6fe0f...b0c0c1 )
by Sebastian
02:05
created

HeaderCollection::addHeader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace DjThossi\SmokeTestingPhp\Collection;
3
4
use DjThossi\SmokeTestingPhp\ValueObject\Header;
5
use DjThossi\SmokeTestingPhp\ValueObject\HeaderKey;
6
7
class HeaderCollection extends BaseCollection
8
{
9
    /**
10
     * @param array $headerData
11
     *
12
     * @return HeaderCollection
13
     */
14
    public static function fromArray(array $headerData)
15
    {
16
        $headerCollection = new self();
17
        foreach ($headerData as $key => $value) {
18
            $header = Header::fromPrimitives($key, $value);
19
            $headerCollection->addHeader($header);
20
        }
21
22
        return $headerCollection;
23
    }
24
25
    /**
26
     * @param Header $header
27
     */
28
    public function addHeader(Header $header)
29
    {
30
        $this->addElement($header);
31
    }
32
33
    /**
34
     * @return Header
35
     */
36
    public function current()
37
    {
38
        return $this->getCurrent();
39
    }
40
41
    /**
42
     * @param HeaderKey $searchKey
43
     *
44
     * @throws HeaderNotFoundException
45
     *
46
     * @return Header
47
     */
48
    public function getHeader(HeaderKey $searchKey)
49
    {
50
        foreach ($this as $header) {
51
            if ($searchKey->asString() === $header->getKey()->asString()) {
52
                return $header;
53
            }
54
        }
55
56
        throw new HeaderNotFoundException($searchKey);
57
    }
58
59
    /**
60
     * @param HeaderKey $searchKey
61
     *
62
     * @return bool
63
     */
64
    public function headerKeyExists(HeaderKey $searchKey)
65
    {
66
        try {
67
            return $this->getHeader($searchKey) !== null;
68
        } catch (HeaderNotFoundException $exception) {
69
            return false;
70
        }
71
    }
72
73
    /**
74
     * @param Header $searchHeader
75
     *
76
     * @return bool
77
     */
78
    public function headerExists(Header $searchHeader)
79
    {
80
        try {
81
            $header = $this->getHeader($searchHeader->getKey());
82
83
            return $header->getValue()->asString() === $searchHeader->getValue()->asString();
84
        } catch (HeaderNotFoundException $exception) {
85
            return false;
86
        }
87
    }
88
}
89