/library/Smart/Web/Request.php

https://bitbucket.org/emerido/test · PHP · 360 lines · 118 code · 67 blank · 175 comment · 5 complexity · 5ae9c600d8b6a9eaa028d73d9c80bb26 MD5 · raw file

  1. <?php
  2. /**
  3. * Smart - PHP 5 Content Management Framework
  4. *
  5. * @author Shvorak Alexey <dr.emerido@gmail.com>
  6. * @copyright 2013 Shvorak Alexey
  7. * @link http://www.smartframework.com
  8. * @license http://www.smartframework.com/license
  9. * @version 0.0.1
  10. * @package Smart
  11. *
  12. * MIT LICENSE
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining
  15. * a copy of this software and associated documentation files (the
  16. * "Software"), to deal in the Software without restriction, including
  17. * without limitation the rights to use, copy, modify, merge, publish,
  18. * distribute, sublicense, and/or sell copies of the Software, and to
  19. * permit persons to whom the Software is furnished to do so, subject to
  20. * the following rules:
  21. *
  22. * The above copyright notice and this permission notice shall be
  23. * included in all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. */
  33. namespace Smart\Web;
  34. use Smart\Web\Bag;
  35. class Request
  36. {
  37. /**
  38. * Request url data
  39. *
  40. * @var \Smart\Web\Uri
  41. */
  42. public $uri;
  43. /**
  44. * Server variables
  45. *
  46. * @var \Smart\Web\Bag\Server
  47. */
  48. public $server;
  49. /**
  50. * @var \Smart\Web\Bag\Header
  51. */
  52. public $header;
  53. /**
  54. * @var \Smart\Web\Bag
  55. */
  56. public $cookie;
  57. /**
  58. * Path query values
  59. *
  60. * @var \Smart\Web\Bag
  61. */
  62. public $query;
  63. /**
  64. * Post variables
  65. *
  66. * @var \Smart\Web\Bag
  67. */
  68. public $post;
  69. /**
  70. * Request format
  71. *
  72. * @var string
  73. */
  74. public $format;
  75. /**
  76. * Request method
  77. *
  78. * @var string
  79. */
  80. public $method;
  81. /**
  82. * Request body data
  83. *
  84. * @var string
  85. */
  86. public $content;
  87. /**
  88. * Instantiate request object
  89. */
  90. public function __construct()
  91. {
  92. $this->uri = new Uri($_SERVER);
  93. $this->post = new Bag($_POST);
  94. $this->query = new Bag($_GET);
  95. $this->cookie = new Bag($_COOKIE);
  96. $this->server = new Bag\Server($_SERVER);
  97. $this->header = new Bag\Header($this->server->getHeaders());
  98. }
  99. /**
  100. * Returns request format
  101. *
  102. * @return string
  103. */
  104. public function getFormat()
  105. {
  106. if (null === $this->format) {
  107. // You can change default content type
  108. $type = $this->header->get('Content-Type', 'text/html');
  109. // Returns format form mime
  110. $this->format = Mime::getFormat($type);
  111. }
  112. return $this->format;
  113. }
  114. /**
  115. * Setup request format
  116. *
  117. * @param string $format
  118. *
  119. * @return \Smart\Web\Request
  120. */
  121. public function setFormat($format)
  122. {
  123. if (Mime::hasFormat($format)) {
  124. $this->format = (string) $format;
  125. }
  126. return $this;
  127. }
  128. /**
  129. * Returns request content
  130. *
  131. * @return string
  132. */
  133. public function getContent()
  134. {
  135. if (null === $this->content) {
  136. $this->content = trim(file_get_contents('php://input'));
  137. }
  138. return $this->content;
  139. }
  140. public function getJsonContent()
  141. {
  142. return json_decode($this->getContent(), true);
  143. }
  144. /**
  145. * Sets request content
  146. *
  147. * @param string $content
  148. *
  149. * @return \Smart\Web\Request
  150. */
  151. public function setContent($content)
  152. {
  153. if (is_string($content)) {
  154. $this->content = $content;
  155. }
  156. return $this;
  157. }
  158. /**
  159. * Returns request path
  160. *
  161. * @return string
  162. */
  163. public function getPath()
  164. {
  165. return $this->uri->getPath();
  166. }
  167. /**
  168. * Returns request method
  169. *
  170. * @return string
  171. */
  172. public function getMethod()
  173. {
  174. if (null === $this->method) {
  175. $this->method = strtoupper($this->server->get('REQUEST_METHOD', 'GET'));
  176. }
  177. return $this->method;
  178. }
  179. /**
  180. * Sets request method
  181. *
  182. * @param string $method
  183. *
  184. * @return \Smart\Web\Request
  185. */
  186. public function setMethod($method)
  187. {
  188. $this->method = null;
  189. $this->server->set('REQUEST_METHOD', strtoupper($method));
  190. return $this;
  191. }
  192. /**
  193. * Returns methods conformity
  194. *
  195. * @param string $method
  196. *
  197. * @return bool
  198. */
  199. public function isMethod($method)
  200. {
  201. return $this->getMethod() === (string) $method;
  202. }
  203. /**
  204. * This request is GET
  205. *
  206. * @return bool
  207. */
  208. public function isGet()
  209. {
  210. return $this->isMethod('GET');
  211. }
  212. /**
  213. * This request is PUT
  214. *
  215. * @return bool
  216. */
  217. public function isPut()
  218. {
  219. return $this->isMethod('PUT');
  220. }
  221. /**
  222. * This request is HEAD
  223. *
  224. * @return bool
  225. */
  226. public function isHead()
  227. {
  228. return $this->isMethod('HEAD');
  229. }
  230. /**
  231. * This request is POST
  232. *
  233. * @return bool
  234. */
  235. public function isPost()
  236. {
  237. return $this->isMethod('POST');
  238. }
  239. /**
  240. * This request is DELETE
  241. *
  242. * @return bool
  243. */
  244. public function isDelete()
  245. {
  246. return $this->isMethod('DELETE');
  247. }
  248. /**
  249. * This request is OPTIONS
  250. *
  251. * @return bool
  252. */
  253. public function isOptions()
  254. {
  255. return $this->isMethod('OPTIONS');
  256. }
  257. /**
  258. * Return true if request secure (https)
  259. *
  260. * @return bool
  261. */
  262. public function isSecure()
  263. {
  264. return strtolower($this->server->get('HTTPS')) === 'on';
  265. }
  266. /**
  267. * This request from flash
  268. *
  269. * @return bool
  270. */
  271. public function isFlash()
  272. {
  273. return (strstr(strtolower($this->header->get('User-Agent')), ' flash')) ? true : false;
  274. }
  275. /**
  276. * This request is Async
  277. *
  278. * @return bool
  279. */
  280. public function isAjax()
  281. {
  282. return $this->isXhr();
  283. }
  284. /**
  285. * This request is Async
  286. *
  287. * @return bool
  288. */
  289. public function isXhr()
  290. {
  291. return $this->header->get('X-Requested-With') === 'XMLHttpRequest';
  292. }
  293. }