Request.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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\Message;
  9. use JonnyW\PhantomJs\Message\RequestInterface;
  10. use JonnyW\PhantomJs\Exception\InvalidUrlException;
  11. use JonnyW\PhantomJs\Exception\InvalidMethodException;
  12. /**
  13. * PHP PhantomJs
  14. *
  15. * @author Jon Wenmoth <contact@jonnyw.me>
  16. */
  17. class Request implements RequestInterface
  18. {
  19. /**
  20. * Headers
  21. *
  22. * @var array
  23. */
  24. protected $headers;
  25. /**
  26. * Request data
  27. *
  28. * @var array
  29. */
  30. protected $data;
  31. /**
  32. * Request method
  33. *
  34. * @var string
  35. */
  36. protected $method;
  37. /**
  38. * Request URL
  39. *
  40. * @var string
  41. */
  42. protected $url;
  43. /**
  44. * Internal constructor
  45. *
  46. * @param string $method
  47. * @param string $url
  48. * @return void
  49. */
  50. public function __construct($method = RequestInterface::METHOD_GET, $url = null)
  51. {
  52. $this->headers = array();
  53. $this->data = array();
  54. $this->setMethod($method);
  55. if ($url) {
  56. $this->setUrl($url);
  57. }
  58. }
  59. /**
  60. * Set request method
  61. *
  62. * @param string $method
  63. * @return Request
  64. */
  65. public function setMethod($method)
  66. {
  67. $method = strtoupper($method);
  68. $reflection = new \ReflectionClass('JonnyW\PhantomJs\Message\RequestInterface');
  69. // Validate method
  70. if (!$reflection->hasConstant('METHOD_' . $method)) {
  71. throw new InvalidMethodException(sprintf('Invalid method provided: %s', $method));
  72. }
  73. $this->method = $method;
  74. return $this;
  75. }
  76. /**
  77. * Get request method
  78. *
  79. * @return string
  80. */
  81. public function getMethod()
  82. {
  83. return $this->method;
  84. }
  85. /**
  86. * Set request URL
  87. *
  88. * @param string $url
  89. * @return Request
  90. */
  91. public function setUrl($url)
  92. {
  93. // Validate URL
  94. if (!filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) {
  95. throw new InvalidUrlException(sprintf('Invalid URL provided: %s', $url));
  96. }
  97. $this->url = $url;
  98. return $this;
  99. }
  100. /**
  101. * Get request URL
  102. * - Assembles query string for GET
  103. * and HEAD requests
  104. *
  105. * @return string
  106. */
  107. public function getUrl()
  108. {
  109. if (!in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
  110. return $this->url;
  111. }
  112. $url = $this->url;
  113. // Add query string to URL
  114. if (count($this->data)) {
  115. $url .= false === strpos($url, '?') ? '?' : '&';
  116. $url .= urldecode(http_build_query($this->data));
  117. }
  118. return $url;
  119. }
  120. /**
  121. * Get content body
  122. * - Returns query string if not GET or HEAD
  123. *
  124. * @return string
  125. */
  126. public function getBody()
  127. {
  128. if (in_array($this->getMethod(), array(RequestInterface::METHOD_GET, RequestInterface::METHOD_HEAD))) {
  129. return '';
  130. }
  131. return urldecode(http_build_query($this->getRequestData()));
  132. }
  133. /**
  134. * Set request data
  135. *
  136. * @param array $data
  137. * @return Request
  138. */
  139. public function setRequestData(array $data)
  140. {
  141. $this->data = $data;
  142. return $this;
  143. }
  144. /**
  145. * Get request data
  146. *
  147. * @param boolean $flat
  148. * @return array
  149. */
  150. public function getRequestData($flat = true)
  151. {
  152. if ($flat) {
  153. $this->flattenData($this->data);
  154. }
  155. return $this->data;
  156. }
  157. /**
  158. * Flatten data into single
  159. * dimensional array
  160. *
  161. * @param array $data
  162. * @param string $prefix
  163. * @param string $format
  164. * @return array
  165. */
  166. protected function flattenData(array $data, $prefix = '', $format = '%s')
  167. {
  168. $flat = array();
  169. foreach ($data as $name => $value) {
  170. $ref = $prefix . sprintf($format, $name);
  171. if (is_array($value)) {
  172. $flat += $this->flattenData($value, $ref, '[%s]');
  173. continue;
  174. }
  175. $flat[$ref] = $value;
  176. }
  177. return $flat;
  178. }
  179. /**
  180. * Set headers
  181. *
  182. * @param array $headers
  183. * @return JonnyW\PhantomJs\Message\Request
  184. */
  185. public function setHeaders(array $headers)
  186. {
  187. $this->headers = $headers;
  188. }
  189. /**
  190. * Add single header
  191. *
  192. * @param string $header
  193. * @param string $value
  194. * @return Request
  195. */
  196. public function addHeader($header, $value)
  197. {
  198. $this->headers[$header] = $value;
  199. return $this;
  200. }
  201. /**
  202. * Merge headers with existing
  203. *
  204. * @param array $headers
  205. * @return Request
  206. */
  207. public function addHeaders(array $headers)
  208. {
  209. $this->headers = array_merge($this->headers, $headers);
  210. return $this;
  211. }
  212. /**
  213. * Get request headers
  214. *
  215. * @param string $format
  216. * @return array
  217. */
  218. public function getHeaders($format = 'default')
  219. {
  220. if ($format == 'json') {
  221. return json_encode($this->headers);
  222. }
  223. return $this->headers;
  224. }
  225. }