|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Author: Jairo Rodríguez <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace BFunky\HttpParser\Entity; |
|
10
|
|
|
|
|
11
|
|
|
use BFunky\HttpParser\Exception\HttpFieldNotFoundOnCollection; |
|
12
|
|
|
|
|
13
|
|
|
class HttpFieldCollection |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var HttpField[] |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $httpFields; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* HttpFieldCollection constructor. |
|
22
|
5 |
|
* @param HttpField[] $httpFields |
|
23
|
|
|
*/ |
|
24
|
5 |
|
public function __construct(array $httpFields = []) |
|
25
|
5 |
|
{ |
|
26
|
1 |
|
$this->httpFields = []; |
|
27
|
|
|
foreach ($httpFields as $httpField) { |
|
28
|
5 |
|
$this->httpFields[$httpField->getName()] = $httpField; |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param HttpField $obj |
|
34
|
|
|
*/ |
|
35
|
|
|
public function add(HttpField $obj): void |
|
36
|
|
|
{ |
|
37
|
|
|
$this->httpFields[$obj->getName()] = $obj; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string $key |
|
42
|
2 |
|
* @throws HttpFieldNotFoundOnCollection |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function delete(string $key): void |
|
45
|
|
|
{ |
|
46
|
|
|
$this->checkKeyExists($key); |
|
47
|
|
|
unset($this->httpFields[$key]); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $key |
|
52
|
|
|
* @return HttpField |
|
53
|
1 |
|
* @throws HttpFieldNotFoundOnCollection |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function get(string $key): HttpField |
|
56
|
|
|
{ |
|
57
|
|
|
$this->checkKeyExists($key); |
|
58
|
|
|
return $this->httpFields[$key]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param $key |
|
63
|
3 |
|
* @throws HttpFieldNotFoundOnCollection |
|
64
|
|
|
*/ |
|
65
|
3 |
|
private function checkKeyExists($key): void |
|
66
|
2 |
|
{ |
|
67
|
|
|
if (!array_key_exists($key, $this->httpFields)) { |
|
68
|
1 |
|
throw new HttpFieldNotFoundOnCollection('Field ' . $key . ' not found'); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param array $httpFields |
|
74
|
2 |
|
* @return self |
|
75
|
|
|
*/ |
|
76
|
2 |
|
public static function fromHttpFieldArray(array $httpFields): self |
|
77
|
|
|
{ |
|
78
|
|
|
return new self($httpFields); |
|
79
|
|
|
} |
|
80
|
|
|
} |