Response.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /*
  3. * This file is part of the php-phantomjs.
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. namespace JonnyW\PhantomJs\Http;
  9. use JonnyW\PhantomJs\Procedure\OutputInterface;
  10. /**
  11. * PHP PhantomJs
  12. *
  13. * @author Jon Wenmoth <contact@jonnyw.me>
  14. */
  15. class Response
  16. implements ResponseInterface, OutputInterface
  17. {
  18. /**
  19. * Http headers array
  20. *
  21. * @var array
  22. * @access public
  23. */
  24. public $headers;
  25. /**
  26. * Response int
  27. *
  28. * @var string
  29. * @access public
  30. */
  31. public $status;
  32. /**
  33. * Response body
  34. *
  35. * @var string
  36. * @access public
  37. */
  38. public $content;
  39. /**
  40. * Response content type header
  41. *
  42. * @var string
  43. * @access public
  44. */
  45. public $contentType;
  46. /**
  47. * Requested URL
  48. *
  49. * @var string
  50. * @access public
  51. */
  52. public $url;
  53. /**
  54. * Redirected URL
  55. *
  56. * @var string
  57. * @access public
  58. */
  59. public $redirectUrl;
  60. /**
  61. * Request time string
  62. *
  63. * @var string
  64. * @access public
  65. */
  66. public $time;
  67. /**
  68. * Console messages
  69. *
  70. * @var array
  71. * @access public
  72. */
  73. public $console;
  74. /**
  75. * Import response data
  76. *
  77. * @access public
  78. * @param array $data
  79. * @return \JonnyW\PhantomJs\Http\Response
  80. */
  81. public function import(array $data)
  82. {
  83. foreach ($data as $param => $value) {
  84. if ($param === 'headers') {
  85. continue;
  86. }
  87. if (property_exists($this, $param)) {
  88. $this->$param = $value;
  89. }
  90. }
  91. $this->headers = array();
  92. if (isset($data['headers'])) {
  93. $this->setHeaders((array) $data['headers']);
  94. }
  95. return $this;
  96. }
  97. /**
  98. * Set headers array
  99. *
  100. * @access protected
  101. * @param array $headers
  102. * @return \JonnyW\PhantomJs\Http\Response
  103. */
  104. protected function setHeaders(array $headers)
  105. {
  106. foreach ($headers as $header) {
  107. if (isset($header['name']) && isset($header['value'])) {
  108. $this->headers[$header['name']] = $header['value'];
  109. }
  110. }
  111. return $this;
  112. }
  113. /**
  114. * Get HTTP headers array
  115. *
  116. * @access public
  117. * @return array
  118. */
  119. public function getHeaders()
  120. {
  121. return (array) $this->headers;
  122. }
  123. /**
  124. * Get HTTP header value for code
  125. *
  126. * @access public
  127. * @param string $code
  128. * @return mixed
  129. */
  130. public function getHeader($code)
  131. {
  132. if (isset($this->headers[$code])) {
  133. return $this->headers[$code];
  134. }
  135. return null;
  136. }
  137. /**
  138. * Get response status code
  139. *
  140. * @access public
  141. * @return integer
  142. */
  143. public function getStatus()
  144. {
  145. return (int) $this->status;
  146. }
  147. /**
  148. * Get page content from response
  149. *
  150. * @access public
  151. * @return string
  152. */
  153. public function getContent()
  154. {
  155. return $this->content;
  156. }
  157. /**
  158. * Get content type header
  159. *
  160. * @access public
  161. * @return string
  162. */
  163. public function getContentType()
  164. {
  165. return $this->contentType;
  166. }
  167. /**
  168. * Get request URL
  169. *
  170. * @access public
  171. * @return string
  172. */
  173. public function getUrl()
  174. {
  175. return $this->url;
  176. }
  177. /**
  178. * Get redirect URL (if redirected)
  179. *
  180. * @access public
  181. * @return string
  182. */
  183. public function getRedirectUrl()
  184. {
  185. return $this->redirectUrl;
  186. }
  187. /**
  188. * Is response a redirect
  189. * - Checks status codes
  190. *
  191. * @access public
  192. * @return boolean
  193. */
  194. public function isRedirect()
  195. {
  196. $status = $this->getStatus();
  197. return (bool) ($status >= 300 && $status <= 307);
  198. }
  199. /**
  200. * Get time string
  201. *
  202. * @access public
  203. * @return string
  204. */
  205. public function getTime()
  206. {
  207. return $this->time;
  208. }
  209. /**
  210. * Get console messages
  211. *
  212. * @access public
  213. * @return array
  214. */
  215. public function getConsole()
  216. {
  217. return $this->console;
  218. }
  219. }