|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: lenovo |
|
5
|
|
|
* Date: 2018/6/15 |
|
6
|
|
|
* Time: 23:30 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace TimSDK\Foundation; |
|
10
|
|
|
|
|
11
|
|
|
use TimSDK\Core\Exceptions\JsonParseException; |
|
12
|
|
|
use TimSDK\Support\Collection; |
|
13
|
|
|
use TimSDK\Support\Json; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class ResponseBag |
|
17
|
|
|
* @package TimSDK\Foundation |
|
18
|
|
|
*/ |
|
19
|
|
|
class ResponseBag |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var Collection |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $headers; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Collection |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $contents; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Http status code |
|
33
|
|
|
* |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $statusCode; |
|
37
|
|
|
|
|
38
|
|
|
public function __construct($contents, $headers, $statusCode = 200) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->contents = $this->getCollectionItems($contents); |
|
41
|
|
|
$this->headers = $this->getCollectionItems($headers); |
|
42
|
|
|
$this->statusCode = $statusCode; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return int |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getStatusCode() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->statusCode; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return Collection |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getHeaders() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->headers; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return Collection |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getContents() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->contents; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get a header parameter |
|
71
|
|
|
* |
|
72
|
|
|
* @param $name |
|
73
|
|
|
* @param null $default |
|
|
|
|
|
|
74
|
|
|
* @return mixed |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getHeader($name, $default = null) |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->headers->get($name, $default); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get a contents parameter |
|
83
|
|
|
* |
|
84
|
|
|
* @param $name |
|
85
|
|
|
* @param null $default |
|
|
|
|
|
|
86
|
|
|
* @return mixed |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getContent($name, $default = null) |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->contents->get($name, $default); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Results array of items from Collection or Arrayable. |
|
95
|
|
|
* |
|
96
|
|
|
* @param $items |
|
97
|
|
|
* @return Collection |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function getCollectionItems($items) |
|
100
|
|
|
{ |
|
101
|
|
|
if ($items instanceof Collection) { |
|
102
|
|
|
return $items; |
|
103
|
|
|
} elseif ($items instanceof \JsonSerializable) { |
|
104
|
|
|
$items = $items->jsonSerialize(); |
|
105
|
|
|
} elseif (is_string($items)) { |
|
106
|
|
|
try { |
|
107
|
|
|
$items = Json::decode($items, true); |
|
108
|
|
|
} catch (JsonParseException $e) { |
|
|
|
|
|
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return new Collection((array) $items); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|