Completed
Push — master ( 06e52a...98c39c )
by Pavel
13:39 queued 10:37
created

JmsContextFactory::deserialization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace ScayTrase\Api\Cruds\Adaptors\JmsSerializer;
4
5
use JMS\Serializer\Context;
6
use JMS\Serializer\DeserializationContext;
7
use JMS\Serializer\SerializationContext;
8
9
final class JmsContextFactory
10
{
11
    /**
12
     * @param null|array $context
13
     *
14
     * @return SerializationContext
15
     */
16
    public static function serialization(array $context = null)
17
    {
18
        $jmsContext = SerializationContext::create();
19
20
        self::configureContext($jmsContext, $context);
21
22
        return $jmsContext;
23
    }
24
25
    public static function deserialization(array $context = null)
26
    {
27
        $jmsContext = DeserializationContext::create();
28
29
        self::configureContext($jmsContext, $context);
30
31
        return $jmsContext;
32
    }
33
34
    private static function configureContext(Context $jmsContext, array $context = null)
35
    {
36
        $jmsContext->setSerializeNull(true);
37
38
        if (null === $context) {
39
            return;
40
        }
41
42
        if (array_key_exists('groups', $context)) {
43
            $jmsContext->setGroups($context['groups']);
44
        }
45
46
        if (array_key_exists('object_to_populate', $context)) {
47
            $jmsContext->setAttribute('target', $context['object_to_populate']);
48
        }
49
    }
50
}
51