Ubiquity  2.0.0
php rapid development framework
UResponse.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ubiquity\utils\http;
4 
11 class UResponse {
12 
20  public static function header($headerField, $value, $replace=null, $responseCode=null) {
21  \header(trim($headerField) . ": " . trim($value), $replace);
22  }
23 
29  private static function _headerArray($headerField, $values) {
30  if (\is_array($values)) {
31  $values=\implode(", ", $values);
32  }
33  self::header($headerField, $values);
34  }
35 
36  public static function setContentType($contentType, $encoding=null) {
37  $value=$contentType;
38  if (isset($encoding))
39  $value.=' ;' . $encoding;
40  self::header('content-type', $value);
41  }
42 
46  public static function noCache() {
47  self::header('Cache-Control', 'no-cache, must-revalidate');
48  self::header('Expires', 'Sat, 26 Jul 1997 05:00:00 GMT');
49  }
50 
55  public static function isSent() {
56  return \headers_sent();
57  }
58 
62  public static function asJSON() {
63  self::header('Content-Type', 'application/json');
64  }
65 
70  public static function asHtml($encoding='utf-8') {
71  self::setContentType('text/html', $encoding);
72  }
73 
78  public static function asXml($encoding='utf-8') {
79  self::setContentType('application/xml', $encoding);
80  }
81 
86  public static function asText($encoding='utf-8') {
87  self::setContentType('plain/text', $encoding);
88  }
89 
95  public static function setAccept($value) {
96  self::header('Accept', $value);
97  }
98 
103  public static function setAccessControlOrigin($origin) {
104  self::header('Access-Control-Allow-Origin', $origin);
105  }
106 
111  public static function setAccessControlMethods($methods) {
112  self::_headerArray('Access-Control-Allow-Methods', $origin);
113  }
114 
119  public static function setAccessControlHeaders($headers) {
120  self::_headerArray('Access-Control-Allow-Headers', $headers);
121  }
122 
127  public static function setAuthorization($authorization) {
128  self::header('Authorization', $authorization);
129  }
130 
135  public static function setResponseCode($value) {
136  \http_response_code($value);
137  }
138 
143  public static function getResponseCode() {
144  return \http_response_code();
145  }
146 }
static setAccept($value)
Sets the Accept header.
Definition: UResponse.php:95
Http Response utilities.
Definition: UResponse.php:11
static asJSON()
Sets the response content-type to application/json.
Definition: UResponse.php:62
static asText($encoding='utf-8')
Sets the response content-type to plain/text.
Definition: UResponse.php:86
static setAccessControlMethods($methods)
Sets the Access-Control-Allow-Methods field value.
Definition: UResponse.php:111
static setAuthorization($authorization)
Set the Authorization header field.
Definition: UResponse.php:127
static setContentType($contentType, $encoding=null)
Definition: UResponse.php:36
static asHtml($encoding='utf-8')
Sets the response content-type to text/html.
Definition: UResponse.php:70
static isSent()
Checks if or where headers have been sent.
Definition: UResponse.php:55
static noCache()
Forces the disabling of the browser cache.
Definition: UResponse.php:46
static getResponseCode()
Get the response code.
Definition: UResponse.php:143
static setAccessControlHeaders($headers)
Sets the Access-Control-Allow-Headers field value.
Definition: UResponse.php:119
$replace
Definition: traits.php:14
static _headerArray($headerField, $values)
Definition: UResponse.php:29
static setResponseCode($value)
Sets the response code.
Definition: UResponse.php:135
static asXml($encoding='utf-8')
Sets the response content-type to application/xml.
Definition: UResponse.php:78
static setAccessControlOrigin($origin)
Sets the Access-Control-Allow-Origin field value.
Definition: UResponse.php:103
static header($headerField, $value, $replace=null, $responseCode=null)
Send a raw HTTP header.
Definition: UResponse.php:20