Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
UResponse.php
Go to the documentation of this file.
1<?php
2
3namespace Ubiquity\utils\http;
4
6
17class UResponse {
18 public static $headers = [ ];
19
28 public static function header($headerField, $value, bool $replace = true, int $responseCode = 0): void {
29 self::$headers [trim ( $headerField )] = trim ( $value );
30 Startup::getHttpInstance ()->header ( trim ( $headerField ), trim ( $value ), $replace, $responseCode );
31 }
32
38 public static function forward(string $url,bool $preserveUrl=true): void {
39 self::header('Location',$preserveUrl?$url:URequest::getUrl($url));
40 }
41
47 private static function _headerArray($headerField, $values): void {
48 if (\is_array ( $values )) {
49 $values = \implode ( ', ', $values );
50 }
51 self::header ( $headerField, $values );
52 }
53
60 public static function setContentType($contentType, $encoding = null): void {
61 $value = $contentType;
62 if (isset ( $encoding ))
63 $value .= '; charset=' . $encoding;
64 self::header ( 'Content-Type', $value, true );
65 }
66
70 public static function noCache(): void {
71 self::header ( 'Cache-Control', 'no-cache, must-revalidate' );
72 self::header ( 'Expires', 'Sat, 26 Jul 1997 05:00:00 GMT' );
73 }
74
82 public static function isSent(&$file = null, &$line = null): bool {
83 return Startup::getHttpInstance ()->headersSent ( $file, $line );
84 }
85
89 public static function asJSON(): void {
90 self::header ( 'Content-Type', 'application/json' );
91 }
92
99 public static function isJSON(): bool {
100 return isset ( self::$headers ['Content-Type'] ) && self::$headers ['Content-Type'] === 'application/json';
101 }
102
108 public static function asHtml($encoding = 'utf-8'): void {
109 self::setContentType ( 'text/html', $encoding );
110 }
111
117 public static function asXml($encoding = 'utf-8'): void {
118 self::setContentType ( 'application/xml', $encoding );
119 }
120
126 public static function asText($encoding = 'utf-8'): void {
127 self::setContentType ( 'plain/text', $encoding );
128 }
129
136 public static function setAccept($value): void {
137 self::header ( 'Accept', $value );
138 }
139
148 public static function enableCors($origin = '*', $methods = 'GET, POST, PUT, DELETE, PATCH, OPTIONS', $headers = 'X-Requested-With, Content-Type, Accept, Origin, Authorization'): void {
149 self::setAccessControlOrigin ( $origin );
150 self::setAccessControlMethods ( $methods );
151 self::setAccessControlHeaders ( $headers );
152 }
153
160 public static function setAccessControlOrigin($origin = '*'): void {
161 self::header ( 'Access-Control-Allow-Origin', $origin );
162 if ($origin !== '*') {
163 self::header ( 'Vary', 'Origin' );
164 }
165 }
166
172 public static function setAccessControlMethods($methods): void {
173 self::_headerArray ( 'Access-Control-Allow-Methods', $methods );
174 }
175
181 public static function setAccessControlHeaders($headers): void {
182 self::_headerArray ( 'Access-Control-Allow-Headers', $headers );
183 }
184
190 public static function setAuthorization($authorization): void {
191 self::header ( 'Authorization', $authorization );
192 }
193
199 public static function setResponseCode($value) {
200 return \http_response_code ( $value );
201 }
202
208 public static function getResponseCode() {
209 return \http_response_code ();
210 }
211}
Starts the framework.
Definition Startup.php:19
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.
Definition URequest.php:18
Http Response utilities.
Definition UResponse.php:17
static isJSON()
Tests if response content-type is application/json Only Works if UResponse has been used for setting ...
Definition UResponse.php:99
static asHtml($encoding='utf-8')
Sets the response content-type to text/html.
static enableCors($origin=' *', $methods='GET, POST, PUT, DELETE, PATCH, OPTIONS', $headers='X-Requested-With, Content-Type, Accept, Origin, Authorization')
Enables CORS.
static forward(string $url, bool $preserveUrl=true)
Forwards to url using Location header.
Definition UResponse.php:38
static isSent(&$file=null, &$line=null)
Checks if or where headers have been sent.
Definition UResponse.php:82
static setAccessControlHeaders($headers)
Sets the Access-Control-Allow-Headers field value.
static setAccessControlOrigin($origin=' *')
Sets the Access-Control-Allow-Origin field value Only a single origin can be specified.
static asText($encoding='utf-8')
Sets the response content-type to plain/text.
static getResponseCode()
Get the response code.
static setAccept($value)
Sets the Accept header.
static header($headerField, $value, bool $replace=true, int $responseCode=0)
Send a raw HTTP header.
Definition UResponse.php:28
static setAccessControlMethods($methods)
Sets the Access-Control-Allow-Methods field value.
static asXml($encoding='utf-8')
Sets the response content-type to application/xml.
static setAuthorization($authorization)
Set the Authorization header field.
static setContentType($contentType, $encoding=null)
Sets header content-type.
Definition UResponse.php:60
static noCache()
Forces the disabling of the browser cache.
Definition UResponse.php:70
static _headerArray($headerField, $values)
Definition UResponse.php:47
static setResponseCode($value)
Sets the response code.
static asJSON()
Sets the response content-type to application/json.
Definition UResponse.php:89