Cheetah
FacebookBatchResponse.php
Go to the documentation of this file.
1 <?php
24 namespace Facebook;
25 
26 use ArrayIterator;
27 use IteratorAggregate;
28 use ArrayAccess;
29 
35 class FacebookBatchResponse extends FacebookResponse implements IteratorAggregate, ArrayAccess
36 {
40  protected $batchRequest;
41 
45  protected $responses = [];
46 
53  public function __construct(FacebookBatchRequest $batchRequest, FacebookResponse $response)
54  {
55  $this->batchRequest = $batchRequest;
56 
57  $request = $response->getRequest();
58  $body = $response->getBody();
59  $httpStatusCode = $response->getHttpStatusCode();
60  $headers = $response->getHeaders();
61  parent::__construct($request, $body, $httpStatusCode, $headers);
62 
63  $responses = $response->getDecodedBody();
64  $this->setResponses($responses);
65  }
66 
72  public function getResponses()
73  {
74  return $this->responses;
75  }
76 
83  public function setResponses(array $responses)
84  {
85  $this->responses = [];
86 
87  foreach ($responses as $key => $graphResponse) {
88  $this->addResponse($key, $graphResponse);
89  }
90  }
91 
98  public function addResponse($key, $response)
99  {
100  $originalRequestName = isset($this->batchRequest[$key]['name']) ? $this->batchRequest[$key]['name'] : $key;
101  $originalRequest = isset($this->batchRequest[$key]['request']) ? $this->batchRequest[$key]['request'] : null;
102 
103  $httpResponseBody = isset($response['body']) ? $response['body'] : null;
104  $httpResponseCode = isset($response['code']) ? $response['code'] : null;
105  // @TODO With PHP 5.5 support, this becomes array_column($response['headers'], 'value', 'name')
106  $httpResponseHeaders = isset($response['headers']) ? $this->normalizeBatchHeaders($response['headers']) : [];
107 
108  $this->responses[$originalRequestName] = new FacebookResponse(
109  $originalRequest,
110  $httpResponseBody,
111  $httpResponseCode,
112  $httpResponseHeaders
113  );
114  }
115 
119  public function getIterator()
120  {
121  return new ArrayIterator($this->responses);
122  }
123 
127  public function offsetSet($offset, $value)
128  {
129  $this->addResponse($offset, $value);
130  }
131 
135  public function offsetExists($offset)
136  {
137  return isset($this->responses[$offset]);
138  }
139 
143  public function offsetUnset($offset)
144  {
145  unset($this->responses[$offset]);
146  }
147 
151  public function offsetGet($offset)
152  {
153  return isset($this->responses[$offset]) ? $this->responses[$offset] : null;
154  }
155 
164  private function normalizeBatchHeaders(array $batchHeaders)
165  {
166  $headers = [];
167 
168  foreach ($batchHeaders as $header) {
169  $headers[$header['name']] = $header['value'];
170  }
171 
172  return $headers;
173  }
174 }
Facebook\FacebookBatchResponse\offsetSet
offsetSet($offset, $value)
Definition: FacebookBatchResponse.php:127
Facebook\FacebookBatchRequest
Definition: FacebookBatchRequest.php:38
use
GNU LESSER GENERAL PUBLIC LICENSE February Free Software Inc Franklin Fifth MA USA Everyone is permitted to copy and distribute verbatim copies of this license but changing it is not allowed[This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it By the GNU General Public Licenses are intended to guarantee your freedom to share and change free software to make sure the software is free for all its users This the Lesser General Public applies to some specially designated software packages typically libraries of the Free Software Foundation and other authors who decide to use it You can use it but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular based on the explanations below When we speak of free we are referring to freedom of use
Definition: license.txt:27
Facebook\FacebookResponse\getDecodedBody
getDecodedBody()
Definition: FacebookResponse.php:150
php
Facebook\FacebookResponse
Definition: FacebookResponse.php:36
Facebook\FacebookBatchResponse
Definition: FacebookBatchResponse.php:36
Facebook\FacebookBatchResponse\offsetUnset
offsetUnset($offset)
Definition: FacebookBatchResponse.php:143
Facebook\FacebookBatchResponse\$batchRequest
$batchRequest
Definition: FacebookBatchResponse.php:40
Facebook\FacebookResponse\getBody
getBody()
Definition: FacebookResponse.php:140
Facebook\FacebookBatchResponse\getResponses
getResponses()
Definition: FacebookBatchResponse.php:72
Facebook\FacebookBatchResponse\offsetGet
offsetGet($offset)
Definition: FacebookBatchResponse.php:151
Facebook\FacebookResponse\getHttpStatusCode
getHttpStatusCode()
Definition: FacebookResponse.php:120
Facebook
Facebook\FacebookBatchResponse\offsetExists
offsetExists($offset)
Definition: FacebookBatchResponse.php:135
Facebook\FacebookResponse\getRequest
getRequest()
Definition: FacebookResponse.php:90
Facebook\FacebookBatchResponse\setResponses
setResponses(array $responses)
Definition: FacebookBatchResponse.php:83
Facebook\FacebookResponse\getHeaders
getHeaders()
Definition: FacebookResponse.php:130
Facebook\FacebookBatchResponse\__construct
__construct(FacebookBatchRequest $batchRequest, FacebookResponse $response)
Definition: FacebookBatchResponse.php:53
Facebook\FacebookBatchResponse\getIterator
getIterator()
Definition: FacebookBatchResponse.php:119
as
as
Definition: Filter.ExtractStyleBlocks.Escaping.txt:10
Facebook\FacebookBatchResponse\addResponse
addResponse($key, $response)
Definition: FacebookBatchResponse.php:98