ContentLengthHeader::getFieldValue()   A
last analyzed

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 0
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\Content;
12
13
use Borobudur\Http\Header\GenericHeader;
14
use Borobudur\Http\Header\HeaderInterface;
15
16
/**
17
 * @author      Iqbal Maulana <[email protected]>
18
 * @created     7/20/15
19
 */
20
class ContentLengthHeader implements HeaderInterface
21
{
22
    /**
23
     * @var mixed
24
     */
25
    private $length;
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param mixed $length
31
     */
32
    public function __construct($length)
33
    {
34
        GenericHeader::assertHeaderValue(trim($length));
35
        $this->length = trim($length);
36
    }
37
38
    /**
39
     * Factory create ContentLengthHeader from string representation.
40
     *
41
     * @param string $headerLine
42
     *
43
     * @return $this
44
     */
45
    public static function fromString($headerLine)
46
    {
47
        list($fieldName, $fieldValue) = GenericHeader::splitHeaderLine($headerLine);
48
        GenericHeader::assertHeaderFieldName('Content-Length', $fieldName);
49
50
        return new static($fieldValue);
51
    }
52
53
    /**
54
     * Get header field name.
55
     *
56
     * @return string
57
     */
58
    public function getFieldName()
59
    {
60
        return 'Content-Length';
61
    }
62
63
    /**
64
     * Get header field value.
65
     *
66
     * @return mixed
67
     */
68
    public function getFieldValue()
69
    {
70
        return (int) $this->length;
71
    }
72
73
    /**
74
     * Cast header to string.
75
     *
76
     * @return string
77
     */
78
    public function __toString()
79
    {
80
        return sprintf('%s: %s', $this->getFieldName(), $this->getFieldValue());
81
    }
82
}
83