HttpMessageHeader   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 5
dl 0
loc 59
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A addField() 0 4 1
A getField() 0 3 1
A toString() 0 10 2
A importHeaderFieldList() 0 6 2
1
<?php
2
3
/*
4
 * Copyright (c) 2011-2015, Celestino Diaz <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
25
namespace Brickoo\Component\Http;
26
27
use Brickoo\Component\Http\Exception\HeaderNotFoundException;
28
use Brickoo\Component\Common\Container;
29
use Brickoo\Component\Common\Assert;
30
use Brickoo\Component\Validation\Constraint\IsInstanceOfConstraint;
31
use Brickoo\Component\Validation\Validator\ConstraintValidator;
32
33
/**
34
 * HttpMessageHeader
35
 *
36
 * Implements a http message header.
37
 * @author Celestino Diaz <[email protected]>
38
 */
39
class HttpMessageHeader extends Container {
40
41
    use HttpHeaderFieldNameNormalizer;
42
43
    /** @param array $headerFieldList */
44 2
    public function __construct(array $headerFieldList = []) {
45 2
        parent::__construct([], new ConstraintValidator(
46 2
            new IsInstanceOfConstraint("\\Brickoo\\Component\\Http\\HttpHeaderField")
47 2
        ));
48 2
        $this->importHeaderFieldList($headerFieldList);
49 2
    }
50
51
    /**
52
     * Add a header field using the field name as storage key.
53
     * @param \Brickoo\Component\Http\HttpHeaderField $headerField
54
     * @return \Brickoo\Component\Http\HttpMessageHeader
55
     */
56 1
    public function addField(HttpHeaderField $headerField) {
57 1
        $this->set($headerField->getName(), $headerField);
58 1
        return $this;
59
    }
60
61
    /**
62
     * Return the header field by its name.
63
     * @param string $headerFieldName
64
     * @return null|\Brickoo\Component\Http\HttpHeaderField
65
     */
66 1
    public function getField($headerFieldName) {
67 1
        return $this->get($headerFieldName, null);
68
    }
69
70
    /**
71
     * Covert message header fields to a header string.
72
     * @return string
73
     */
74 1
    public function toString() {
75 1
        $headerString = "";
76
77 1
        $headerFields = $this->normalize($this->toArray());
78 1
        foreach ($headerFields as $headerField) {
79 1
            $headerString .= $headerField->toString()."\r\n";
80 1
        }
81
82 1
        return $headerString;
83
    }
84
85
    /**
86
     * Import the header fields from a list.
87
     * @param array $headerFieldList
88
     * @return \Brickoo\Component\Http\HttpMessageHeader
89
     */
90 1
    private function importHeaderFieldList(array $headerFieldList) {
91 1
        foreach ($headerFieldList as $headerField) {
92 1
            $this->addField($headerField);
93 1
        }
94 1
        return $this;
95
    }
96
97
}
98