|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Tools to use API as ActiveRecord for Yii2 |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/hiqdev/yii2-hiart |
|
6
|
|
|
* @package yii2-hiart |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @copyright Copyright (c) 2015-2017, 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
|
2 |
|
public function __construct(Request $request, $rawData, array $rawHeaders) |
|
31
|
|
|
{ |
|
32
|
2 |
|
$this->request = $request; |
|
33
|
2 |
|
$this->rawData = $rawData; |
|
34
|
2 |
|
$this->headers = $this->parseHeaders($rawHeaders); |
|
35
|
2 |
|
} |
|
36
|
|
|
|
|
37
|
2 |
|
public function getRawData() |
|
38
|
|
|
{ |
|
39
|
2 |
|
return $this->rawData; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
2 |
|
public function getHeader($name) |
|
43
|
|
|
{ |
|
44
|
2 |
|
$name = strtolower($name); |
|
45
|
|
|
|
|
46
|
2 |
|
return isset($this->headers[$name]) ? $this->headers[$name] : null; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
2 |
|
public function parseHeaders($headers) |
|
50
|
|
|
{ |
|
51
|
2 |
|
$result = []; |
|
52
|
|
|
|
|
53
|
2 |
|
foreach ($headers as $header) { |
|
54
|
2 |
|
if (strncmp($header, 'HTTP/', 5) === 0) { |
|
55
|
2 |
|
$parts = explode(' ', $header, 3); |
|
56
|
2 |
|
$this->version = substr($parts[0], 5); |
|
|
|
|
|
|
57
|
2 |
|
$this->statusCode = $parts[1]; |
|
58
|
2 |
|
$this->reasonPhrase = $parts[2]; |
|
59
|
2 |
|
} elseif (($pos = strpos($header, ':')) !== false) { |
|
60
|
2 |
|
$name = strtolower(trim(substr($header, 0, $pos))); |
|
61
|
2 |
|
$value = trim(substr($header, $pos + 1)); |
|
62
|
2 |
|
$result[$name][] = $value; |
|
63
|
2 |
|
} else { |
|
64
|
|
|
$result['raw'][] = $header; |
|
65
|
|
|
} |
|
66
|
2 |
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
return $result; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
public function getStatusCode() |
|
72
|
|
|
{ |
|
73
|
2 |
|
return $this->statusCode; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
public function getReasonPhrase() |
|
77
|
|
|
{ |
|
78
|
2 |
|
return $this->reasonPhrase; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: