Completed
Pull Request — master (#556)
by Evgenij
02:53
created

ConfiguredContextFactory::setGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace JMS\SerializerBundle\ContextFactory;
4
5
use JMS\Serializer\Context;
6
use JMS\Serializer\ContextFactory\DeserializationContextFactoryInterface;
7
use JMS\Serializer\ContextFactory\SerializationContextFactoryInterface;
8
use JMS\Serializer\DeserializationContext;
9
use JMS\Serializer\SerializationContext;
10
11
/**
12
 * Class ConfiguredContextFactory
13
 */
14
class ConfiguredContextFactory implements SerializationContextFactoryInterface, DeserializationContextFactoryInterface
15
{
16
    /**
17
     * Application version
18
     *
19
     * @var null|string
20
     */
21
    private $version;
22
23
    /**
24
     * Flag if we should serialize null values
25
     *
26
     * @var bool
27
     */
28
    private $serializeNulls;
29
30
    /**
31
     * Key-value pairs with custom attributes
32
     *
33
     * @var array
34
     */
35
    private $attributes = array();
36
37
    /**
38
     * Serialization groups
39
     *
40
     * @var string[]
41
     */
42
    private $groups = array();
43
44
    /**
45
     * @param null|string $version
46
     */
47
    public function setVersion($version)
48
    {
49
        $this->version = $version;
50
    }
51
52
    /**
53
     * @param bool $serializeNulls
54
     */
55
    public function setSerializeNulls($serializeNulls)
56
    {
57
        $this->serializeNulls = (bool)$serializeNulls;
58
    }
59
60
    /**
61
     * @param array $attributes
62
     */
63
    public function setAttributes(array $attributes)
64
    {
65
        $this->attributes = $attributes;
66
    }
67
68
    /**
69
     * @param string[] $groups
70
     */
71
    public function setGroups(array $groups)
72
    {
73
        $this->groups = $groups;
74
    }
75
76
    /**
77
     * @inheritDoc
78
     */
79
    public function createDeserializationContext()
80
    {
81
        return $this->configureContext(new DeserializationContext());
82
    }
83
84
    /**
85
     * @inheritDoc
86
     */
87
    public function createSerializationContext()
88
    {
89
        return $this->configureContext(new SerializationContext());
90
    }
91
92
    /**
93
     * Configures context according to configuration
94
     *
95
     * @param Context $context The context
96
     *
97
     * @return Context Given object
98
     */
99
    private function configureContext(Context $context)
100
    {
101
        foreach ($this->attributes as $key => $value) {
102
            $context->setAttribute($key, $value);
103
        }
104
        if (!empty($this->groups)) {
105
            $context->setGroups($this->groups);
106
        }
107
        if ($this->serializeNulls !== null) {
108
            $context->setSerializeNull($this->serializeNulls);
109
        }
110
        if ($this->version !== null) {
111
            $context->setVersion($this->version);
112
        }
113
114
        return $context;
115
    }
116
}
117