AbstractHeader::set()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 7
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
/*
3
 * This file is part of the Borobudur-Http package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Http\Header;
12
13
/**
14
 * @author      Iqbal Maulana <[email protected]>
15
 * @created     8/3/15
16
 */
17
abstract class AbstractHeader implements HeaderInterface
18
{
19
    use HeaderFactoryTrait;
20
21
    /**
22
     * @var string
23
     */
24
    private $value;
25
26
    /**
27
     * Constructor.
28
     *
29
     * @param string|null $value
30
     */
31
    public function __construct($value = null)
32
    {
33
        GenericHeader::assertEmptyHeaderFieldName(static::$headerName);
34
35
        if (null !== $value) {
36
            $this->set($value);
37
        }
38
    }
39
40
    /**
41
     * Set header value.
42
     *
43
     * @param string $value
44
     *
45
     * @return $this
46
     */
47
    public function set($value)
48
    {
49
        GenericHeader::assertHeaderValue($value);
50
        $this->value = $value;
51
52
        return $this;
53
    }
54
55
    /**
56
     * Get header field name.
57
     *
58
     * @return string
59
     */
60
    public function getFieldName()
61
    {
62
        return static::$headerName;
63
    }
64
65
    /**
66
     * Get header field value.
67
     *
68
     * @return mixed
69
     */
70
    public function getFieldValue()
71
    {
72
        return $this->value;
73
    }
74
75
    /**
76
     * Cast header to string.
77
     *
78
     * @return string
79
     */
80
    public function __toString()
81
    {
82
        return sprintf('%s: %s', $this->getFieldName(), $this->getFieldValue());
83
    }
84
}
85