Example #1
0
 public function testSetDetectContentOptions()
 {
     $config = new Configuration();
     $config->setDetectContentOptions(array(Configuration::DETECT_CONTENT_HEADER => 'james'));
     $this->assertCount(1, $config->getDetectContentOptions());
     $config->setDetectContentOptions(array(Configuration::DETECT_CONTENT_HEADER => 'jill'));
     $this->assertCount(1, $config->getDetectContentOptions());
     $config->setDetectContentOptions([]);
     $this->assertCount(0, $config->getDetectContentOptions());
 }
Example #2
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();
 }
Example #3
0
 /**
  * Attempt to match a representation
  *
  * @param AbstractRepresentation|string $representation
  * @param array $representationObjects
  * @return AbstractRepresentation|null
  * @throws RepresentationException
  */
 protected function matchRepresentation($representation, array &$representationObjects)
 {
     if (!is_object($representation)) {
         $className = $this->getRepresentationClassName($representation);
         $representationObjects[] = $representation = new $className();
     }
     if (!$representation instanceof AbstractRepresentation) {
         throw RepresentationException::representationMustBeInstanceOfDrestRepresentation();
     }
     if (($representation = $this->determineRepresentationByHttpMethod($representation, $this->config->getDetectContentOptions())) !== null) {
         return $representation;
     }
     return null;
 }