-
Notifications
You must be signed in to change notification settings - Fork 1
/
ResponseOfClient.php
192 lines (161 loc) · 3.92 KB
/
ResponseOfClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
namespace Poirot\ApiClient;
use Poirot\ApiClient\Interfaces\Response\iResponse;
class ResponseOfClient
implements iResponse
{
protected $meta = array();
protected $responseCode;
/** @var string Origin Response Body */
protected $rawBody;
/** @var \Exception Exception */
protected $exception = null;
/**
* iResponse constructor.
*
* @param string $rawResponseBody Response body
* @param int $responseCode Response code
* @param array|\Traversable $meta Meta Headers
* @param null|\Exception $exception Exception
*/
function __construct($rawResponseBody, $responseCode = null, $meta = null, \Exception $exception = null)
{
$this->rawBody = $rawResponseBody;
$this->responseCode = $responseCode;
if ($meta !== null)
$this->meta = $this->_assertMetaData($meta);
if ($exception !== null)
$this->exception = $exception;
}
/**
* Set Meta Data Headers
*
* @param array|\Traversable $data Meta Data Header
*
* @return $this Clone
*/
function withMeta($data)
{
$meta = $this->_assertMetaData($data);
$new = clone $this;
$new->meta = array_merge($this->meta, $meta);
return $new;
}
/**
* Set Response Origin Content
*
* @param string $rawBody Content Body
*
* @return $this
*/
function withRawBody($rawBody)
{
$new = clone $this;
$new->rawBody = $rawBody;
return $new;
}
/**
* Set Response Code
*
* @param string $code Response code
*
* @return $this
*/
function withResponseCode($code)
{
$new = clone $this;
$new->responseCode = $code;
return $new;
}
/**
* Set Exception
*
* @param \Exception $exception Exception
* @return $this
*/
function withException(\Exception $exception)
{
$new = clone $this;
$new->exception = $exception;
return $new;
}
/**
* Process Raw Body As Result
*
* :proc
* mixed function($originResult, $self);
*
* @param callable $callable
*
* @return mixed
*/
function expected(/*callable*/ $callable = null)
{
if ($callable !== null)
return call_user_func($callable, $this->rawBody, $this);
return $this->rawBody;
}
/**
* Meta Data Or Headers
*
* @param null|string $metaKey Specific meta key to retrieve
*
* @return array
* @return null|mixed When meta key given
*/
function getMeta($metaKey = null)
{
if ($metaKey !== null)
return (isset($this->meta[$metaKey])) ? $this->meta[$metaKey] : null;
return $this->meta;
}
/**
* Get Response Origin Body Content
*
* @return string
*/
function getRawBody()
{
return $this->rawBody;
}
/**
* Response Code
*
* @return int|null
*/
function getResponseCode()
{
return $this->responseCode;
}
/**
* Has Exception?
*
* @return \Exception
*/
function hasException()
{
return $this->exception;
}
// ..
/**
* Assert Given Meta Data
*
* @param array|\Traversable $meta
*
* @return array
*/
protected function _assertMetaData($meta)
{
if ($meta instanceof \Traversable)
$meta = iterator_to_array($meta);
$exception = new \InvalidArgumentException(sprintf(
'Meta Must be Array Or Traversable Associated Key/Value Pair; given: (%s).'
, \Poirot\Std\flatten($meta)
));
if (! is_array($meta) )
throw $exception;
if ( !empty($meta) && array_values($meta) === $meta )
throw $exception;
return $meta;
}
}