|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2017 Facebook, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* You are hereby granted a non-exclusive, worldwide, royalty-free license to |
|
6
|
|
|
* use, copy, modify, and distribute this software in source code or binary |
|
7
|
|
|
* form for use in connection with the web services and APIs provided by |
|
8
|
|
|
* Facebook. |
|
9
|
|
|
* |
|
10
|
|
|
* As with any software that integrates with the Facebook platform, your use |
|
11
|
|
|
* of this software is subject to the Facebook Developer Principles and |
|
12
|
|
|
* Policies [http://developers.facebook.com/policy/]. This copyright notice |
|
13
|
|
|
* shall be included in all copies or substantial portions of the software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
|
18
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|
20
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|
21
|
|
|
* DEALINGS IN THE SOFTWARE. |
|
22
|
|
|
*/ |
|
23
|
|
|
namespace Facebook; |
|
24
|
|
|
|
|
25
|
|
|
use ArrayIterator; |
|
26
|
|
|
use IteratorAggregate; |
|
27
|
|
|
use ArrayAccess; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @package Facebook |
|
31
|
|
|
*/ |
|
32
|
|
|
class BatchResponse extends Response implements IteratorAggregate, ArrayAccess |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* @var BatchRequest the original entity that made the batch request |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $batchRequest; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Response[] an array of FacebookResponse entities |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $responses = []; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Creates a new Response entity. |
|
46
|
|
|
* |
|
47
|
|
|
* @param BatchRequest $batchRequest |
|
48
|
|
|
* @param Response $response |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(BatchRequest $batchRequest, Response $response) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->batchRequest = $batchRequest; |
|
53
|
|
|
|
|
54
|
|
|
$request = $response->getRequest(); |
|
55
|
|
|
$body = $response->getBody(); |
|
56
|
|
|
$httpStatusCode = $response->getHttpStatusCode(); |
|
57
|
|
|
$headers = $response->getHeaders(); |
|
58
|
|
|
parent::__construct($request, $body, $httpStatusCode, $headers); |
|
59
|
|
|
|
|
60
|
|
|
$responses = $response->getDecodedBody(); |
|
61
|
|
|
$this->setResponses($responses); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Returns an array of FacebookResponse entities. |
|
66
|
|
|
* |
|
67
|
|
|
* @return Response[] |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getResponses() |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->responses; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* The main batch response will be an array of requests so |
|
76
|
|
|
* we need to iterate over all the responses. |
|
77
|
|
|
* |
|
78
|
|
|
* @param array $responses |
|
79
|
|
|
*/ |
|
80
|
|
|
public function setResponses(array $responses) |
|
81
|
|
|
{ |
|
82
|
|
|
$this->responses = []; |
|
83
|
|
|
|
|
84
|
|
|
foreach ($responses as $key => $graphResponse) { |
|
85
|
|
|
$this->addResponse($key, $graphResponse); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Add a response to the list. |
|
91
|
|
|
* |
|
92
|
|
|
* @param int $key |
|
93
|
|
|
* @param null|array $response |
|
94
|
|
|
*/ |
|
95
|
|
|
public function addResponse($key, $response) |
|
96
|
|
|
{ |
|
97
|
|
|
$originalRequestName = isset($this->batchRequest[$key]['name']) ? $this->batchRequest[$key]['name'] : $key; |
|
98
|
|
|
$originalRequest = isset($this->batchRequest[$key]['request']) ? $this->batchRequest[$key]['request'] : null; |
|
99
|
|
|
|
|
100
|
|
|
$httpResponseBody = isset($response['body']) ? $response['body'] : null; |
|
101
|
|
|
$httpResponseCode = isset($response['code']) ? $response['code'] : null; |
|
102
|
|
|
// @TODO With PHP 5.5 support, this becomes array_column($response['headers'], 'value', 'name') |
|
103
|
|
|
$httpResponseHeaders = isset($response['headers']) ? $this->normalizeBatchHeaders($response['headers']) : []; |
|
104
|
|
|
|
|
105
|
|
|
$this->responses[$originalRequestName] = new Response( |
|
106
|
|
|
$originalRequest, |
|
|
|
|
|
|
107
|
|
|
$httpResponseBody, |
|
108
|
|
|
$httpResponseCode, |
|
109
|
|
|
$httpResponseHeaders |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* {@inheritdoc} |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getIterator() |
|
117
|
|
|
{ |
|
118
|
|
|
return new ArrayIterator($this->responses); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* {@inheritdoc} |
|
123
|
|
|
*/ |
|
124
|
|
|
public function offsetSet($offset, $value) |
|
125
|
|
|
{ |
|
126
|
|
|
$this->addResponse($offset, $value); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* {@inheritdoc} |
|
131
|
|
|
*/ |
|
132
|
|
|
public function offsetExists($offset) |
|
133
|
|
|
{ |
|
134
|
|
|
return isset($this->responses[$offset]); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* {@inheritdoc} |
|
139
|
|
|
*/ |
|
140
|
|
|
public function offsetUnset($offset) |
|
141
|
|
|
{ |
|
142
|
|
|
unset($this->responses[$offset]); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* {@inheritdoc} |
|
147
|
|
|
*/ |
|
148
|
|
|
public function offsetGet($offset) |
|
149
|
|
|
{ |
|
150
|
|
|
return isset($this->responses[$offset]) ? $this->responses[$offset] : null; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Converts the batch header array into a standard format. |
|
155
|
|
|
* |
|
156
|
|
|
* @TODO replace with array_column() when PHP 5.5 is supported. |
|
157
|
|
|
* |
|
158
|
|
|
* @param array $batchHeaders |
|
159
|
|
|
* |
|
160
|
|
|
* @return array |
|
161
|
|
|
*/ |
|
162
|
|
|
private function normalizeBatchHeaders(array $batchHeaders) |
|
163
|
|
|
{ |
|
164
|
|
|
$headers = []; |
|
165
|
|
|
|
|
166
|
|
|
foreach ($batchHeaders as $header) { |
|
167
|
|
|
$headers[$header['name']] = $header['value']; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return $headers; |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|