1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ActiveRecord for API |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/yii2-hiart |
6
|
|
|
* @package yii2-hiart |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\hiart\stream; |
12
|
|
|
|
13
|
|
|
use hiqdev\hiart\AbstractResponse; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* PHP stream response implementation. |
17
|
|
|
* |
18
|
|
|
* @author Andrii Vasyliev <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class Response extends AbstractResponse |
21
|
|
|
{ |
22
|
|
|
protected $rawData; |
23
|
|
|
|
24
|
|
|
protected $headers; |
25
|
|
|
|
26
|
|
|
protected $statusCode; |
27
|
|
|
|
28
|
|
|
protected $reasonPhrase; |
29
|
|
|
|
30
|
|
|
public function __construct(Request $request, $rawData, array $rawHeaders) |
31
|
|
|
{ |
32
|
|
|
$this->request = $request; |
33
|
|
|
$this->rawData = $rawData; |
34
|
|
|
$this->headers = $this->parseHeaders($rawHeaders); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getRawData() |
38
|
|
|
{ |
39
|
|
|
return $this->rawData; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getHeader($name) |
43
|
|
|
{ |
44
|
|
|
$name = strtolower($name); |
45
|
|
|
|
46
|
|
|
return isset($this->headers[$name]) ? $this->headers[$name] : null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getHeaders() |
50
|
|
|
{ |
51
|
|
|
return $this->headers; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function parseHeaders($headers) |
55
|
|
|
{ |
56
|
|
|
$result = []; |
57
|
|
|
|
58
|
|
|
foreach ($headers as $header) { |
59
|
|
|
if (strncmp($header, 'HTTP/', 5) === 0) { |
60
|
|
|
$parts = explode(' ', $header, 3); |
61
|
|
|
$this->version = substr($parts[0], 5); |
|
|
|
|
62
|
|
|
$this->statusCode = $parts[1]; |
63
|
|
|
$this->reasonPhrase = $parts[2]; |
64
|
|
|
} elseif (($pos = strpos($header, ':')) !== false) { |
65
|
|
|
$name = strtolower(trim(substr($header, 0, $pos))); |
66
|
|
|
$value = trim(substr($header, $pos + 1)); |
67
|
|
|
$result[$name][] = $value; |
68
|
|
|
} else { |
69
|
|
|
$result['raw'][] = $header; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $result; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getStatusCode() |
77
|
|
|
{ |
78
|
|
|
return $this->statusCode; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getReasonPhrase() |
82
|
|
|
{ |
83
|
|
|
return $this->reasonPhrase; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|