1: <?php
2:
3: namespace Racoon\Api;
4:
5:
6: use Racoon\Api\Exception\Exception;
7: use Racoon\Api\Exception\InvalidJsonException;
8: use Racoon\Router\DispatcherResult;
9: use Racoon\Router\Router;
10: use Racoon\Api\Schema\Schema;
11:
12: class Request
13: {
14:
15: 16: 17:
18: protected $startTime;
19:
20: 21: 22:
23: protected $endTime;
24:
25: 26: 27:
28: protected $request;
29:
30: 31: 32:
33: protected $jsonString;
34:
35: 36: 37:
38: protected $httpMethod;
39:
40: 41: 42:
43: protected $uri;
44:
45: 46: 47:
48: protected $schema;
49:
50: 51: 52:
53: protected $dispatcherResult;
54:
55: 56: 57:
58: protected $responseMessage;
59:
60: 61: 62:
63: protected $controllerResponse;
64:
65: 66: 67: 68:
69: protected $displayException;
70:
71: public function __construct()
72: {
73: $this->setStartTime(microtime(true));
74: }
75:
76:
77: 78: 79: 80: 81: 82:
83: public function getOptionalRequestData($name, $default = null)
84: {
85: $data = $this->getRequestData();
86: $result = $default;
87: if (isset($data->{$name}) && mb_strlen($data->{$name}, 'UTF-8') > 0) {
88: $result = $data->{$name};
89: }
90: return $result;
91: }
92:
93:
94: 95: 96: 97: 98: 99: 100:
101: public function getRequiredRequestData($name, $errorMessage = 'Missing required parameter: [parameter]')
102: {
103: $result = $this->getOptionalRequestData($name, null);
104: if ($result === null) {
105: $errorMessage = str_replace('[parameter]', $name, $errorMessage);
106: throw new \Racoon\Api\Exception\Exception($this, true, $errorMessage, 400);
107: }
108: return $result;
109: }
110:
111:
112: 113: 114: 115:
116: public function getRequestData($fullRequest = false)
117: {
118: if ($fullRequest) {
119: return $this->request;
120: } else {
121: return (is_object($this->request) && isset($this->request->request)) ? $this->request->request : null;
122: }
123: }
124:
125:
126: 127: 128:
129: public function getFullRequestData()
130: {
131: return $this->getRequestData(true);
132: }
133:
134:
135: 136: 137: 138:
139: public function setRequest($request)
140: {
141: $this->request = $request;
142: return $this;
143: }
144:
145:
146: 147: 148: 149: 150:
151: public function setRequestJson($json)
152: {
153: $this->setJsonString($json);
154:
155: if (! (is_string($json) && strlen($json) > 0)) {
156:
157: throw new InvalidJsonException($this, 'Invalid JSON string.');
158: }
159: $jsonObject = json_decode($json);
160:
161: if ($jsonObject !== null) {
162: $this->setRequest($jsonObject);
163: } else {
164:
165: throw new InvalidJsonException($this, 'JSON string could not be decoded.');
166: }
167:
168: return $this;
169: }
170:
171:
172: 173: 174:
175: public function getJsonString()
176: {
177: return $this->jsonString;
178: }
179:
180:
181: 182: 183: 184:
185: public function setJsonString($jsonString)
186: {
187: $this->jsonString = $jsonString;
188: return $this;
189: }
190:
191:
192: 193: 194:
195: public function getHttpMethod()
196: {
197: return $this->httpMethod;
198: }
199:
200:
201: 202: 203: 204:
205: public function setHttpMethod($httpMethod)
206: {
207: $this->httpMethod = $httpMethod;
208: return $this;
209: }
210:
211:
212: 213: 214:
215: public function getUri()
216: {
217: return $this->uri;
218: }
219:
220:
221: 222: 223: 224:
225: public function setUri($uri)
226: {
227: $this->uri = $uri;
228: return $this;
229: }
230:
231:
232: 233: 234: 235: 236: 237: 238:
239: public function process(Router $router, $requiresSchema = false)
240: {
241: $controllerResponse = null;
242:
243: try {
244:
245: $dispatcherResult = $router
246: ->processRoutes($this->getHttpMethod(), $this->getUri())
247: ->getDispatcherResult();
248:
249: $this->setDispatcherResult($dispatcherResult);
250:
251: if ($dispatcherResult->getClassObject() instanceof Controller) {
252: $dispatcherResult->getClassObject()->setRequest($this);
253: }
254:
255: if (method_exists($dispatcherResult->getClassObject(), 'setupRequest') && is_callable([$dispatcherResult->getClassObject(), 'setupRequest'])) {
256: call_user_func([$dispatcherResult->getClassObject(), 'setupRequest']);
257: }
258:
259: $controllerResponse = call_user_func_array([
260: $dispatcherResult->getClassObject(),
261: $dispatcherResult->getMethod(),
262: ], $dispatcherResult->getVars());
263:
264: if ($requiresSchema && ! is_object($this->getSchema())) {
265: throw new Exception($this, false, "Requires Schema is set to true but no Schema is present.");
266: }
267:
268: } catch (Exception $e) {
269:
270: if (! is_object($e->getRequest())) {
271: $e->setRequest($this);
272: }
273:
274: throw $e;
275: }
276:
277: $this->setControllerResponse($controllerResponse);
278:
279: return $this;
280: }
281:
282:
283: 284: 285:
286: public function getStartTime()
287: {
288: return $this->startTime;
289: }
290:
291:
292: 293: 294:
295: public function setStartTime($startTime)
296: {
297: $this->startTime = $startTime;
298: }
299:
300:
301: 302: 303:
304: public function getEndTime()
305: {
306: return $this->endTime;
307: }
308:
309:
310: 311: 312:
313: public function setEndTime($endTime)
314: {
315: $this->endTime = $endTime;
316: }
317:
318:
319: 320: 321:
322: public function getElapsedTime($convertToMs = false)
323: {
324: if ($this->startTime !== null && $this->endTime !== null) {
325: $time = ($this->endTime - $this->startTime);
326: if ($convertToMs) {
327: $time = ($time * 1000);
328: }
329: return $time;
330: }
331:
332: return null;
333: }
334:
335:
336: 337: 338:
339: public function getSchema()
340: {
341: return $this->schema;
342: }
343:
344:
345: 346: 347:
348: public function setSchema($schema)
349: {
350: $this->schema = $schema;
351: }
352:
353:
354: 355: 356:
357: public function getDispatcherResult()
358: {
359: return $this->dispatcherResult;
360: }
361:
362:
363: 364: 365:
366: public function setDispatcherResult($dispatcherResult)
367: {
368: $this->dispatcherResult = $dispatcherResult;
369: }
370:
371:
372: 373: 374:
375: public function getResponseMessage()
376: {
377: return $this->responseMessage;
378: }
379:
380:
381: 382: 383:
384: public function setResponseMessage($responseMessage)
385: {
386: $this->responseMessage = $responseMessage;
387: }
388:
389:
390: 391: 392:
393: public function getControllerResponse()
394: {
395: return $this->controllerResponse;
396: }
397:
398:
399: 400: 401:
402: protected function setControllerResponse($controllerResponse)
403: {
404: $this->controllerResponse = $controllerResponse;
405: }
406:
407:
408: 409: 410:
411: public function getDisplayException()
412: {
413: return $this->displayException;
414: }
415:
416:
417: 418: 419:
420: public function setDisplayException($displayException)
421: {
422: $this->displayException = $displayException;
423: }
424:
425: }