/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
- <?php
- namespace Tasty\Response;
- use Tasty\Model\ArrayAccessTrait;
- use Tasty\Model\HttpInfo;
- use Tasty\View\Error;
- use Tasty\View\View;
- class Response implements \ArrayAccess, \Iterator, \Serializable, \JsonSerializable
- {
- use ArrayAccessTrait;
- protected $_mime = 'text/html';
- protected $_lang = null;
- protected $_charset = null;
- protected $_headers = array();
- protected $_body = null;
- protected $_resultCode = 200;
- protected $_resultMsg = null;
- protected $_view = null;
- protected $_baseUrl = '';
- protected $_protocol = 'http';
- protected $_hostname = '';
- /**
- * @param null|array $config
- */
- public function __construct($config=null)
- {
- if ( ! is_array($config)) {
- $config = [$config];
- }
- if ( array_key_exists('baseUrl', $config)) {
- $this->_baseUrl = (string)$config['baseUrl'];
- }
- if ( array_key_exists('hostname', $config)) {
- $this->_hostname = (string)$config['hostname'];
- }
- if ( array_key_exists('protocol', $config)) {
- $this->_protocol = (string)$config['protocol'];
- }
- }
- /**
- * Sets a "part" variable, that is nothing else than a normal variable, prefixed with "parts:"
- *
- * @param $name
- * @param $value
- *
- * @return $this
- */
- public function setPart($name, $value)
- {
- $this->set('parts:'.$name, $value);
- return $this;
- }
- /**
- * Gets a "part" variable, that is nothing else than a normal variable, prefixed with "parts:"
- * The variable is unescaped
- *
- * @param $name
- * @param string $default
- *
- * @return mixed|null|string
- */
- public function getPart($name, $default='')
- {
- return $this->get('parts:'.$name, $default);
- }
- /**
- * Gets the baseUrl, set from a bootstrap or App
- *
- * @return string
- */
- public function getBaseUrl()
- {
- return $this->_baseUrl;
- }
- /**
- * Sets the new baseUrl
- *
- * @param string $url
- *
- * @return $this
- */
- public function setBaseUrl($url)
- {
- $this->_baseUrl = (string)$url;
- return $this;
- }
- /**
- * Generate a full Url appending the baseUrl in front of it
- * TODO: does it belong here?
- *
- * @param string $path
- * @param bool $full
- * @return string
- */
- public function baseUrl($path='', $full=false)
- {
- $path = preg_replace('#^\/*#u', '', $path); // remove leading slashes
- $baseUrl = $this->getBaseUrl();
- $baseUrl = preg_replace('#\/*$#u', '', $baseUrl) . '/'; // remove trailing slashes, then add one
- if ($full) {
- $urlRoot = $this->getProtocol() .'://'. $this->getHostname();
- $baseUrl = $urlRoot . $baseUrl;
- }
- return $baseUrl . $path;
- }
- public function getProtocol()
- {
- return $this->_protocol;
- }
- public function getHostname()
- {
- return $this->_hostname;
- }
- /**
- * @param int $code
- * @param string $msg
- *
- * @return $this
- */
- public function setStatus($code=200, $msg=null)
- {
- $this->_resultCode = intval($code);
- $this->_resultMsg = strval($msg);
- return $this;
- }
- /**
- * @return int
- */
- public function getStatus()
- {
- return $this->_resultCode;
- }
- /**
- * TODO: strictly coupled!
- *
- * @return string
- */
- public function getStatusMsg()
- {
- if ( is_null($this->_resultMsg) ) {
- return HttpInfo::statusMsg($this->_resultCode);
- } else {
- return $this->_resultMsg;
- }
- }
- /**
- * @param string|null $mime
- *
- * @return $this
- */
- public function setMime($mime)
- {
- $this->_mime = $mime;
- return $this;
- }
- /**
- * @return bool
- */
- public function hasMime()
- {
- return ! is_null($this->_mime);
- }
- /**
- * @return string|null
- */
- public function getMime()
- {
- return $this->_mime;
- }
- /**
- * @param string|null $lang
- *
- * @return $this
- */
- public function setLang($lang)
- {
- $this->_lang = $lang;
- return $this;
- }
- /**
- * @return bool
- */
- public function hasLang()
- {
- return ! is_null($this->_lang);
- }
- /**
- * @return string|null
- */
- public function getLang()
- {
- return $this->_lang;
- }
- /**
- * @param string|null $charset
- *
- * @return $this
- */
- public function setCharset($charset)
- {
- $this->_charset = $charset;
- return $this;
- }
- /**
- * @return bool
- */
- public function hasCharset()
- {
- return ! is_null($this->_charset);
- }
- /**
- * @param string|null $default
- *
- * @return string
- *
- * @throws Exception\Value
- */
- public function getCharset($default='utf-8')
- {
- if ( ! $this->hasCharset()) {
- if (is_null($default)) {
- throw new Exception\Value('Charset not defined');
- } else {
- return $default;
- }
- }
- return $this->_charset;
- }
- /**
- * @param string $k The header to set
- * @param string $v The header value
- *
- * @return Response $this
- */
- public function addHeader($k, $v)
- {
- $this->_headers[] = array('k'=>$k, 'v'=>$v);
- return $this;
- }
- /**
- * @return array
- */
- public function getHeaders()
- {
- return $this->_headers;
- }
- /**
- * Give a View that will process response before output.
- * You can use whatever you want, it just needs to have a method render() that returns a string.
- * Your object can accept a Response object to get response variables
- *
- * @param object|null $view
- *
- * @return $this
- *
- * @throws Exception\NotValid
- */
- public function setView($view=null)
- {
- if ( ! is_object($view) && ! is_null($view) ) {
- throw new Exception\NotValid('Must give a valid View object or null to disable.');
- }
- if ( is_object($view) && ! method_exists($view, 'render')) {
- throw new Exception\NotValid('Given View does not support rendering.');
- }
- $this->_view = $view;
- return $this;
- }
- /**
- * Returns the view object associated with this response
- *
- * @return View
- */
- public function getView()
- {
- return $this->_view;
- }
- /**
- * Does the response have a view to preprocess response before output?
- *
- * @return bool
- */
- public function hasView()
- {
- return ( ! is_null($this->_view) );
- }
- /*
- * Interfaces implementation
- */
- public function serialize()
- {
- return serialize($this->_data);
- }
- public function unserialize($serialized)
- {
- $this->_data = unserialize($serialized);
- }
- public function jsonSerialize()
- {
- return $this->_data;
- }
- /**
- * @return View
- */
- public function render()
- {
- if ( ! $this->hasView()) {
- $this->setView( new Error("No view found\n<pre>".print_r($this,true).'</pre>') );
- }
- return $this->getView();
- }
- }