FacebookBatchResponse.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. */
  24. namespace Facebook;
  25. use ArrayIterator;
  26. use IteratorAggregate;
  27. use ArrayAccess;
  28. /**
  29. * Class FacebookBatchResponse
  30. *
  31. * @package Facebook
  32. */
  33. class FacebookBatchResponse extends FacebookResponse implements IteratorAggregate, ArrayAccess
  34. {
  35. /**
  36. * @var FacebookBatchRequest The original entity that made the batch request.
  37. */
  38. protected $batchRequest;
  39. /**
  40. * @var array An array of FacebookResponse entities.
  41. */
  42. protected $responses = [];
  43. /**
  44. * Creates a new Response entity.
  45. *
  46. * @param FacebookBatchRequest $batchRequest
  47. * @param FacebookResponse $response
  48. */
  49. public function __construct(FacebookBatchRequest $batchRequest, FacebookResponse $response)
  50. {
  51. $this->batchRequest = $batchRequest;
  52. $request = $response->getRequest();
  53. $body = $response->getBody();
  54. $httpStatusCode = $response->getHttpStatusCode();
  55. $headers = $response->getHeaders();
  56. parent::__construct($request, $body, $httpStatusCode, $headers);
  57. $responses = $response->getDecodedBody();
  58. $this->setResponses($responses);
  59. }
  60. /**
  61. * Returns an array of FacebookResponse entities.
  62. *
  63. * @return array
  64. */
  65. public function getResponses()
  66. {
  67. return $this->responses;
  68. }
  69. /**
  70. * The main batch response will be an array of requests so
  71. * we need to iterate over all the responses.
  72. *
  73. * @param array $responses
  74. */
  75. public function setResponses(array $responses)
  76. {
  77. $this->responses = [];
  78. foreach ($responses as $key => $graphResponse) {
  79. $this->addResponse($key, $graphResponse);
  80. }
  81. }
  82. /**
  83. * Add a response to the list.
  84. *
  85. * @param int $key
  86. * @param array|null $response
  87. */
  88. public function addResponse($key, $response)
  89. {
  90. $originalRequestName = isset($this->batchRequest[$key]['name']) ? $this->batchRequest[$key]['name'] : $key;
  91. $originalRequest = isset($this->batchRequest[$key]['request']) ? $this->batchRequest[$key]['request'] : null;
  92. $httpResponseBody = isset($response['body']) ? $response['body'] : null;
  93. $httpResponseCode = isset($response['code']) ? $response['code'] : null;
  94. // @TODO With PHP 5.5 support, this becomes array_column($response['headers'], 'value', 'name')
  95. $httpResponseHeaders = isset($response['headers']) ? $this->normalizeBatchHeaders($response['headers']) : [];
  96. $this->responses[$originalRequestName] = new FacebookResponse(
  97. $originalRequest,
  98. $httpResponseBody,
  99. $httpResponseCode,
  100. $httpResponseHeaders
  101. );
  102. }
  103. /**
  104. * @inheritdoc
  105. */
  106. public function getIterator()
  107. {
  108. return new ArrayIterator($this->responses);
  109. }
  110. /**
  111. * @inheritdoc
  112. */
  113. public function offsetSet($offset, $value)
  114. {
  115. $this->addResponse($offset, $value);
  116. }
  117. /**
  118. * @inheritdoc
  119. */
  120. public function offsetExists($offset)
  121. {
  122. return isset($this->responses[$offset]);
  123. }
  124. /**
  125. * @inheritdoc
  126. */
  127. public function offsetUnset($offset)
  128. {
  129. unset($this->responses[$offset]);
  130. }
  131. /**
  132. * @inheritdoc
  133. */
  134. public function offsetGet($offset)
  135. {
  136. return isset($this->responses[$offset]) ? $this->responses[$offset] : null;
  137. }
  138. /**
  139. * Converts the batch header array into a standard format.
  140. * @TODO replace with array_column() when PHP 5.5 is supported.
  141. *
  142. * @param array $batchHeaders
  143. *
  144. * @return array
  145. */
  146. private function normalizeBatchHeaders(array $batchHeaders)
  147. {
  148. $headers = [];
  149. foreach ($batchHeaders as $header) {
  150. $headers[$header['name']] = $header['value'];
  151. }
  152. return $headers;
  153. }
  154. }