made the bundle compatible with Elastica 0.20.x

This commit is contained in:
Lukas Kahwe Smith 2013-04-22 13:20:10 +02:00
commit aafb6e53fb
28 changed files with 161 additions and 125 deletions

View file

@ -1,7 +1,8 @@
<?php
namespace FOQ\ElasticaBundle\Serializer;
namespace FOS\ElasticaBundle\Serializer;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\Serializer;
class Callback
@ -10,6 +11,8 @@ class Callback
protected $groups;
protected $version;
public function setSerializer($serializer)
{
$this->serializer = $serializer;
@ -20,14 +23,31 @@ class Callback
$this->groups = $groups;
}
public function setVersion($version)
{
$this->version = $version;
}
public function serialize($object)
{
if ($this->serializer instanceof Serializer) {
$this->serializer->setGroups($this->groups);
} elseif ($this->groups) {
throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer"');
$context = $this->serializer instanceof Serializer ? new SerializationContext() : null;
if ($this->groups) {
if (!$context) {
throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer"');
}
$context->setGroups($this->groups);
}
return $this->serializer->serialize($object, 'json');
if ($this->version) {
if (!$context) {
throw new \RuntimeException('Setting serialization version requires using "JMS\Serializer\Serializer"');
}
$context->setVersion($this->version);
}
return $this->serializer->serialize($object, 'json', $context);
}
}