Overview

Namespaces

  • Racoon
    • Api
      • Auth
      • Exception
      • Response
        • Format
        • Generate
      • Schema

Classes

  • Racoon\Api\App
  • Racoon\Api\Auth\ApiKeyAuthenticator
  • Racoon\Api\Auth\NoAuthenticator
  • Racoon\Api\Controller
  • Racoon\Api\Request
  • Racoon\Api\Response\Format\JsonFormatter
  • Racoon\Api\Response\Generate\DetailedResponse
  • Racoon\Api\Schema\Item
  • Racoon\Api\Schema\Schema
  • Racoon\Api\Schema\Translator

Interfaces

  • Racoon\Api\Auth\AuthInterface
  • Racoon\Api\Response\Format\FormatterInterface
  • Racoon\Api\Response\Generate\GeneratorInterface

Exceptions

  • Racoon\Api\Exception\AuthenticationException
  • Racoon\Api\Exception\Exception
  • Racoon\Api\Exception\InvalidArgumentException
  • Racoon\Api\Exception\InvalidJsonException
  • Racoon\Api\Exception\InvalidRouteException
  • Racoon\Api\Exception\NotFoundException
  • Racoon\Api\Exception\ResponseFormattingException
  • Overview
  • Namespace
  • Class
  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:      * @var float
 17:      */
 18:     protected $startTime;
 19: 
 20:     /**
 21:      * @var float
 22:      */
 23:     protected $endTime;
 24: 
 25:     /**
 26:      * @var null|object
 27:      */
 28:     protected $request;
 29: 
 30:     /**
 31:      * @var string|null
 32:      */
 33:     protected $jsonString;
 34: 
 35:     /**
 36:      * @var string
 37:      */
 38:     protected $httpMethod;
 39: 
 40:     /**
 41:      * @var string
 42:      */
 43:     protected $uri;
 44: 
 45:     /**
 46:      * @var Schema
 47:      */
 48:     protected $schema;
 49: 
 50:     /**
 51:      * @var DispatcherResult
 52:      */
 53:     protected $dispatcherResult;
 54: 
 55:     /**
 56:      * @var string|null
 57:      */
 58:     protected $responseMessage;
 59: 
 60:     /**
 61:      * @var mixed
 62:      */
 63:     protected $controllerResponse;
 64: 
 65:     /**
 66:      * An exception to be displayed to the user.
 67:      * @var \Exception
 68:      */
 69:     protected $displayException;
 70: 
 71:     public function __construct()
 72:     {
 73:         $this->setStartTime(microtime(true));
 74:     }
 75: 
 76: 
 77:     /**
 78:      * Returns a piece of optional data from the input data.
 79:      * @param string $name
 80:      * @param null|mixed $default
 81:      * @return mixed|null
 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:      * Returns a piece of required data from the input data.
 96:      * @param string $name
 97:      * @param string $errorMessage The error message to be shown to the user. [parameter] will be replaced with the missing parameter name.
 98:      * @return mixed|null
 99:      * @throws Exception
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:      * @param bool $fullRequest
114:      * @return null|object
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:      * @return null|object
128:      */
129:     public function getFullRequestData()
130:     {
131:         return $this->getRequestData(true);
132:     }
133: 
134: 
135:     /**
136:      * @param null|object $request
137:      * @return $this
138:      */
139:     public function setRequest($request)
140:     {
141:         $this->request = $request;
142:         return $this;
143:     }
144: 
145: 
146:     /**
147:      * @param string|null $json
148:      * @return $this
149:      * @throws InvalidJsonException
150:      */
151:     public function setRequestJson($json)
152:     {
153:         $this->setJsonString($json);
154: 
155:         if (! (is_string($json) && strlen($json) > 0)) {
156:             // $json can't actually be json.
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:             // Could't decode $json string.
165:             throw new InvalidJsonException($this, 'JSON string could not be decoded.');
166:         }
167: 
168:         return $this;
169:     }
170: 
171: 
172:     /**
173:      * @return null|string
174:      */
175:     public function getJsonString()
176:     {
177:         return $this->jsonString;
178:     }
179: 
180: 
181:     /**
182:      * @param null|string $jsonString
183:      * @return $this
184:      */
185:     public function setJsonString($jsonString)
186:     {
187:         $this->jsonString = $jsonString;
188:         return $this;
189:     }
190: 
191: 
192:     /**
193:      * @return string
194:      */
195:     public function getHttpMethod()
196:     {
197:         return $this->httpMethod;
198:     }
199: 
200: 
201:     /**
202:      * @param string $httpMethod
203:      * @return $this
204:      */
205:     public function setHttpMethod($httpMethod)
206:     {
207:         $this->httpMethod = $httpMethod;
208:         return $this;
209:     }
210: 
211: 
212:     /**
213:      * @return string
214:      */
215:     public function getUri()
216:     {
217:         return $this->uri;
218:     }
219: 
220: 
221:     /**
222:      * @param string $uri
223:      * @return $this
224:      */
225:     public function setUri($uri)
226:     {
227:         $this->uri = $uri;
228:         return $this;
229:     }
230: 
231: 
232:     /**
233:      * Process the current request.
234:      * @param Router $router
235:      * @param bool $requiresSchema
236:      * @return mixed
237:      * @throws Exception
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:             // Attach the request to the exception.
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:      * @return float
285:      */
286:     public function getStartTime()
287:     {
288:         return $this->startTime;
289:     }
290: 
291: 
292:     /**
293:      * @param float $startTime
294:      */
295:     public function setStartTime($startTime)
296:     {
297:         $this->startTime = $startTime;
298:     }
299: 
300: 
301:     /**
302:      * @return float
303:      */
304:     public function getEndTime()
305:     {
306:         return $this->endTime;
307:     }
308: 
309: 
310:     /**
311:      * @param float $endTime
312:      */
313:     public function setEndTime($endTime)
314:     {
315:         $this->endTime = $endTime;
316:     }
317: 
318: 
319:     /**
320:      * null|float
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:      * @return Schema
338:      */
339:     public function getSchema()
340:     {
341:         return $this->schema;
342:     }
343: 
344: 
345:     /**
346:      * @param Schema $schema
347:      */
348:     public function setSchema($schema)
349:     {
350:         $this->schema = $schema;
351:     }
352: 
353: 
354:     /**
355:      * @return DispatcherResult
356:      */
357:     public function getDispatcherResult()
358:     {
359:         return $this->dispatcherResult;
360:     }
361: 
362: 
363:     /**
364:      * @param DispatcherResult $dispatcherResult
365:      */
366:     public function setDispatcherResult($dispatcherResult)
367:     {
368:         $this->dispatcherResult = $dispatcherResult;
369:     }
370: 
371: 
372:     /**
373:      * @return null|string
374:      */
375:     public function getResponseMessage()
376:     {
377:         return $this->responseMessage;
378:     }
379: 
380: 
381:     /**
382:      * @param null|string $responseMessage
383:      */
384:     public function setResponseMessage($responseMessage)
385:     {
386:         $this->responseMessage = $responseMessage;
387:     }
388: 
389: 
390:     /**
391:      * @return mixed
392:      */
393:     public function getControllerResponse()
394:     {
395:         return $this->controllerResponse;
396:     }
397: 
398: 
399:     /**
400:      * @param mixed $controllerResponse
401:      */
402:     protected function setControllerResponse($controllerResponse)
403:     {
404:         $this->controllerResponse = $controllerResponse;
405:     }
406: 
407: 
408:     /**
409:      * @return \Exception
410:      */
411:     public function getDisplayException()
412:     {
413:         return $this->displayException;
414:     }
415: 
416: 
417:     /**
418:      * @param \Exception $displayException
419:      */
420:     public function setDisplayException($displayException)
421:     {
422:         $this->displayException = $displayException;
423:     }
424: 
425: }
API documentation generated by ApiGen