/** * Detect an instance of a representation class using a matched route, or default representation classes * @param RouteMetaData $route * @param Mapping\RouteMetaData $route * @throws UnableToMatchRepresentationException * @throws RepresentationException - if unable to instantiate a representation object from config settings * @return AbstractRepresentation $representation */ protected function getDeterminedRepresentation(Mapping\RouteMetaData &$route = null) { $representations = !is_null($route) ? $route->getClassMetaData()->getRepresentations() : $this->config->getDefaultRepresentations(); if (empty($representations)) { throw RepresentationException::noRepresentationsSetForRoute($route->getName(), $route->getClassMetaData()->getClassName()); } $representationObjects = array(); foreach ($representations as $representation) { if (!is_object($representation)) { // Check if the class is namespaced, if so instantiate from root $className = strstr($representation, '\\') !== false ? '\\' . ltrim($representation, '\\') : $representation; $className = !class_exists($className) ? '\\DrestCommon\\Representation\\' . ltrim($className, '\\') : $className; if (!class_exists($className)) { throw RepresentationException::unknownRepresentationClass($representation); } $representationObjects[] = $representation = new $className(); } if (!$representation instanceof AbstractRepresentation) { throw RepresentationException::representationMustBeInstanceOfDrestRepresentation(); } switch ($this->request->getHttpMethod()) { // Match on content option case Request::METHOD_GET: // This representation matches the required media type requested by the client if ($representation->isExpectedContent($this->config->getDetectContentOptions(), $this->request)) { return $representation; } break; // Match on content-type // Match on content-type case Request::METHOD_POST: case Request::METHOD_PUT: case Request::METHOD_PATCH: if ($representation->getContentType() === $this->request->getHeaders('Content-Type')) { return $representation; } break; } } // For get requests with "415 for no media match" set on, throw an exception if ($this->request->getHttpMethod() == Request::METHOD_GET && $this->config->get415ForNoMediaMatchSetting()) { throw UnableToMatchRepresentationException::noMatch(); } // Return the first instantiated representation instance if (isset($representationObjects[0])) { return $representationObjects[0]; } // We have no representation instances from either annotations or config object throw UnableToMatchRepresentationException::noMatch(); }
/** * Detect an instance of a representation class using a matched route, or default representation classes * @param Request $request * @param RouteMetaData $route * @throws UnableToMatchRepresentationException * @throws RepresentationException - if unable to instantiate a representation object from config settings * @return 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(); }