1: <?php
2:
3: namespace Deimos\Request;
4:
5: trait RequestExtension
6: {
7:
8: use Adapter\Request;
9: use Adapter\Server;
10: use Adapter\Router;
11: use Adapter\Query;
12: use Adapter\Other;
13: use Adapter\Data;
14:
15: 16: 17: 18: 19: 20: 21:
22: public function url($withParams = false)
23: {
24: $url = $this->isHttps() ? 'https://' : 'http://';
25: $url .= $this->server('http_host');
26:
27: return $url . $this->urlPath($withParams);
28: }
29:
30: 31: 32: 33: 34: 35: 36:
37: public function urlPath($withParams = false)
38: {
39: $path = $this->server('request_uri');
40:
41: if (!$withParams)
42: {
43: $position = mb_strpos($path, '?');
44: if ($position !== false)
45: {
46: $path = mb_substr($path, 0, $position);
47: }
48: }
49:
50: return $path;
51: }
52:
53: 54: 55:
56: public function isHttps()
57: {
58: $https = $this->server('https');
59:
60: return $this->filterVariable($https, FILTER_VALIDATE_BOOLEAN, false);
61: }
62:
63: 64: 65: 66: 67:
68: public function queryString($url = null)
69: {
70: $query = '';
71:
72: if (!$url)
73: {
74: $url = $this->server('request_uri');
75: }
76:
77: $position = mb_strpos($url, '?');
78:
79: if ($position !== false)
80: {
81: $query = mb_substr($url, $position + 1);
82: }
83:
84: return $query;
85: }
86:
87: 88: 89: 90:
91: public function json(array $data = array(), $options = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)
92: {
93:
94: if (!headers_sent())
95: {
96: header('Cache-Control: no-cache, must-revalidate');
97: header('Expires: Tue, 19 May 1981 18:00:00 GMT');
98: header('Content-type: application/json; charset=utf-8');
99: }
100:
101: if (is_array($options))
102: {
103: $this->helper->json()->reset();
104:
105: foreach ($options as $option)
106: {
107: $this->helper->json()->addOption($option);
108: }
109: }
110: else
111: {
112: $this->helper->json()->setOption($options);
113: }
114:
115: return $this->helper->json()->encode($data);
116:
117: }
118:
119: 120: 121: 122: 123:
124: public function isAjax()
125: {
126: $httpXRequestedWith = $this->server('http_x_requested_with');
127: $httpXRequestedWith = $this->normalizeLow($httpXRequestedWith);
128:
129: return $httpXRequestedWith === 'xmlhttprequest';
130: }
131:
132: 133: 134:
135: public function isDelete()
136: {
137: return $this->method() === 'DELETE';
138: }
139:
140: 141: 142:
143: public function method()
144: {
145: return $this->server('request_method');
146: }
147:
148: 149: 150:
151: public function isHead()
152: {
153: return $this->method() === 'HEAD';
154: }
155:
156: 157: 158:
159: public function isPut()
160: {
161: return $this->method() === 'PUT';
162: }
163:
164: 165: 166:
167: public function isPatch()
168: {
169: return $this->method() === 'PATCH';
170: }
171:
172: 173: 174:
175: public function isPost()
176: {
177: return $this->method() === 'POST';
178: }
179:
180: 181: 182:
183: public function isGet()
184: {
185: return $this->method() === 'GET';
186: }
187:
188: }