Completed
Pull Request — master (#1131)
by Karel
05:49
created

Callback::setGroups()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4.128

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 9.2
c 1
b 0
f 0
cc 4
eloc 4
nc 2
nop 1
crap 4.128
1
<?php
2
3
namespace FOS\ElasticaBundle\Serializer;
4
5
use JMS\Serializer\SerializationContext;
6
use Symfony\Component\Serializer\SerializerInterface;
7
use JMS\Serializer\SerializerInterface as JMSSerializer;
8
9
class Callback
10
{
11
    protected $serializer;
12
    protected $groups = array();
13
    protected $version;
14
    protected $serializeNull;
15
16
    /**
17
     * @param $serializer
18
     */
19 5
    public function setSerializer($serializer)
20
    {
21 5
        $this->serializer = $serializer;
22 5
        if (!method_exists($this->serializer, 'serialize')) {
23 1
            throw new \RuntimeException('The serializer must have a "serialize" method.');
24
        }
25 4
    }
26
27
    /**
28
     * @param array $groups
29
     */
30 4
    public function setGroups(array $groups)
31
    {
32 4
        $this->groups = $groups;
33
34 4
        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"');
36
        }
37 4
    }
38
39
    /**
40
     * @param $version
41
     */
42 3
    public function setVersion($version)
43
    {
44 3
        $this->version = $version;
45
46 3
        if ($this->version && !$this->serializer instanceof JMSSerializer) {
47
            throw new \RuntimeException('Setting serialization version requires using "JMS\Serializer\Serializer".');
48
        }
49 3
    }
50
51
    /**
52
     * @param $serializeNull
53
     */
54 3
    public function setSerializeNull($serializeNull)
55
    {
56 3
        $this->serializeNull = $serializeNull;
57
58 3
        if (true === $this->serializeNull && !$this->serializer instanceof JMSSerializer) {
59
            throw new \RuntimeException('Setting null value serialization option requires using "JMS\Serializer\Serializer".');
60
        }
61 3
    }
62
63
    /**
64
     * @param $object
65
     *
66
     * @return mixed
67
     */
68 2
    public function serialize($object)
69
    {
70 2
        $context = $this->serializer instanceof JMSSerializer ? SerializationContext::create()->enableMaxDepthChecks() : array();
71
72 2
        if (!empty($this->groups)) {
73 1
            if ($context instanceof SerializationContext) {
74 1
                $context->setGroups($this->groups);
75
            } else {
76
                $context['groups'] = $this->groups;
77
            }
78
        }
79
80 2
        if ($this->version) {
81 1
            $context->setVersion($this->version);
82
        }
83
84 2
        if (!is_array($context)) {
85 2
          $context->setSerializeNull($this->serializeNull);
86
        }
87
88 2
        return $this->serializer->serialize($object, 'json', $context);
89
    }
90
}
91