Go to the documentation of this file.00001 <?php
00017 class axResponse {
00018
00023 protected $_view;
00024
00029 protected $_viewSection;
00030
00035 protected $_viewFormat;
00036
00041 protected $_viewLayout;
00042
00047 protected $_layoutEnabled;
00048
00053 protected $_vars;
00054
00059 protected $_headers;
00060
00068 protected $_outputCallback;
00069
00075 protected $_filter;
00076
00084 protected $_filterFlag;
00085
00090 protected $_messages;
00091
00096 protected $_styleSheets;
00097
00102 protected $_scripts;
00103
00107 public function __construct () {
00108 $this->_layoutEnabled = true;
00109 $this->_vars = array();
00110 $this->_headers = array();
00111 $this->_messages = array();
00112 $this->_styleSheets = array();
00113 $this->_scripts = array();
00114 }
00115
00120 public function reset () {
00121 $this->_view = null;
00122 $this->_viewSection = null;
00123 $this->_viewFormat = null;
00124 $this->_viewLayout = null;
00125 $this->_outputCallback = null;
00126 $this->_filter = null;
00127 $this->_layoutEnabled = true;
00128 $this->_vars = array();
00129 $this->_headers = array();
00130 $this->_messages = array();
00131 $this->_styleSheets = array();
00132 $this->_scripts = array();
00133 }
00134
00142 public function getView () {
00143 return $this->_view;
00144 }
00145
00151 public function setView ($view) {
00152 $this->_view = $view;
00153 return $this;
00154 }
00155
00163 public function getViewSection () {
00164 return $this->_viewSection;
00165 }
00166
00172 public function setViewSection ($section) {
00173 $this->_viewSection = $section;
00174 return $this;
00175 }
00176
00184 public function getFormat () {
00185 return $this->_viewFormat;
00186 }
00187
00193 public function setFormat ($format) {
00194 $this->_viewFormat = $format;
00195 return $this;
00196 }
00197
00205 public function getLayout () {
00206 return $this->_viewLayout;
00207 }
00208
00214 public function setLayout ($layout) {
00215 $this->_viewLayout = $layout;
00216 return $this;
00217 }
00218
00224 public function enableLayout ($enabled = true) {
00225 $this->_layoutEnabled = (boolean)$enabled;
00226 return $this;
00227 }
00228
00233 public function layoutState () {
00234 return $this->_layoutEnabled;
00235 }
00236
00247 public function getVar ($name) {
00248 if ($this->_filterFlag)
00249 $this->_applyFilter();
00250
00251 return isset($this->_vars[$name]) ? $this->_vars[$name] : null;
00252 }
00253
00260 public function setVar ($name, $value) {
00261 $this->_vars[(string)$name] = $value;
00262 $this->_filterFlag = true;
00263 return $this;
00264 }
00265
00273 public function __get ($key) {
00274 return $this->getVar($key);
00275 }
00276
00285 public function __set ($key, $value) {
00286 $this->setVar($key,$value);
00287 }
00288
00299 public function getVars () {
00300 if ($this->_filterFlag)
00301 $this->_applyFilter();
00302
00303 return $this->_vars;
00304 }
00305
00316 public function add ($collection, $method = self::MERGE_VARS) {
00317 if (!$collection)
00318 return $this;
00319
00320 switch ($method) {
00321 case self::MERGE_VARS:
00322 $this->_vars = array_merge($this->_vars, $collection);
00323 break;
00324 case self::ADD_VARS:
00325 $this->_vars += $vars;
00326 break;
00327 default:
00328 return false;
00329 }
00330 $this->_filterFlag = true;
00331 return $this;
00332 }
00333
00338 public function clearVars () {
00339 $this->_vars = array();
00340 return $this;
00341 }
00342
00353 public function setHeader ($field, $value) {
00354 $this->_headers[$field] = "{$field}: {$value}";
00355 return $this;
00356 }
00357
00375 public function setHaders (array $headers) {
00376 foreach ($headers as $field => $value) {
00377 $this->setHeader($field, $value);
00378 }
00379 return $this;
00380 }
00381
00388 public function setStatus ($http_status) {
00389 $this->_headers['status'] = $http_status;
00390 return $this;
00391 }
00392
00397 public function getHeaders () {
00398 return array_unique($this->_headers);
00399 }
00400
00405 public function clearHeaders () {
00406 $this->_headers = array();
00407 return $this;
00408 }
00409
00427 public function setOutputCallback ($callback) {
00428 if (!is_callable($this->_outputCallback = $callback)) {
00429 $this->_outputCallback = null;
00430 return false;
00431 }
00432 return $this;
00433 }
00434
00442 public function getOutputCallback () {
00443 return $this->_outputCallback;
00444 }
00445
00450 public function hasFilter () {
00451 return isset($this->_filter);
00452 }
00453
00460 public function getFilter () {
00461 return $this->_filter;
00462 }
00463
00482 public function setFilter (array $filter) {
00483 $this->_filter = $filter;
00484 $this->_filterFlag = true;
00485 return $this;
00486 }
00487
00499 public function addMessage ($message, $level = self::MESSAGE_WARNING) {
00500 if (!isset($this->_messages[$level]))
00501 $this->_messages[$level] = array();
00502
00503 $this->_messages[$level][(string)$message] = (string)$message;
00504 return $this;
00505 }
00506
00512 public function removeMessage ($message) {
00513 foreach ($this->_messages as $level => $messages) {
00514 if (isset($messages[(string)$message]))
00515 unset($this->_messages[$level][(string)$message]);
00516 }
00517 return $this;
00518 }
00519
00528 public function getMessages ($level = null) {
00529 if ($level)
00530 return isset($this->_messages[$level]) ? $this->_messages[$level] : array();
00531 else
00532 return $this->_messages;
00533 }
00534
00539 public function clearMessages () {
00540 $this->_messages = array();
00541 return $this;
00542 }
00543
00551 public function addStyleSheet ($stylesheet, $type= "text/css", $media = "screen") {
00552 $this->_styleSheets[$stylesheet] = array(
00553 'href' => $stylesheet,
00554 'type' => $type,
00555 'media' => $media
00556 );
00557 return $this;
00558 }
00559
00566 public function removeStyleSheet ($stylehseet) {
00567 unset($this->_styleSheets[$stylesheet]);
00568 return $this;
00569 }
00570
00575 public function clearStyleSheets () {
00576 $this->_styleSheets = array();
00577 return $this;
00578 }
00579
00586 public function addScript ($script, $type = "text/javascript") {
00587 $this->_scripts[$script] = array(
00588 'src' => $script,
00589 'type' => $type
00590 );
00591 return $this;
00592 }
00593
00600 public function removeScript ($script) {
00601 unset($this->_scripts[$script]);
00602 return $this;
00603 }
00604
00609 public function clearScripts () {
00610 $this->_scripts = array();
00611 return $this;
00612 }
00613
00629 public function setCookie ($name,
00630 $value = "",
00631 $expire = 0,
00632 $path = "",
00633 $domain = "",
00634 $secure = false,
00635 $httponly = false) {
00636 return setcookie($name,$value,$expire,$path,$domain,$secure,$httponly) ? $this : false;
00637 }
00638
00654 public function setRawCookie ($name,
00655 $value = "",
00656 $expire = 0,
00657 $path = "",
00658 $domain = "",
00659 $secure = false,
00660 $httponly = false) {
00661 return setrawcookie($name,$value,$expire,$path,$domain,$secure,$httponly) ? $this : false;
00662 }
00663
00674 protected function _applyFilter () {
00675 if (!isset($this->_filter))
00676 return false;
00677
00678 $this->_vars = filter_var_array($this->_vars, $this->_filter);
00679 $this->_filterFlag = false;
00680 if (!$this->_vars) {
00681 $this->clearVars();
00682 $this->_filter = null;
00683 throw new RuntimeException("Incorrect filter definition");
00684 }
00685 return true;
00686 }
00687
00693 const MERGE_VARS = "merge";
00694 const ADD_VARS = "add";
00695
00700 const MESSAGE_NOTICE = "notice";
00701 const MESSAGE_WARNING = "warning";
00702 const MESSAGE_ALERT = "alert";
00703
00710 const HEADER_ACCEPT_RANGE = "Accept-Ranges";
00711 const HEADER_AGE = "Age";
00712 const HEADER_ALLOW = "Allow";
00713 const HEADER_CACHE_CONTROL = "Cache-Control";
00714 const HEADER_CONNECTION = "Connection";
00715 const HEADER_CONTENT_ENCODING = "Content-Encoding";
00716 const HEADER_CONTENT_LANGUAGE = "Content-Language";
00717 const HEADER_CONTENT_LENGTH = "Content-Length";
00718 const HEADER_CONTENT_LOCATION = "Content-Location";
00719 const HEADER_CONTENT_MD5 = "Content-MD5";
00720 const HEADER_CONTENT_DISPOSITION = "Content-Disposition";
00721 const HEADER_CONTENT_RANGE = "Content-Range";
00722 const HEADER_CONTENT_TYPE = "Content-Type";
00723 const HEADER_DATE = "Date";
00724 const HEADER_ETAG = "ETag";
00725 const HEADER_EXPIRES = "Expires";
00726 const HEADER_LAST_MODIFIED = "Last-Modified";
00727 const HEADER_LINK = "Link";
00728 const HEADER_LOCATION = "Location";
00729 const HEADER_P3P = "P3P";
00730 const HEADER_PRAGMA = "Pragma";
00731 const HEADER_PROXY_AUTHENTICATE = "Proxy-Authenticate";
00732 const HEADER_REFRESH = "Refresh";
00733 const HEADER_RETRY_AFTER = "Retry-After";
00734 const HEADER_SERVER = "Server";
00735 const HEADER_SET_COOKIE = "Set-Cookie";
00736 const HEADER_STS = "Strict-Transport-Security";
00737 const HEADER_TRAILER = "Trailer";
00738 const HEADER_TRANSFER_ENCODING = "Transfer-Encoding";
00739 const HEADER_VARY = "Vary";
00740 const HEADER_VIA = "Via";
00741 const HEADER_WARNING = "Warning";
00742 const HEADER_WWW_AUTHENTICATE = "WWW-Authenticate";
00743 }