Drest\Manager\Representation::getDeterminedRepresentation PHP Method

getDeterminedRepresentation() public method

Detect an instance of a representation class using a matched route, or default representation classes
public getDeterminedRepresentation ( DrestCommon\Request\Request $request, RouteMetaData &$route = null ) : DrestCommon\Representation\AbstractRepresentation
$request DrestCommon\Request\Request
$route Drest\Mapping\RouteMetaData
return DrestCommon\Representation\AbstractRepresentation $representation
    public function getDeterminedRepresentation(Request $request, RouteMetaData &$route = null)
    {
        $this->request = $request;
        if (($representations = $this->getRepresentationClasses($route)) === []) {
            $name = is_null($route) ? '"unknown name"' : $route->getName();
            $className = is_null($route) ? '"unknown class"' : $route->getClassMetaData()->getClassName();
            throw RepresentationException::noRepresentationsSetForRoute($name, $className);
        }
        if (($representation = $this->searchAndValidateRepresentations($representations)) !== null) {
            return $representation;
        }
        // We have no representation instances from either annotations or config object
        throw UnableToMatchRepresentationException::noMatch();
    }

Usage Example

Beispiel #1
0
 /**
  * Handle an error by passing the exception to the registered error handler
  * @param  \Exception $e
  * @throws \Exception
  */
 private function handleError(\Exception $e)
 {
     $eh = $this->getErrorHandler();
     try {
         $representation = $this->representationManager->getDeterminedRepresentation($this->getRequest());
         $errorDocument = $representation->getDefaultErrorResponse();
         $eh->error($e, 500, $errorDocument);
     } catch (UnableToMatchRepresentationException $e) {
         $errorDocument = new ErrorResponseText();
         $eh->error($e, 500, $errorDocument);
     }
     $this->getResponse()->setStatusCode($eh->getResponseCode());
     $this->getResponse()->setHttpHeader('Content-Type', $errorDocument::getContentType());
     $this->getResponse()->setBody($errorDocument->render());
 }