Completed
Push — master ( 122153...1ebbbc )
by Nazar
04:18
created

_SERVER::valid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @package   CleverStyle CMS
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2015-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs;
9
use
10
	ArrayAccess,
11
	Iterator;
12
13
/**
14
 * Generic wrapper for `$_SERVER` to make its usage easier and more secure
15
 *
16
 * @deprecated Use `cs\Request` instead, it provides very similar, but more powerful interface
17
 * @todo       Remove in 4.x
18
 *
19
 * @property string $language       Language accepted by client, `''` by default
20
 * @property string $version        Version accepted by client, will match `/^[0-9\.]+$/`, useful for API, `1` by default
21
 * @property string $content_type   Content type, `''` by default
22
 * @property bool   $dnt            Do not track
23
 * @property string $host           The best guessed host
24
 * @property string $ip             The best guessed IP of client (based on all known headers), `$this->remote_addr` by default
25
 * @property string $protocol       Protocol `http` or `https`
26
 * @property string $query_string   Query string
27
 * @property string $referer        HTTP referer, `''` by default
28
 * @property string $remote_addr    Where request came from, not necessary real IP of client
29
 * @property string $request_uri    Request uri
30
 * @property string $request_method Request method
31
 * @property bool   $secure         Is requested with HTTPS
32
 * @property string $user_agent     User agent
33
 */
34
class _SERVER implements ArrayAccess, Iterator {
35
	protected $_SERVER;
36
37
	function __construct (array $SERVER) {
38
		$this->_SERVER = $SERVER;
39
	}
40
	function __get ($key) {
41
		$Request = Request::instance();
42
		switch ($key) {
43
			case 'language':
44
			case 'version':
45
			case 'content_type':
46
			case 'dnt':
47
			case 'secure':
48
			case 'host':
49
			case 'ip':
50
			case 'query_string':
51
			case 'referer':
52
			case 'remote_addr':
53
			case 'user_agent':
54
				return $Request->$key;
55
			case 'request_method':
56
				return $Request->method;
57
			case 'request_uri':
58
				return $this->_SERVER['REQUEST_URI'];
59
			case 'protocol':
60
				return $Request->schema;
61
			default:
62
				return false;
63
		}
64
	}
65
	/**
66
	 * Whether key exists (from original `$_SERVER` superglobal)
67
	 *
68
	 * @param string $index
69
	 *
70
	 * @return bool
71
	 */
72
	function offsetExists ($index) {
73
		return isset($this->_SERVER[$index]);
74
	}
75
	/**
76
	 * Get key (from original `$_SERVER` superglobal)
77
	 *
78
	 * @param string $index
79
	 *
80
	 * @return mixed
81
	 */
82
	function offsetGet ($index) {
83
		return $this->_SERVER[$index];
84
	}
85
	/**
86
	 * Set key (from original `$_SERVER` superglobal)
87
	 *
88
	 * @param string $index
89
	 * @param mixed  $value
90
	 */
91
	function offsetSet ($index, $value) {
92
		$this->_SERVER[$index] = $value;
93
	}
94
	/**
95
	 * Unset key (from original `$_SERVER` superglobal)
96
	 *
97
	 * @param string $index
98
	 */
99
	public function offsetUnset ($index) {
100
		unset($this->_SERVER[$index]);
101
	}
102
	/**
103
	 * Get current (from original `$_SERVER` superglobal)
104
	 *
105
	 * @return mixed Can return any type.
106
	 */
107
	public function current () {
108
		return current($this->_SERVER);
109
	}
110
	/**
111
	 * Move forward to next element (from original `$_SERVER` superglobal)
112
	 */
113
	public function next () {
114
		next($this->_SERVER);
115
	}
116
	/**
117
	 * Return the key of the current element (from original `$_SERVER` superglobal)
118
	 *
119
	 * @return mixed scalar on success, or null on failure.
120
	 */
121
	public function key () {
122
		return key($this->_SERVER);
123
	}
124
	/**
125
	 * Checks if current position is valid (from original `$_SERVER` superglobal)
126
	 *
127
	 * @return boolean The return value will be casted to boolean and then evaluated.
128
	 * Returns true on success or false on failure.
129
	 */
130
	public function valid () {
131
		return $this->key() !== null;
132
	}
133
	/**
134
	 * Rewind the Iterator to the first element (from original `$_SERVER` superglobal)
135
	 */
136
	public function rewind () {
137
		reset($this->_SERVER);
138
	}
139
}
140