Ubiquity  2.0.3
php rapid development framework
UResponse.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\utils\http;
4 
12 class UResponse {
13 
14  public static $headers=[];
15 
28  public static function header($headerField, $value, $replace = null, $responseCode = null) {
29  self::$headers[trim ( $headerField )]=trim($value);
30  \header ( trim ( $headerField ) . ": " . trim ( $value ), $replace, $responseCode );
31  }
32 
38  private static function _headerArray($headerField, $values) {
39  if (\is_array ( $values )) {
40  $values = \implode ( ", ", $values );
41  }
42  self::header ( $headerField, $values );
43  }
44 
45  public static function setContentType($contentType, $encoding = null) {
46  $value = $contentType;
47  if (isset ( $encoding ))
48  $value .= ' ;' . $encoding;
49  self::header ( 'content-type', $value );
50  }
51 
55  public static function noCache() {
56  self::header ( 'Cache-Control', 'no-cache, must-revalidate' );
57  self::header ( 'Expires', 'Sat, 26 Jul 1997 05:00:00 GMT' );
58  }
59 
65  public static function isSent() {
66  return \headers_sent ();
67  }
68 
72  public static function asJSON() {
73  self::header ( 'Content-Type', 'application/json' );
74  }
75 
76  public static function isJSON(){
77  return isset(self::$headers["Content-Type"]) && self::$headers["Content-Type"]==='application/json';
78  }
79 
86  public static function asHtml($encoding = 'utf-8') {
87  self::setContentType ( 'text/html', $encoding );
88  }
89 
96  public static function asXml($encoding = 'utf-8') {
97  self::setContentType ( 'application/xml', $encoding );
98  }
99 
106  public static function asText($encoding = 'utf-8') {
107  self::setContentType ( 'plain/text', $encoding );
108  }
109 
117  public static function setAccept($value) {
118  self::header ( 'Accept', $value );
119  }
120 
126  public static function setAccessControlOrigin($origin) {
127  self::header ( 'Access-Control-Allow-Origin', $origin );
128  }
129 
135  public static function setAccessControlMethods($methods) {
136  self::_headerArray ( 'Access-Control-Allow-Methods', $methods );
137  }
138 
144  public static function setAccessControlHeaders($headers) {
145  self::_headerArray ( 'Access-Control-Allow-Headers', $headers );
146  }
147 
153  public static function setAuthorization($authorization) {
154  self::header ( 'Authorization', $authorization );
155  }
156 
162  public static function setResponseCode($value) {
163  \http_response_code ( $value );
164  }
165 
171  public static function getResponseCode() {
172  return \http_response_code ();
173  }
174 }
static setAccept($value)
Sets the Accept header.
Definition: UResponse.php:117
Http Response utilities.
Definition: UResponse.php:12
static asJSON()
Sets the response content-type to application/json.
Definition: UResponse.php:72
static asText($encoding='utf-8')
Sets the response content-type to plain/text.
Definition: UResponse.php:106
static setAccessControlMethods($methods)
Sets the Access-Control-Allow-Methods field value.
Definition: UResponse.php:135
static setAuthorization($authorization)
Set the Authorization header field.
Definition: UResponse.php:153
static setContentType($contentType, $encoding=null)
Definition: UResponse.php:45
static asHtml($encoding='utf-8')
Sets the response content-type to text/html.
Definition: UResponse.php:86
static isSent()
Checks if or where headers have been sent.
Definition: UResponse.php:65
static noCache()
Forces the disabling of the browser cache.
Definition: UResponse.php:55
static getResponseCode()
Get the response code.
Definition: UResponse.php:171
static setAccessControlHeaders($headers)
Sets the Access-Control-Allow-Headers field value.
Definition: UResponse.php:144
$replace
Definition: traits.php:14
static _headerArray($headerField, $values)
Definition: UResponse.php:38
static setResponseCode($value)
Sets the response code.
Definition: UResponse.php:162
static asXml($encoding='utf-8')
Sets the response content-type to application/xml.
Definition: UResponse.php:96
static setAccessControlOrigin($origin)
Sets the Access-Control-Allow-Origin field value.
Definition: UResponse.php:126
static header($headerField, $value, $replace=null, $responseCode=null)
Send a raw HTTP header.
Definition: UResponse.php:28