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
|
|
|
|