Drahak\Restful\Mapping\MapperContext::getMapper PHP Method

getMapper() public method

Get mapper
public getMapper ( string $contentType ) : Drahak\Restful\Mapping\IMapper
$contentType string in format mimeType[; charset=utf8]
return Drahak\Restful\Mapping\IMapper
    public function getMapper($contentType)
    {
        $contentType = explode(';', $contentType);
        $contentType = Strings::trim($contentType[0]);
        $contentType = $contentType ? $contentType : 'NULL';
        if (!isset($this->services[$contentType])) {
            throw new InvalidStateException('There is no mapper for Content-Type: ' . $contentType);
        }
        return $this->services[$contentType];
    }

Usage Example

Exemplo 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;
 }
All Usage Examples Of Drahak\Restful\Mapping\MapperContext::getMapper