|
@@ -1,188 +0,0 @@
|
1
|
|
-<?php
|
2
|
|
-namespace Tqdev\PhpCrudApi;
|
3
|
|
-
|
4
|
|
-class Request
|
5
|
|
-{
|
6
|
|
- private $method;
|
7
|
|
- private $path;
|
8
|
|
- private $pathSegments;
|
9
|
|
- private $params;
|
10
|
|
- private $body;
|
11
|
|
- private $headers;
|
12
|
|
- private $highPerformance;
|
13
|
|
-
|
14
|
|
- public function __construct(string $method = null, string $path = null, string $query = null, array $headers = null, string $body = null, bool $highPerformance = true)
|
15
|
|
- {
|
16
|
|
- $this->parseMethod($method);
|
17
|
|
- $this->parsePath($path);
|
18
|
|
- $this->parseParams($query);
|
19
|
|
- $this->parseHeaders($headers);
|
20
|
|
- $this->parseBody($body);
|
21
|
|
- $this->highPerformance = $highPerformance;
|
22
|
|
- }
|
23
|
|
-
|
24
|
|
- private function parseMethod(string $method = null)
|
25
|
|
- {
|
26
|
|
- if (!$method) {
|
27
|
|
- if (isset($_SERVER['REQUEST_METHOD'])) {
|
28
|
|
- $method = $_SERVER['REQUEST_METHOD'];
|
29
|
|
- } else {
|
30
|
|
- $method = 'GET';
|
31
|
|
- }
|
32
|
|
- }
|
33
|
|
- $this->method = $method;
|
34
|
|
- }
|
35
|
|
-
|
36
|
|
- private function parsePath(string $path = null)
|
37
|
|
- {
|
38
|
|
- if (!$path) {
|
39
|
|
- if (isset($_SERVER['PATH_INFO'])) {
|
40
|
|
- $path = $_SERVER['PATH_INFO'];
|
41
|
|
- } else {
|
42
|
|
- $path = '/';
|
43
|
|
- }
|
44
|
|
- }
|
45
|
|
- $this->path = $path;
|
46
|
|
- $this->pathSegments = explode('/', $path);
|
47
|
|
- }
|
48
|
|
-
|
49
|
|
- private function parseParams(string $query = null)
|
50
|
|
- {
|
51
|
|
- if (!$query) {
|
52
|
|
- if (isset($_SERVER['QUERY_STRING'])) {
|
53
|
|
- $query = $_SERVER['QUERY_STRING'];
|
54
|
|
- } else {
|
55
|
|
- $query = '';
|
56
|
|
- }
|
57
|
|
- }
|
58
|
|
- $query = str_replace('][]=', ']=', str_replace('=', '[]=', $query));
|
59
|
|
- parse_str($query, $this->params);
|
60
|
|
- }
|
61
|
|
-
|
62
|
|
- private function parseHeaders(array $headers = null)
|
63
|
|
- {
|
64
|
|
- if (!$headers) {
|
65
|
|
- $headers = array();
|
66
|
|
- if (!$this->highPerformance) {
|
67
|
|
- foreach ($_SERVER as $name => $value) {
|
68
|
|
- if (substr($name, 0, 5) == 'HTTP_') {
|
69
|
|
- $key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))));
|
70
|
|
- $headers[$key] = $value;
|
71
|
|
- }
|
72
|
|
- }
|
73
|
|
- }
|
74
|
|
- }
|
75
|
|
- $this->headers = $headers;
|
76
|
|
- }
|
77
|
|
-
|
78
|
|
- private function decodeBody(string $body) /*: ?object*/
|
79
|
|
- {
|
80
|
|
- $first = substr($body, 0, 1);
|
81
|
|
- if ($first == '[' || $first == '{') {
|
82
|
|
- $object = json_decode($body);
|
83
|
|
- $causeCode = json_last_error();
|
84
|
|
- if ($causeCode !== JSON_ERROR_NONE) {
|
85
|
|
- $object = null;
|
86
|
|
- }
|
87
|
|
- } else {
|
88
|
|
- parse_str($body, $input);
|
89
|
|
- foreach ($input as $key => $value) {
|
90
|
|
- if (substr($key, -9) == '__is_null') {
|
91
|
|
- $input[substr($key, 0, -9)] = null;
|
92
|
|
- unset($input[$key]);
|
93
|
|
- }
|
94
|
|
- }
|
95
|
|
- $object = (object) $input;
|
96
|
|
- }
|
97
|
|
- return $object;
|
98
|
|
- }
|
99
|
|
-
|
100
|
|
- private function parseBody(string $body = null) /*: void*/
|
101
|
|
- {
|
102
|
|
- if (!$body) {
|
103
|
|
- $body = file_get_contents('php://input');
|
104
|
|
- }
|
105
|
|
- $this->body = $this->decodeBody($body);
|
106
|
|
- }
|
107
|
|
-
|
108
|
|
- public function getMethod(): string
|
109
|
|
- {
|
110
|
|
- return $this->method;
|
111
|
|
- }
|
112
|
|
-
|
113
|
|
- public function getPath(): string
|
114
|
|
- {
|
115
|
|
- return $this->path;
|
116
|
|
- }
|
117
|
|
-
|
118
|
|
- public function getPathSegment(int $part): string
|
119
|
|
- {
|
120
|
|
- if ($part < 0 || $part >= count($this->pathSegments)) {
|
121
|
|
- return '';
|
122
|
|
- }
|
123
|
|
- return $this->pathSegments[$part];
|
124
|
|
- }
|
125
|
|
-
|
126
|
|
- public function getParams(): array
|
127
|
|
- {
|
128
|
|
- return $this->params;
|
129
|
|
- }
|
130
|
|
-
|
131
|
|
- public function setParams(array $params) /*: void*/
|
132
|
|
- {
|
133
|
|
- $this->params = $params;
|
134
|
|
- }
|
135
|
|
-
|
136
|
|
- public function getBody() /*: ?array*/
|
137
|
|
- {
|
138
|
|
- return $this->body;
|
139
|
|
- }
|
140
|
|
-
|
141
|
|
- public function setBody($body) /*: void*/
|
142
|
|
- {
|
143
|
|
- $this->body = $body;
|
144
|
|
- }
|
145
|
|
-
|
146
|
|
- public function addHeader(string $key, string $value)
|
147
|
|
- {
|
148
|
|
- $this->headers[$key] = $value;
|
149
|
|
- }
|
150
|
|
-
|
151
|
|
- public function getHeader(string $key): string
|
152
|
|
- {
|
153
|
|
- if (isset($this->headers[$key])) {
|
154
|
|
- return $this->headers[$key];
|
155
|
|
- }
|
156
|
|
- if ($this->highPerformance) {
|
157
|
|
- $serverKey = 'HTTP_' . strtoupper(str_replace('-', '_', $key));
|
158
|
|
- if (isset($_SERVER[$serverKey])) {
|
159
|
|
- return $_SERVER[$serverKey];
|
160
|
|
- }
|
161
|
|
- }
|
162
|
|
- return '';
|
163
|
|
- }
|
164
|
|
-
|
165
|
|
- public function getHeaders(): array
|
166
|
|
- {
|
167
|
|
- return $this->headers;
|
168
|
|
- }
|
169
|
|
-
|
170
|
|
- public static function fromString(string $request): Request
|
171
|
|
- {
|
172
|
|
- $parts = explode("\n\n", trim($request), 2);
|
173
|
|
- $head = $parts[0];
|
174
|
|
- $body = isset($parts[1]) ? $parts[1] : null;
|
175
|
|
- $lines = explode("\n", $head);
|
176
|
|
- $line = explode(' ', trim(array_shift($lines)), 2);
|
177
|
|
- $method = $line[0];
|
178
|
|
- $url = isset($line[1]) ? $line[1] : '';
|
179
|
|
- $path = parse_url($url, PHP_URL_PATH);
|
180
|
|
- $query = parse_url($url, PHP_URL_QUERY);
|
181
|
|
- $headers = array();
|
182
|
|
- foreach ($lines as $line) {
|
183
|
|
- list($key, $value) = explode(':', $line, 2);
|
184
|
|
- $headers[$key] = trim($value);
|
185
|
|
- }
|
186
|
|
- return new Request($method, $path, $query, $headers, $body);
|
187
|
|
- }
|
188
|
|
-}
|