2013-01-25 16:48:29 +01:00
|
|
|
<?php
|
|
|
|
|
2013-04-22 13:20:10 +02:00
|
|
|
namespace FOS\ElasticaBundle\Serializer;
|
2013-01-25 16:48:29 +01:00
|
|
|
|
2013-04-22 13:20:10 +02:00
|
|
|
use JMS\Serializer\SerializationContext;
|
2013-04-22 17:00:26 +02:00
|
|
|
use JMS\Serializer\SerializerInterface;
|
2013-01-29 23:17:40 +01:00
|
|
|
|
2013-01-25 16:48:29 +01:00
|
|
|
class Callback
|
|
|
|
{
|
|
|
|
protected $serializer;
|
|
|
|
protected $groups;
|
2013-04-22 13:20:10 +02:00
|
|
|
protected $version;
|
|
|
|
|
2013-01-29 23:17:40 +01:00
|
|
|
public function setSerializer($serializer)
|
|
|
|
{
|
2013-01-25 16:48:29 +01:00
|
|
|
$this->serializer = $serializer;
|
2013-04-22 20:02:12 +02:00
|
|
|
if (!method_exists($this->serializer, 'serialize')) {
|
|
|
|
throw new \RuntimeException('The serializer must have a "serialize" method.');
|
|
|
|
}
|
2013-01-25 16:48:29 +01:00
|
|
|
}
|
|
|
|
|
2013-01-29 23:17:40 +01:00
|
|
|
public function setGroups(array $groups)
|
|
|
|
{
|
2013-01-25 16:48:29 +01:00
|
|
|
$this->groups = $groups;
|
2013-04-22 20:02:12 +02:00
|
|
|
|
|
|
|
if ($this->groups) {
|
|
|
|
if (!$this->serializer instanceof SerializerInterface) {
|
|
|
|
throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer".');
|
|
|
|
}
|
|
|
|
}
|
2013-01-25 16:48:29 +01:00
|
|
|
}
|
|
|
|
|
2013-04-22 13:20:10 +02:00
|
|
|
public function setVersion($version)
|
|
|
|
{
|
|
|
|
$this->version = $version;
|
2013-04-22 20:02:12 +02:00
|
|
|
|
|
|
|
if ($this->version) {
|
|
|
|
if (!$this->serializer instanceof SerializerInterface) {
|
|
|
|
throw new \RuntimeException('Setting serialization version requires using "JMS\Serializer\Serializer".');
|
|
|
|
}
|
|
|
|
}
|
2013-04-22 13:20:10 +02:00
|
|
|
}
|
|
|
|
|
2013-01-25 16:48:29 +01:00
|
|
|
public function serialize($object)
|
|
|
|
{
|
2014-06-29 22:14:32 +02:00
|
|
|
$context = $this->serializer instanceof SerializerInterface ? SerializationContext::create()->enableMaxDepthChecks() : array();
|
2013-04-22 13:20:10 +02:00
|
|
|
|
|
|
|
if ($this->groups) {
|
|
|
|
$context->setGroups($this->groups);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->version) {
|
|
|
|
$context->setVersion($this->version);
|
2013-01-29 23:17:40 +01:00
|
|
|
}
|
|
|
|
|
2013-04-22 13:20:10 +02:00
|
|
|
return $this->serializer->serialize($object, 'json', $context);
|
2013-01-25 16:48:29 +01:00
|
|
|
}
|
|
|
|
}
|