Author: Drahomír Hanák
Inheritance: extends Nette\Object
Esempio n. 1
0
 /**
  * Parse request body if any
  * @return array|\Traversable
  *
  * @throws BadRequestException
  */
 protected function parseRequestBody()
 {
     $requestBody = array();
     $input = class_exists('Nette\\Framework') && Nette\Framework::VERSION_ID <= 20200 ? file_get_contents('php://input') : $this->httpRequest->getRawBody();
     if ($input) {
         try {
             $this->mapper = $this->mapperContext->getMapper($this->httpRequest->getHeader('Content-Type'));
             $requestBody = $this->mapper->parse($input);
         } catch (InvalidStateException $e) {
             throw BadRequestException::unsupportedMediaType('No mapper defined for Content-Type ' . $this->httpRequest->getHeader('Content-Type'), $e);
         } catch (MappingException $e) {
             throw new BadRequestException($e->getMessage(), 400, $e);
         }
     }
     return $requestBody;
 }
Esempio n. 2
0
 /**
  * Create new api response
  * @param IResource $resource
  * @param string|null $contentType
  * @return IResponse
  *
  * @throws InvalidStateException
  */
 public function create(IResource $resource, $contentType = NULL)
 {
     if ($contentType === NULL) {
         $contentType = $this->jsonp === FALSE || !$this->request->getQuery($this->jsonp) ? $this->getPreferredContentType($this->request->getHeader('Accept')) : IResource::JSONP;
     }
     if (!isset($this->responses[$contentType])) {
         throw new InvalidStateException('Unregistered API response for ' . $contentType);
     }
     if (!class_exists($this->responses[$contentType])) {
         throw new InvalidStateException('API response class does not exist.');
     }
     if (!$resource->getData()) {
         $this->response->setCode(204);
         // No content
     }
     $responseClass = $this->responses[$contentType];
     $response = new $responseClass($resource->getData(), $this->mapperContext->getMapper($contentType), $contentType);
     if ($response instanceof BaseResponse) {
         $response->setPrettyPrint($this->isPrettyPrint());
     }
     return $response;
 }
Esempio n. 3
0
 /**
  * @param MapperContext $mapper
  * @param IRequest $httpRequest
  */
 public function __construct(MapperContext $mapperContext, IRequest $httpRequest)
 {
     $this->mapper = $mapperContext->getMapper($httpRequest->getHeader('content-type'));
 }