Passed
Push — master ( 367555...dfeadf )
by Jean-Christophe
04:46
created

UResponse::isSent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ubiquity\utils\http;
4
5
/**
6
 * Http Response utilities
7
 *
8
 * @author jc
9
 * @version 1.0.0.0
10
 *
11
 */
12
class UResponse {
13
	
14
	public static $headers=[];
15
16
	/**
17
	 * Send a raw HTTP header
18
	 *
19
	 * @param string $headerField
20
	 *        	the header field
21
	 * @param string $value
22
	 *        	the header value
23
	 * @param boolean $replace
24
	 *        	The optional replace parameter indicates whether the header should replace a previous similar header
25
	 * @param int $responseCode
26
	 *        	Forces the HTTP response code to the specified value
27
	 */
28 2
	public static function header($headerField, $value, $replace = null, $responseCode = null) {
29 2
		self::$headers[trim ( $headerField )]=trim($value);
30 2
		\header ( trim ( $headerField ) . ": " . trim ( $value ), $replace, $responseCode );
31 2
	}
32
33
	/**
34
	 *
35
	 * @param string $headerField
36
	 * @param mixed $values
37
	 */
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 1
	public static function setContentType($contentType, $encoding = null) {
46 1
		$value = $contentType;
47 1
		if (isset ( $encoding ))
48 1
			$value .= ' ;' . $encoding;
49 1
		self::header ( 'content-type', $value );
50 1
	}
51
52
	/**
53
	 * Forces the disabling of the browser cache
54
	 */
55 1
	public static function noCache() {
56 1
		self::header ( 'Cache-Control', 'no-cache, must-revalidate' );
57 1
		self::header ( 'Expires', 'Sat, 26 Jul 1997 05:00:00 GMT' );
58 1
	}
59
60
	/**
61
	 * Checks if or where headers have been sent
62
	 *
63
	 * @return boolean
64
	 */
65
	public static function isSent() {
66
		return \headers_sent ();
67
	}
68
69
	/**
70
	 * Sets the response content-type to application/json
71
	 */
72 1
	public static function asJSON() {
73 1
		self::header ( 'Content-Type', 'application/json' );
74 1
	}
75
	
76
	public static function isJSON(){
77
		return isset(self::$headers["Content-Type"]) && self::$headers["Content-Type"]==='application/json';
78
	}
79
80
	/**
81
	 * Sets the response content-type to text/html
82
	 *
83
	 * @param string $encoding
84
	 *        	default: utf-8
85
	 */
86
	public static function asHtml($encoding = 'utf-8') {
87
		self::setContentType ( 'text/html', $encoding );
88
	}
89
90
	/**
91
	 * Sets the response content-type to application/xml
92
	 *
93
	 * @param string $encoding
94
	 *        	default: utf-8
95
	 */
96 1
	public static function asXml($encoding = 'utf-8') {
97 1
		self::setContentType ( 'application/xml', $encoding );
98 1
	}
99
100
	/**
101
	 * Sets the response content-type to plain/text
102
	 *
103
	 * @param string $encoding
104
	 *        	default: utf-8
105
	 */
106
	public static function asText($encoding = 'utf-8') {
107
		self::setContentType ( 'plain/text', $encoding );
108
	}
109
110
	/**
111
	 * Sets the Accept header
112
	 *
113
	 * @param string $value
114
	 *        	one of Http accept values
115
	 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation/List_of_default_Accept_values
116
	 */
117
	public static function setAccept($value) {
118
		self::header ( 'Accept', $value );
119
	}
120
121
	/**
122
	 * Sets the Access-Control-Allow-Origin field value
123
	 *
124
	 * @param string $origin
125
	 */
126
	public static function setAccessControlOrigin($origin) {
127
		self::header ( 'Access-Control-Allow-Origin', $origin );
128
	}
129
130
	/**
131
	 * Sets the Access-Control-Allow-Methods field value
132
	 *
133
	 * @param string|array $methods
134
	 */
135
	public static function setAccessControlMethods($methods) {
136
		self::_headerArray ( 'Access-Control-Allow-Methods', $methods );
137
	}
138
139
	/**
140
	 * Sets the Access-Control-Allow-Headers field value
141
	 *
142
	 * @param string|array $headers
143
	 */
144
	public static function setAccessControlHeaders($headers) {
145
		self::_headerArray ( 'Access-Control-Allow-Headers', $headers );
146
	}
147
148
	/**
149
	 * Set the Authorization header field
150
	 *
151
	 * @param string $authorization
152
	 */
153
	public static function setAuthorization($authorization) {
154
		self::header ( 'Authorization', $authorization );
155
	}
156
157
	/**
158
	 * Sets the response code
159
	 *
160
	 * @param int $value
161
	 */
162
	public static function setResponseCode($value) {
163
		\http_response_code ( $value );
164
	}
165
166
	/**
167
	 * Get the response code
168
	 *
169
	 * @return int
170
	 */
171
	public static function getResponseCode() {
172
		return \http_response_code ();
173
	}
174
}
175