Skip to content

Commit cd9d309

Browse files
authored
Merge pull request #1086 from mickadoo/feature/allow-symfony-serializer
Feature/allow symfony serializer
2 parents f57fd01 + 1e8cb8c commit cd9d309

File tree

4 files changed

+87
-23
lines changed

4 files changed

+87
-23
lines changed

Resources/doc/serializer.md

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,17 @@ it means types do not have to be mapped.
88
A) Install and declare the serializer
99
-------------------------------------
1010

11-
Follow the installation instructions for [JMSSerializerBundle](http://jmsyst.com/bundles/JMSSerializerBundle).
11+
Both the [Symfony Serializer](http://symfony.com/doc/current/components/serializer.html) and
12+
[JMSSerializerBundle](http://jmsyst.com/bundles/JMSSerializerBundle) are supported.
1213

13-
Enable the serializer configuration for the bundle:
14+
Enable the serializer in the configuration:
1415

1516
```yaml
1617
#app/config/config.yml
1718
fos_elastica:
1819
serializer: ~
1920
```
2021
21-
The default configuration that comes with FOSElasticaBundle supports both the JMS Serializer
22-
and the Symfony Serializer. If JMSSerializerBundle is installed, additional support for
23-
serialization groups, versions and null value serialization are added to the bundle. Example:
24-
25-
```yaml
26-
fos_elastica:
27-
serializer:
28-
groups: [elastica, Default]
29-
version: '1.1'
30-
serialize_null: true
31-
```
32-
3322
B) Set up each defined type to support serialization
3423
----------------------------------------------------
3524
@@ -43,5 +32,37 @@ fos_elastica:
4332
types:
4433
user:
4534
serializer:
46-
groups: [elastica, Default]
35+
groups: [elastica]
36+
```
37+
38+
And inside the User class:
39+
40+
```php
41+
use Symfony\Component\Serializer\Annotation\Groups;
42+
43+
class User {
44+
45+
/**
46+
* @Groups({"elastica"})
47+
*
48+
* @var string
49+
*/
50+
private $username;
51+
52+
}
4753
```
54+
55+
In addition the JMS Serializer allows you to specify options for version and whether to serialize null
56+
57+
```yaml
58+
fos_elastica:
59+
60+
indexes:
61+
app:
62+
types:
63+
user:
64+
serializer:
65+
groups: [elastica]
66+
version: '1.1'
67+
serialize_null: true
68+
```

Serializer/Callback.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
namespace FOS\ElasticaBundle\Serializer;
44

55
use JMS\Serializer\SerializationContext;
6-
use JMS\Serializer\SerializerInterface;
6+
use Symfony\Component\Serializer\SerializerInterface;
7+
use JMS\Serializer\SerializerInterface as JMSSerializer;
78

89
class Callback
910
{
@@ -30,8 +31,8 @@ public function setGroups(array $groups)
3031
{
3132
$this->groups = $groups;
3233

33-
if (!empty($this->groups) && !$this->serializer instanceof SerializerInterface) {
34-
throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer".');
34+
if (!empty($this->groups) && !$this->serializer instanceof SerializerInterface && !$this->serializer instanceof JMSSerializer) {
35+
throw new \RuntimeException('Setting serialization groups requires using "JMS\Serializer\Serializer" or "Symfony\Component\Serializer\Serializer"');
3536
}
3637
}
3738

@@ -42,7 +43,7 @@ public function setVersion($version)
4243
{
4344
$this->version = $version;
4445

45-
if ($this->version && !$this->serializer instanceof SerializerInterface) {
46+
if ($this->version && !$this->serializer instanceof JMSSerializer) {
4647
throw new \RuntimeException('Setting serialization version requires using "JMS\Serializer\Serializer".');
4748
}
4849
}
@@ -54,7 +55,7 @@ public function setSerializeNull($serializeNull)
5455
{
5556
$this->serializeNull = $serializeNull;
5657

57-
if (true === $this->serializeNull && !$this->serializer instanceof SerializerInterface) {
58+
if (true === $this->serializeNull && !$this->serializer instanceof JMSSerializer) {
5859
throw new \RuntimeException('Setting null value serialization option requires using "JMS\Serializer\Serializer".');
5960
}
6061
}
@@ -66,10 +67,14 @@ public function setSerializeNull($serializeNull)
6667
*/
6768
public function serialize($object)
6869
{
69-
$context = $this->serializer instanceof SerializerInterface ? SerializationContext::create()->enableMaxDepthChecks() : array();
70+
$context = $this->serializer instanceof JMSSerializer ? SerializationContext::create()->enableMaxDepthChecks() : array();
7071

7172
if (!empty($this->groups)) {
72-
$context->setGroups($this->groups);
73+
if ($context instanceof SerializationContext) {
74+
$context->setGroups($this->groups);
75+
} else {
76+
$context['groups'] = $this->groups;
77+
}
7378
}
7479

7580
if ($this->version) {

Tests/Serializer/CallbackTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use \FOS\ElasticaBundle\Serializer\Callback;
4+
5+
class CallbackTest extends PHPUnit_Framework_TestCase
6+
{
7+
public function testSerializerMustHaveSerializeMethod()
8+
{
9+
$callback = new Callback();
10+
$this->setExpectedException('RuntimeException', 'The serializer must have a "serialize" method.');
11+
$callback->setSerializer(new \stdClass());
12+
}
13+
14+
public function testSetGroupsWorksWithValidSerializer()
15+
{
16+
$callback = new Callback();
17+
$serializer = $this->getMock('Symfony\Component\Serializer\Serializer', array(), array(), '', false);
18+
$callback->setSerializer($serializer);
19+
20+
$callback->setGroups(array('foo'));
21+
}
22+
23+
public function testSetGroupsFailsWithInvalidSerializer()
24+
{
25+
$callback = new Callback();
26+
$serializer = $this->getMockBuilder('FakeSerializer')->setMethods(array('serialize'))->getMock();
27+
$callback->setSerializer($serializer);
28+
29+
$this->setExpectedException(
30+
'RuntimeException',
31+
'Setting serialization groups requires using "JMS\Serializer\Serializer" or '
32+
. '"Symfony\Component\Serializer\Serializer"'
33+
);
34+
35+
$callback->setGroups(array('foo'));
36+
}
37+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"knplabs/knp-paginator-bundle": "~2.4",
3434
"symfony/browser-kit" : "~2.3|~3.0",
3535
"symfony/expression-language" : "~2.4|~3.0",
36-
"symfony/twig-bundle": "~2.3|~3.0"
36+
"symfony/twig-bundle": "~2.3|~3.0",
37+
"symfony/serializer": "~2.7|~3.0"
3738
},
3839
"autoload": {
3940
"psr-4": { "FOS\\ElasticaBundle\\": "" }

0 commit comments

Comments
 (0)