Completed
Push — master ( 37883e...733fa6 )
by Jean-Christophe
01:30
created

UResponse::_headerArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
namespace Ubiquity\utils\http;
4
5
/**
6
 * Http Response utilities
7
 * @author jc
8
 * @version 1.0.0.0
9
 *
10
 */
11
class UResponse {
12
13
	/**
14
	 * Send a raw HTTP header
15
	 * @param string $headerField the header field
16
	 * @param string $value the header value
17
	 * @param boolean $replace The optional replace parameter indicates whether the header should replace a previous similar header
18
	 * @param int $responseCode Forces the HTTP response code to the specified value
19
	 */
20
	public static function header($headerField, $value, $replace=null, $responseCode=null) {
0 ignored issues
show
Unused Code introduced by
The parameter $responseCode is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
21
		\header(trim($headerField) . ": " . trim($value), $replace);
22
	}
23
24
	/**
25
	 *
26
	 * @param string $headerField
27
	 * @param mixed $values
28
	 */
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
43
	/**
44
	 * Forces the disabling of the browser cache
45
	 */
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
51
	/**
52
	 * Checks if or where headers have been sent
53
	 * @return boolean
54
	 */
55
	public static function isSent() {
56
		return \headers_sent();
57
	}
58
59
	/**
60
	 * Sets the response content-type to application/json
61
	 */
62
	public static function asJSON() {
63
		self::header('Content-Type', 'application/json');
64
	}
65
66
	/**
67
	 * Sets the response content-type to text/html
68
	 * @param string $encoding default: utf-8
69
	 */
70
	public static function asHtml($encoding='utf-8') {
71
		self::setContentType('text/html', $encoding);
72
	}
73
74
	/**
75
	 * Sets the response content-type to application/xml
76
	 * @param string $encoding default: utf-8
77
	 */
78
	public static function asXml($encoding='utf-8') {
79
		self::setContentType('application/xml', $encoding);
80
	}
81
82
	/**
83
	 * Sets the response content-type to plain/text
84
	 * @param string $encoding default: utf-8
85
	 */
86
	public static function asText($encoding='utf-8') {
87
		self::setContentType('plain/text', $encoding);
88
	}
89
90
	/**
91
	 * Sets the Accept header
92
	 * @param string $value one of Http accept values
93
	 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation/List_of_default_Accept_values
94
	 */
95
	public static function setAccept($value) {
96
		self::header('Accept', $value);
97
	}
98
99
	/**
100
	 * Sets the Access-Control-Allow-Origin field value
101
	 * @param string $origin
102
	 */
103
	public static function setAccessControlOrigin($origin) {
104
		self::header('Access-Control-Allow-Origin', $origin);
105
	}
106
107
	/**
108
	 * Sets the Access-Control-Allow-Methods field value
109
	 * @param string|array $origin
0 ignored issues
show
Bug introduced by
There is no parameter named $origin. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
110
	 */
111
	public static function setAccessControlMethods($methods) {
0 ignored issues
show
Unused Code introduced by
The parameter $methods is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
		self::_headerArray('Access-Control-Allow-Methods', $origin);
0 ignored issues
show
Bug introduced by
The variable $origin does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
113
	}
114
115
	/**
116
	 * Sets the Access-Control-Allow-Headers field value
117
	 * @param string|array $headers
118
	 */
119
	public static function setAccessControlHeaders($headers) {
120
		self::_headerArray('Access-Control-Allow-Headers', $headers);
121
	}
122
123
	/**
124
	 * Set the Authorization header field
125
	 * @param string $authorization
126
	 */
127
	public static function setAuthorization($authorization) {
128
		self::header('Authorization', $authorization);
129
	}
130
131
	/**
132
	 * Sets the response code
133
	 * @param int $value
134
	 */
135
	public static function setResponseCode($value) {
136
		\http_response_code($value);
137
	}
138
139
	/**
140
	 * Get the response code
141
	 * @return int
142
	 */
143
	public static function getResponseCode() {
144
		return \http_response_code();
145
	}
146
}
147