1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201:
<?php
/**
* MvcCore
*
* This source file is subject to the BSD 3 License
* For the full copyright and license information, please view
* the LICENSE.md file that are distributed with this source code.
*
* @copyright Copyright (c) 2016 Tom Flidr (https://github.com/mvccore)
* @license https://mvccore.github.io/docs/mvccore/5.0.0/LICENCE.md
*/
namespace MvcCore\Response;
trait PropsGettersSetters {
protected static $codeMessages = [
\MvcCore\IResponse::OK => 'OK',
\MvcCore\IResponse::MOVED_PERMANENTLY => 'Moved Permanently',
\MvcCore\IResponse::SEE_OTHER => 'See Other',
\MvcCore\IResponse::NOT_FOUND => 'Not Found',
\MvcCore\IResponse::INTERNAL_SERVER_ERROR => 'Internal Server Error',
];
/**
* Response HTTP protocol version by `$_SERVER['SERVER_PROTOCOL']`.
* Example: `HTTP/1.0 | HTTP/1.1 | HTTP/2 | SPDY`
* @var string|NULL
*/
protected $httpVersion = NULL;
/**
* Response HTTP code.
* Example: `200 | 301 | 404`
* @var int|NULL
*/
protected $code = NULL;
/**
* Optional response HTTP code message.
* @var string|NULL
*/
protected $codeMessage = NULL;
/**
* Response HTTP headers as `key => value` array.
* Example:
* `array(
* 'Content-Type' => 'text/html',
* 'Content-Encoding' => 'utf-8'
* );`
* @var \string[]
*/
protected $headers = [];
/**
* Response content encoding.
* Example: `"utf-8" | "windows-1250" | "ISO-8859-2"`
* @var \string|NULL
*/
protected $encoding = NULL;
/**
* Response HTTP body.
* Example: `"<!DOCTYPE html><html lang="en"><head><meta ..."`
* @var \string|NULL
*/
protected $body = NULL;
/**
* `TRUE` if headers or body has been sent.
* @var bool
*/
protected $bodySent = FALSE;
/**
* Disabled headers, never sent except if there is
* rendered exception in development environment.
* @var array
*/
protected $disabledHeaders = [];
/**
* Reference to current application request object.
* @var \MvcCore\Request
*/
protected $request = NULL;
/**
* @inheritDocs
* @return string
*/
public function GetHttpVersion () {
/** @var $this \MvcCore\Response */
if ($this->httpVersion === NULL) {
$server = & $this->request->GetGlobalCollection('server');
$this->httpVersion = isset($server['SERVER_PROTOCOL'])
? $server['SERVER_PROTOCOL']
: 'HTTP/1.1';
}
return $this->httpVersion;
}
/**
* @inheritDocs
* @param string $httpVersion
* @return \MvcCore\Response
*/
public function SetHttpVersion ($httpVersion) {
/** @var $this \MvcCore\Response */
$this->httpVersion = $httpVersion;
return $this;
}
/**
* @inheritDocs
* @param int $code
* @param string|NULL $codeMessage
* @return \MvcCore\Response
*/
public function SetCode ($code, $codeMessage = NULL) {
/** @var $this \MvcCore\Response */
$this->code = $code;
if ($codeMessage !== NULL) $this->codeMessage = $codeMessage;
http_response_code($code);
return $this;
}
/**
* @inheritDocs
* @return int
*/
public function GetCode () {
/** @var $this \MvcCore\Response */
if ($this->code === NULL) {
$phpCode = http_response_code();
$this->code = $phpCode === FALSE ? \MvcCore\IResponse::OK : $phpCode;
}
return $this->code;
}
/**
* @inheritDocs
* @param string $encoding
* @return \MvcCore\Response
*/
public function SetEncoding ($encoding = 'utf-8') {
/** @var $this \MvcCore\Response */
$this->encoding = $encoding;
$this->headers['Content-Encoding'] = $encoding;
header('Content-Encoding: ' . $encoding);
if (isset($this->headers['Content-Type']))
header('Content-Type: ' . $this->headers['Content-Type'] . '; charset=' . $encoding);
return $this;
}
/**
* @inheritDocs
* @return string|NULL
*/
public function GetEncoding () {
/** @var $this \MvcCore\Response */
if ($this->encoding === NULL) {
if (isset($this->headers['Content-Encoding'])) {
$this->encoding = $this->headers['Content-Encoding'];
} else if (isset($this->headers['Content-Type'])) {
$value = $this->headers['Content-Type'];
$charsetPos = strpos($value, 'charset');
if ($charsetPos !== FALSE) {
$equalPos = strpos($value, '=', $charsetPos);
if ($equalPos !== FALSE)
$this->encoding = trim(substr($value, $equalPos + 1));
}
}
if (!$this->encoding)
$this->encoding = 'utf-8';
}
return $this->encoding;
}
/**
* @inheritDocs
* @return bool
*/
public function IsRedirect () {
/** @var $this \MvcCore\Response */
return isset($this->headers['Location']);
}
/**
* @inheritDocs
* @return bool
*/
public function IsSent () {
/** @var $this \MvcCore\Response */
return $this->bodySent && headers_sent();
}
}