Completed
Push — master ( 3c1426...32b61b )
by Jean-Christophe
01:45
created

Response::setAccessControlMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
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 Response {
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 plain/text
76
	 * @param string $encoding default: utf-8
77
	 */
78
	public static function asText($encoding='utf-8') {
79
		self::setContentType('plain/text', $encoding);
80
	}
81
82
	/**
83
	 * Sets the Accept header
84
	 * @param string $value one of Http accept values
85
	 * @see https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation/List_of_default_Accept_values
86
	 */
87
	public static function setAccept($value) {
88
		self::header('Accept', $value);
89
	}
90
91
	/**
92
	 * Sets the Access-Control-Allow-Origin field value
93
	 * @param string $origin
94
	 */
95
	public static function setAccessControlOrigin($origin) {
96
		self::header('Access-Control-Allow-Origin', $origin);
97
	}
98
99
	/**
100
	 * Sets the Access-Control-Allow-Methods field value
101
	 * @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...
102
	 */
103
	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...
104
		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...
105
	}
106
107
	/**
108
	 * Sets the Access-Control-Allow-Headers field value
109
	 * @param string|array $headers
110
	 */
111
	public static function setAccessControlHeaders($headers) {
112
		self::_headerArray('Access-Control-Allow-Headers', $headers);
113
	}
114
115
	/**
116
	 * Sets the response code
117
	 * @param int $value
118
	 */
119
	public static function setResponseCode($value) {
120
		\http_response_code($value);
121
	}
122
123
	/**
124
	 * Get the response code
125
	 * @return int
126
	 */
127
	public static function getResponseCode() {
128
		return \http_response_code();
129
	}
130
}
131