FacebookResponse.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * Copyright 2014 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. /**
  26. * Class FacebookResponse
  27. * @package Facebook
  28. * @author Fosco Marotto <fjm@fb.com>
  29. * @author David Poll <depoll@fb.com>
  30. */
  31. class FacebookResponse
  32. {
  33. /**
  34. * @var FacebookRequest The request which produced this response
  35. */
  36. private $request;
  37. /**
  38. * @var array The decoded response from the Graph API
  39. */
  40. private $responseData;
  41. /**
  42. * @var string The raw response from the Graph API
  43. */
  44. private $rawResponse;
  45. /**
  46. * @var bool Indicates whether sent ETag matched the one on the FB side
  47. */
  48. private $etagHit;
  49. /**
  50. * @var string ETag received with the response. `null` in case of ETag hit.
  51. */
  52. private $etag;
  53. /**
  54. * Creates a FacebookResponse object for a given request and response.
  55. *
  56. * @param FacebookRequest $request
  57. * @param array $responseData JSON Decoded response data
  58. * @param string $rawResponse Raw string response
  59. * @param bool $etagHit Indicates whether sent ETag matched the one on the FB side
  60. * @param string|null $etag ETag received with the response. `null` in case of ETag hit.
  61. */
  62. public function __construct($request, $responseData, $rawResponse, $etagHit = false, $etag = null)
  63. {
  64. $this->request = $request;
  65. $this->responseData = $responseData;
  66. $this->rawResponse = $rawResponse;
  67. $this->etagHit = $etagHit;
  68. $this->etag = $etag;
  69. }
  70. /**
  71. * Returns the request which produced this response.
  72. *
  73. * @return FacebookRequest
  74. */
  75. public function getRequest()
  76. {
  77. return $this->request;
  78. }
  79. /**
  80. * Returns the decoded response data.
  81. *
  82. * @return array
  83. */
  84. public function getResponse()
  85. {
  86. return $this->responseData;
  87. }
  88. /**
  89. * Returns the raw response
  90. *
  91. * @return string
  92. */
  93. public function getRawResponse()
  94. {
  95. return $this->rawResponse;
  96. }
  97. /**
  98. * Returns true if ETag matched the one sent with a request
  99. *
  100. * @return bool
  101. */
  102. public function isETagHit()
  103. {
  104. return $this->etagHit;
  105. }
  106. /**
  107. * Returns the ETag
  108. *
  109. * @return string
  110. */
  111. public function getETag()
  112. {
  113. return $this->etag;
  114. }
  115. /**
  116. * Gets the result as a GraphObject. If a type is specified, returns the
  117. * strongly-typed subclass of GraphObject for the data.
  118. *
  119. * @param string $type
  120. *
  121. * @return mixed
  122. */
  123. public function getGraphObject($type = 'Facebook\GraphObject') {
  124. return (new GraphObject($this->responseData))->cast($type);
  125. }
  126. /**
  127. * Returns an array of GraphObject returned by the request. If a type is
  128. * specified, returns the strongly-typed subclass of GraphObject for the data.
  129. *
  130. * @param string $type
  131. *
  132. * @return mixed
  133. */
  134. public function getGraphObjectList($type = 'Facebook\GraphObject') {
  135. $out = array();
  136. $data = $this->responseData->data;
  137. for ($i = 0; $i < count($data); $i++) {
  138. $out[] = (new GraphObject($data[$i]))->cast($type);
  139. }
  140. return $out;
  141. }
  142. /**
  143. * If this response has paginated data, returns the FacebookRequest for the
  144. * next page, or null.
  145. *
  146. * @return FacebookRequest|null
  147. */
  148. public function getRequestForNextPage()
  149. {
  150. return $this->handlePagination('next');
  151. }
  152. /**
  153. * If this response has paginated data, returns the FacebookRequest for the
  154. * previous page, or null.
  155. *
  156. * @return FacebookRequest|null
  157. */
  158. public function getRequestForPreviousPage()
  159. {
  160. return $this->handlePagination('previous');
  161. }
  162. /**
  163. * Returns the FacebookRequest for the previous or next page, or null.
  164. *
  165. * @param string $direction
  166. *
  167. * @return FacebookRequest|null
  168. */
  169. private function handlePagination($direction) {
  170. if (isset($this->responseData->paging->$direction)) {
  171. $url = parse_url($this->responseData->paging->$direction);
  172. parse_str($url['query'], $params);
  173. if (isset($params['type']) && strpos($this->request->getPath(), $params['type']) != false){
  174. unset($params['type']);
  175. }
  176. return new FacebookRequest(
  177. $this->request->getSession(),
  178. $this->request->getMethod(),
  179. $this->request->getPath(),
  180. $params
  181. );
  182. } else {
  183. return null;
  184. }
  185. }
  186. }