PageRenderTime 71ms CodeModel.GetById 42ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/packages/contextio/classes/contextio/response.php

https://bitbucket.org/samhunter3/redzu
PHP | 175 lines | 110 code | 17 blank | 48 comment | 20 complexity | b2d9fae5930ff31d18aadbdd5c2f46d0 MD5 | raw file
  1. <?php
  2. namespace ContextIO;
  3. /*
  4. Copyright (C) 2011 DokDok Inc.
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. */
  21. /**
  22. * Container of the ContextIOResponse class
  23. * @copyright Copyright (C) 2011 DokDok Inc.
  24. * @licence http://opensource.org/licenses/mit-license MIT Licence
  25. */
  26. class ContextIOResponse {
  27. protected $headers;
  28. protected $rawResponseHeaders;
  29. protected $rawRequestHeaders;
  30. protected $rawResponse;
  31. protected $decodedResponse;
  32. protected $httpCode;
  33. protected $contentType;
  34. protected $hasError;
  35. function __construct($httpCode, $requestHeaders, $responseHeaders, $contentType, $rawResponse) {
  36. $this->httpCode = (int)$httpCode;
  37. $this->contentType = $contentType;
  38. $this->rawResponse = $rawResponse;
  39. $this->rawResponseHeaders = (is_array($responseHeaders)) ? $responseHeaders : false;
  40. $this->rawRequestHeaders = (is_array($requestHeaders)) ? $requestHeaders : false;
  41. $this->hasError = false;
  42. $this->headers = array('request'=>$requestHeaders, 'response'=>null);
  43. $this->_decodeResponse();
  44. $this->_parseHeaders('response');
  45. $this->_parseHeaders('request');
  46. }
  47. private function _parseHeaders($which = 'response') {
  48. $raw = ($which == 'response') ? $this->rawResponseHeaders : $this->rawRequestHeaders;
  49. if ($raw !== false) {
  50. $headers = array();
  51. $headers[($which == 'response') ? 'Status-Line' : 'Request-Line'] = trim(array_shift($raw));
  52. $headerName = '';
  53. foreach ($raw as $headerLine) {
  54. $firstChar = substr($headerLine, 0, 1);
  55. if ($firstChar == chr(32) || $firstChar == chr(9)) {
  56. // continuing value of previous header line
  57. if (is_array($headers[$headerName])) {
  58. $idx = count($headers[$headerName]) - 1;
  59. $headers[$headerName][$idx] .= "\n".trim($headerLine);
  60. }
  61. else {
  62. $headers[$headerName] .= "\n".trim($headerLine);
  63. }
  64. }
  65. else {
  66. // New header line
  67. $idx = strpos($headerLine, ':');
  68. if ($idx !== false) {
  69. $headerName = trim(substr($headerLine, 0, $idx));
  70. $headerValue = trim(substr($headerLine, $idx + 1));
  71. if (array_key_exists($headerName, $this->headers)) {
  72. // Already have an occurence of this header. Make the header an array with all occurences
  73. if (is_array($headers[$headerName])) {
  74. $headers[$headerName][] = $headerValue;
  75. }
  76. else {
  77. $headers[$headerName] = array(
  78. $headers[$headerName],
  79. $headerValue
  80. );
  81. }
  82. }
  83. else {
  84. // First occurence, simply give value as a string
  85. $headers[$headerName] = $headerValue;
  86. }
  87. }
  88. }
  89. }
  90. $this->headers[$which] = $headers;
  91. }
  92. }
  93. private function _decodeResponse() {
  94. if (! (($this->httpCode >= 200) && ($this->httpCode < 400))) {
  95. $this->hasError = true;
  96. }
  97. if ($this->contentType != 'application/json') {
  98. $this->hasError = true;
  99. return;
  100. }
  101. $this->decodedResponse = json_decode($this->rawResponse, true);
  102. }
  103. public function getRawResponse() {
  104. return $this->rawResponse;
  105. }
  106. public function getRawResponseHeaders() {
  107. return $this->rawResponseHeaders;
  108. }
  109. public function getResponseHeaders() {
  110. return $this->headers['response'];
  111. }
  112. public function getRawRequestHeaders() {
  113. return $this->rawRequestHeaders;
  114. }
  115. public function getRequestHeaders() {
  116. return $this->headers['request'];
  117. }
  118. public function getHttpCode() {
  119. return $this->httpCode;
  120. }
  121. /**
  122. * Returns the response body parsed into a PHP structure. To get the JSON
  123. * string, use getRawResponse()
  124. */
  125. public function getData() {
  126. return $this->decodedResponse;
  127. }
  128. /**
  129. * Let's you access the value of one specific property in the response body.
  130. * This support nested properties. For example:
  131. * <code>
  132. * $response = $ContextIO->getMessage($accountId, array("message_id"=>"1234abcd"));
  133. * $data = $response->getData();
  134. * $firstRecipientEmail = $data['addresses']['to'][0]['email'];
  135. * </code>
  136. * ... is equivalent to ...
  137. * <code>
  138. * $response = $ContextIO->getMessage($accountId, array("message_id"=>"1234abcd"));
  139. * $firstRecipientEmail = $response->getDataProperty("addresses.to.0.email");
  140. * </code>
  141. */
  142. public function getDataProperty($propertyName) {
  143. $props = explode(".", $propertyName);
  144. $value = $this->decodedResponse;
  145. do {
  146. $prop = array_shift($props);
  147. $value = (strval(intval($prop)) === $prop) ? $value[intval($prop)] : (array_key_exists($prop, $value)) ? $value[$prop] : null;
  148. } while(count($props) >= 1 && !is_null($value));
  149. return $value;
  150. }
  151. public function hasError() {
  152. return $this->hasError;
  153. }
  154. }
  155. ?>