/lib/Tasty/Response/Response.php

https://bitbucket.org/Alex_PK/tasty · PHP · 359 lines · 184 code · 48 blank · 127 comment · 15 complexity · f3e3a5c380056d5dad072304d6a36672 MD5 · raw file

  1. <?php
  2. namespace Tasty\Response;
  3. use Tasty\Model\ArrayAccessTrait;
  4. use Tasty\Model\HttpInfo;
  5. use Tasty\View\Error;
  6. use Tasty\View\View;
  7. class Response implements \ArrayAccess, \Iterator, \Serializable, \JsonSerializable
  8. {
  9. use ArrayAccessTrait;
  10. protected $_mime = 'text/html';
  11. protected $_lang = null;
  12. protected $_charset = null;
  13. protected $_headers = array();
  14. protected $_body = null;
  15. protected $_resultCode = 200;
  16. protected $_resultMsg = null;
  17. protected $_view = null;
  18. protected $_baseUrl = '';
  19. protected $_protocol = 'http';
  20. protected $_hostname = '';
  21. /**
  22. * @param null|array $config
  23. */
  24. public function __construct($config=null)
  25. {
  26. if ( ! is_array($config)) {
  27. $config = [$config];
  28. }
  29. if ( array_key_exists('baseUrl', $config)) {
  30. $this->_baseUrl = (string)$config['baseUrl'];
  31. }
  32. if ( array_key_exists('hostname', $config)) {
  33. $this->_hostname = (string)$config['hostname'];
  34. }
  35. if ( array_key_exists('protocol', $config)) {
  36. $this->_protocol = (string)$config['protocol'];
  37. }
  38. }
  39. /**
  40. * Sets a "part" variable, that is nothing else than a normal variable, prefixed with "parts:"
  41. *
  42. * @param $name
  43. * @param $value
  44. *
  45. * @return $this
  46. */
  47. public function setPart($name, $value)
  48. {
  49. $this->set('parts:'.$name, $value);
  50. return $this;
  51. }
  52. /**
  53. * Gets a "part" variable, that is nothing else than a normal variable, prefixed with "parts:"
  54. * The variable is unescaped
  55. *
  56. * @param $name
  57. * @param string $default
  58. *
  59. * @return mixed|null|string
  60. */
  61. public function getPart($name, $default='')
  62. {
  63. return $this->get('parts:'.$name, $default);
  64. }
  65. /**
  66. * Gets the baseUrl, set from a bootstrap or App
  67. *
  68. * @return string
  69. */
  70. public function getBaseUrl()
  71. {
  72. return $this->_baseUrl;
  73. }
  74. /**
  75. * Sets the new baseUrl
  76. *
  77. * @param string $url
  78. *
  79. * @return $this
  80. */
  81. public function setBaseUrl($url)
  82. {
  83. $this->_baseUrl = (string)$url;
  84. return $this;
  85. }
  86. /**
  87. * Generate a full Url appending the baseUrl in front of it
  88. * TODO: does it belong here?
  89. *
  90. * @param string $path
  91. * @param bool $full
  92. * @return string
  93. */
  94. public function baseUrl($path='', $full=false)
  95. {
  96. $path = preg_replace('#^\/*#u', '', $path); // remove leading slashes
  97. $baseUrl = $this->getBaseUrl();
  98. $baseUrl = preg_replace('#\/*$#u', '', $baseUrl) . '/'; // remove trailing slashes, then add one
  99. if ($full) {
  100. $urlRoot = $this->getProtocol() .'://'. $this->getHostname();
  101. $baseUrl = $urlRoot . $baseUrl;
  102. }
  103. return $baseUrl . $path;
  104. }
  105. public function getProtocol()
  106. {
  107. return $this->_protocol;
  108. }
  109. public function getHostname()
  110. {
  111. return $this->_hostname;
  112. }
  113. /**
  114. * @param int $code
  115. * @param string $msg
  116. *
  117. * @return $this
  118. */
  119. public function setStatus($code=200, $msg=null)
  120. {
  121. $this->_resultCode = intval($code);
  122. $this->_resultMsg = strval($msg);
  123. return $this;
  124. }
  125. /**
  126. * @return int
  127. */
  128. public function getStatus()
  129. {
  130. return $this->_resultCode;
  131. }
  132. /**
  133. * TODO: strictly coupled!
  134. *
  135. * @return string
  136. */
  137. public function getStatusMsg()
  138. {
  139. if ( is_null($this->_resultMsg) ) {
  140. return HttpInfo::statusMsg($this->_resultCode);
  141. } else {
  142. return $this->_resultMsg;
  143. }
  144. }
  145. /**
  146. * @param string|null $mime
  147. *
  148. * @return $this
  149. */
  150. public function setMime($mime)
  151. {
  152. $this->_mime = $mime;
  153. return $this;
  154. }
  155. /**
  156. * @return bool
  157. */
  158. public function hasMime()
  159. {
  160. return ! is_null($this->_mime);
  161. }
  162. /**
  163. * @return string|null
  164. */
  165. public function getMime()
  166. {
  167. return $this->_mime;
  168. }
  169. /**
  170. * @param string|null $lang
  171. *
  172. * @return $this
  173. */
  174. public function setLang($lang)
  175. {
  176. $this->_lang = $lang;
  177. return $this;
  178. }
  179. /**
  180. * @return bool
  181. */
  182. public function hasLang()
  183. {
  184. return ! is_null($this->_lang);
  185. }
  186. /**
  187. * @return string|null
  188. */
  189. public function getLang()
  190. {
  191. return $this->_lang;
  192. }
  193. /**
  194. * @param string|null $charset
  195. *
  196. * @return $this
  197. */
  198. public function setCharset($charset)
  199. {
  200. $this->_charset = $charset;
  201. return $this;
  202. }
  203. /**
  204. * @return bool
  205. */
  206. public function hasCharset()
  207. {
  208. return ! is_null($this->_charset);
  209. }
  210. /**
  211. * @param string|null $default
  212. *
  213. * @return string
  214. *
  215. * @throws Exception\Value
  216. */
  217. public function getCharset($default='utf-8')
  218. {
  219. if ( ! $this->hasCharset()) {
  220. if (is_null($default)) {
  221. throw new Exception\Value('Charset not defined');
  222. } else {
  223. return $default;
  224. }
  225. }
  226. return $this->_charset;
  227. }
  228. /**
  229. * @param string $k The header to set
  230. * @param string $v The header value
  231. *
  232. * @return Response $this
  233. */
  234. public function addHeader($k, $v)
  235. {
  236. $this->_headers[] = array('k'=>$k, 'v'=>$v);
  237. return $this;
  238. }
  239. /**
  240. * @return array
  241. */
  242. public function getHeaders()
  243. {
  244. return $this->_headers;
  245. }
  246. /**
  247. * Give a View that will process response before output.
  248. * You can use whatever you want, it just needs to have a method render() that returns a string.
  249. * Your object can accept a Response object to get response variables
  250. *
  251. * @param object|null $view
  252. *
  253. * @return $this
  254. *
  255. * @throws Exception\NotValid
  256. */
  257. public function setView($view=null)
  258. {
  259. if ( ! is_object($view) && ! is_null($view) ) {
  260. throw new Exception\NotValid('Must give a valid View object or null to disable.');
  261. }
  262. if ( is_object($view) && ! method_exists($view, 'render')) {
  263. throw new Exception\NotValid('Given View does not support rendering.');
  264. }
  265. $this->_view = $view;
  266. return $this;
  267. }
  268. /**
  269. * Returns the view object associated with this response
  270. *
  271. * @return View
  272. */
  273. public function getView()
  274. {
  275. return $this->_view;
  276. }
  277. /**
  278. * Does the response have a view to preprocess response before output?
  279. *
  280. * @return bool
  281. */
  282. public function hasView()
  283. {
  284. return ( ! is_null($this->_view) );
  285. }
  286. /*
  287. * Interfaces implementation
  288. */
  289. public function serialize()
  290. {
  291. return serialize($this->_data);
  292. }
  293. public function unserialize($serialized)
  294. {
  295. $this->_data = unserialize($serialized);
  296. }
  297. public function jsonSerialize()
  298. {
  299. return $this->_data;
  300. }
  301. /**
  302. * @return View
  303. */
  304. public function render()
  305. {
  306. if ( ! $this->hasView()) {
  307. $this->setView( new Error("No view found\n<pre>".print_r($this,true).'</pre>') );
  308. }
  309. return $this->getView();
  310. }
  311. }