Skip to content

Commit

Permalink
added objects version into "View" object, to be able override the def…
Browse files Browse the repository at this point in the history
…ault FOSRestBundle setting
  • Loading branch information
Oleg Zinchenko committed Mar 23, 2012
1 parent 21edb2b commit 2f13647
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
35 changes: 32 additions & 3 deletions View/View.php
Expand Up @@ -66,16 +66,21 @@ class View
*/
private $route;

/**
* @var string
*/
private $objectsVersion;

/**
* Convenience method to allow for a fluent interface.
*
* @param mixed $data
* @param integer $statusCode
* @param array $headers
*/
public static function create($data = null, $statusCode = null, array $headers = array())
public static function create($data = null, $statusCode = null, array $headers = array(), $objectsVersion = null)
{
return new static($data, $statusCode, $headers);
return new static($data, $statusCode, $headers, $objectsVersion);
}

/**
Expand All @@ -85,11 +90,12 @@ public static function create($data = null, $statusCode = null, array $headers =
* @param integer $statusCode
* @param array $headers
*/
public function __construct($data = null, $statusCode = null, array $headers = array())
public function __construct($data = null, $statusCode = null, array $headers = array(), $objectsVersion = null)
{
$this->data = $data;
$this->statusCode = $statusCode;
$this->headers = $headers;
$this->objectsVersion = $objectsVersion;
$this->templateVar = 'data';
}

Expand Down Expand Up @@ -146,6 +152,19 @@ public function setStatusCode($code)
return $this;
}

/**
* set the serializer objects version
*
* @param $objectsVersion
* @return View
*/
public function setObjectsVersion($objectsVersion)
{
$this->objectsVersion = $objectsVersion;

return $this;
}

/**
* Sets template to use for the encoding
*
Expand Down Expand Up @@ -318,4 +337,14 @@ public function getRoute()
{
return $this->route;
}

/**
* get the objects version
*
* @return string objects version
*/
public function getObjectsVersion()
{
return $this->objectsVersion;
}
}
10 changes: 6 additions & 4 deletions View/ViewHandler.php
Expand Up @@ -164,11 +164,13 @@ protected function getSerializer()
/**
* Get the serializer objects version
*
* @return string "Objects versioning" version
* @param View $view
*
* @return string|null "Objects versioning" version
*/
protected function getObjectsVersion()
protected function getObjectsVersion(View $view)
{
return $this->container->getParameter('fos_rest.objects_version');
return $view->getObjectsVersion() ?: $this->container->getParameter('fos_rest.objects_version');
}

/**
Expand Down Expand Up @@ -321,7 +323,7 @@ public function createResponse(View $view, Request $request, $format)
$content = $this->renderTemplate($view, $format);
} else {
$serializer = $this->getSerializer();
$serializer->setVersion($this->getObjectsVersion());
$serializer->setVersion($this->getObjectsVersion($view));
$content = $serializer->serialize($view->getData(), $format);
}

Expand Down

0 comments on commit 2f13647

Please sign in to comment.