| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
namespace AWS\CRT\HTTP; |
| 7 |
|
| 8 |
use AWS\CRT\Internal\Encoding; |
| 9 |
|
| 10 |
final class Headers { |
| 11 |
private $headers; |
| 12 |
|
| 13 |
public function __construct($headers = []) { |
| 14 |
$this->headers = $headers; |
| 15 |
} |
| 16 |
|
| 17 |
public static function marshall($headers) { |
| 18 |
$buf = ""; |
| 19 |
foreach ($headers->headers as $header => $value) { |
| 20 |
$buf .= Encoding::encodeString($header); |
| 21 |
$buf .= Encoding::encodeString($value); |
| 22 |
} |
| 23 |
return $buf; |
| 24 |
} |
| 25 |
|
| 26 |
public static function unmarshall($buf) { |
| 27 |
$strings = Encoding::readStrings($buf); |
| 28 |
$headers = []; |
| 29 |
for ($idx = 0; $idx < count($strings);) { |
| 30 |
$headers[$strings[$idx++]] = $strings[$idx++]; |
| 31 |
} |
| 32 |
return new Headers($headers); |
| 33 |
} |
| 34 |
|
| 35 |
public function count() { |
| 36 |
return count($this->headers); |
| 37 |
} |
| 38 |
|
| 39 |
public function get($header) { |
| 40 |
return isset($this->headers[$header]) ? $this->headers[$header] : null; |
| 41 |
} |
| 42 |
|
| 43 |
public function set($header, $value) { |
| 44 |
$this->headers[$header] = $value; |
| 45 |
} |
| 46 |
|
| 47 |
public function toArray() { |
| 48 |
return $this->headers; |
| 49 |
} |
| 50 |
} |