示例#1
0
 /**
  * The representation class to be used
  * @param mixed $representation
  * @throws \DrestCommon\Representation\RepresentationException
  */
 public function setRepresentationClass($representation)
 {
     $this->errorResponseClass = null;
     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);
         }
         $this->representationClass = $className;
     } elseif ($representation instanceof AbstractRepresentation) {
         $this->representationClass = get_class($representation);
     } else {
         throw RepresentationException::needRepresentationToUse();
     }
 }
示例#2
0
 /**
  * Set a representation instance to be used on this resource
  * @param  object|string           $representation - can be either an instance of DrestCommon\Representation\AbstractRepresentation or a string (shorthand allowed - Json / Xml) referencing the class.
  * @throws RepresentationException
  */
 public function addRepresentation($representation)
 {
     if (is_object($representation)) {
         if (!$representation instanceof AbstractRepresentation) {
             throw RepresentationException::unknownRepresentationClass(get_class($representation));
         }
         $this->representations[] = $representation;
     } elseif (is_string($representation)) {
         $this->representations[] = $representation;
     } else {
         throw RepresentationException::representationMustBeObjectOrString();
     }
 }
示例#3
0
 /**
  * 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();
 }
示例#4
0
 /**
  * Get's the representation class name.
  * Removes any root NS chars
  * Falls back to a DrestCommon Representation lookup
  *
  * @param string $representation
  * @return string
  * @throws RepresentationException
  */
 protected function getRepresentationClassName($representation)
 {
     $className = strstr($representation, '\\') !== false ? '\\' . ltrim($representation, '\\') : $representation;
     $className = !class_exists($className) ? '\\DrestCommon\\Representation\\' . ltrim($className, '\\') : $className;
     if (!class_exists($className)) {
         throw RepresentationException::unknownRepresentationClass($representation);
     }
     return $className;
 }