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
|
|
|
use Borobudur\Http\Header\Exception\InvalidHeaderValueException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Iqbal Maulana <[email protected]> |
17
|
|
|
* @created 7/19/15 |
18
|
|
|
*/ |
19
|
|
|
class AgeHeader implements HeaderInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
private $deltaSeconds; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor. |
28
|
|
|
* |
29
|
|
|
* @param int|null $deltaSeconds |
30
|
|
|
*/ |
31
|
|
|
public function __construct($deltaSeconds = null) |
32
|
|
|
{ |
33
|
|
|
if (null !== $deltaSeconds) { |
34
|
|
|
$this->setDeltaSeconds($deltaSeconds); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Set delta seconds. |
40
|
|
|
* |
41
|
|
|
* @param int $deltaSeconds |
42
|
|
|
* |
43
|
|
|
* @return $this |
44
|
|
|
* |
45
|
|
|
* @throws InvalidHeaderValueException |
46
|
|
|
*/ |
47
|
|
|
public function setDeltaSeconds($deltaSeconds) |
48
|
|
|
{ |
49
|
|
|
$this->deltaSeconds = GenericHeader::computeDeltaSeconds($deltaSeconds); |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Factory create AgeHeader from string. |
56
|
|
|
* |
57
|
|
|
* @param string $headerLine |
58
|
|
|
* |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public static function fromString($headerLine) |
62
|
|
|
{ |
63
|
|
|
list($fieldName, $fieldValue) = GenericHeader::splitHeaderLine($headerLine); |
64
|
|
|
GenericHeader::assertHeaderFieldName('Age', $fieldName); |
65
|
|
|
|
66
|
|
|
return new static($fieldValue !== null ? $fieldValue : null); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Cast header to string. |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function __toString() |
75
|
|
|
{ |
76
|
|
|
return sprintf('%s: %s', $this->getFieldName(), $this->getFieldValue()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get header field name. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getFieldName() |
85
|
|
|
{ |
86
|
|
|
return 'Age'; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get header field value. |
91
|
|
|
* |
92
|
|
|
* @return int |
93
|
|
|
*/ |
94
|
|
|
public function getFieldValue() |
95
|
|
|
{ |
96
|
|
|
return $this->deltaSeconds; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|