1
|
|
|
<?php |
2
|
|
|
namespace gossi\swagger\collections; |
3
|
|
|
|
4
|
|
|
use gossi\swagger\Header; |
5
|
|
|
use phootwork\collection\CollectionUtils; |
6
|
|
|
use phootwork\collection\Map; |
7
|
|
|
use phootwork\lang\Arrayable; |
8
|
|
|
use gossi\swagger\AbstractModel; |
9
|
|
|
|
10
|
|
|
class Headers extends AbstractModel implements Arrayable, \Iterator { |
11
|
|
|
|
12
|
|
|
/** @var Map */ |
13
|
|
|
private $headers; |
14
|
|
|
|
15
|
8 |
|
public function __construct($contents = []) { |
16
|
8 |
|
$this->parse($contents === null ? [] : $contents); |
17
|
8 |
|
} |
18
|
|
|
|
19
|
8 |
|
private function parse($contents) { |
20
|
8 |
|
$data = CollectionUtils::toMap($contents); |
21
|
|
|
|
22
|
|
|
// headers |
23
|
8 |
|
$this->headers = new Map(); |
24
|
8 |
|
foreach ($data as $h => $props) { |
25
|
1 |
|
$this->headers->set($h, new Header($h, $props)); |
26
|
8 |
|
} |
27
|
8 |
|
} |
28
|
|
|
|
29
|
8 |
|
public function toArray() { |
30
|
8 |
|
return CollectionUtils::toArrayRecursive($this->headers); |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
public function size() { |
34
|
1 |
|
return $this->headers->size(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns whether a header with the given name exists |
39
|
|
|
* |
40
|
|
|
* @param string $header |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
1 |
|
public function has($header) { |
44
|
1 |
|
return $this->headers->has($header); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Returns whether the given header exists |
49
|
|
|
* |
50
|
|
|
* @param Header $header |
51
|
|
|
* @return bool |
52
|
|
|
*/ |
53
|
1 |
|
public function contains(Header $header) { |
54
|
1 |
|
return $this->headers->contains($header); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the header info for the given code |
59
|
|
|
* |
60
|
|
|
* @param string $header |
61
|
|
|
* @return Header |
62
|
|
|
*/ |
63
|
1 |
|
public function get($header) { |
64
|
1 |
|
return $this->headers->get($header); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Sets the header |
69
|
|
|
* |
70
|
|
|
* @param Header $header |
71
|
|
|
*/ |
72
|
1 |
|
public function add(Header $header) { |
73
|
1 |
|
$this->headers->set($header->getHeader(), $header); |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Sets all headers from another headers collection. Will overwrite existing ones. |
78
|
|
|
* |
79
|
|
|
* @param Headers $headers |
80
|
|
|
*/ |
81
|
|
|
public function addAll(Headers $headers) { |
82
|
|
|
foreach ($headers as $header) { |
83
|
|
|
$this->add($header); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Removes the given header |
89
|
|
|
* |
90
|
|
|
* @param string $header |
91
|
|
|
*/ |
92
|
1 |
|
public function remove($header) { |
93
|
1 |
|
$this->headers->remove($header); |
94
|
1 |
|
} |
95
|
|
|
|
96
|
|
|
public function current() { |
97
|
|
|
return $this->headers->current(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function key() { |
101
|
|
|
return $this->headers->key(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function next() { |
105
|
|
|
return $this->headers->next(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function rewind() { |
109
|
|
|
return $this->headers->rewind(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function valid() { |
113
|
|
|
return $this->headers->valid(); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|