Completed
Pull Request — master (#856)
by Chris
10:20
created

ObjectHandler::getSubscribingMethods()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 0
1
<?php
2
3
namespace JMS\Serializer\Handler;
4
5
use JMS\Serializer\Context;
6
use JMS\Serializer\Exception\RuntimeException;
7
use JMS\Serializer\GraphNavigator;
8
use JMS\Serializer\JsonDeserializationVisitor;
9
use JMS\Serializer\JsonSerializationVisitor;
10
11
class ObjectHandler implements SubscribingHandlerInterface
12
{
13
    public static function getSubscribingMethods()
14
    {
15
        $methods = [];
16
17
        foreach (array('json', 'xml', 'yml') as $format) {
18
            $methods[] = [
19
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
20
                'format' => $format,
21
                'type' => 'Object',
22
                'method' => 'serializeObject',
23
            ];
24
            $methods[] = [
25
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
26
                'format' => $format,
27
                'type' => 'Object',
28
                'method' => 'deserializeObject',
29
            ];
30
        }
31
32
        return $methods;
33
    }
34
35
    public function serializeObject(JsonSerializationVisitor $visitor, $object, array $type, Context $context)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
    {
37
        if ($object === null) {
38
            return null;
39
        }
40
41
        $class = get_class($object);
42
        $type['name'] = $class;
43
        $serialisation = $context->getNavigator()->accept($object, $type, $context);
44
        $serialisation['::class'] = $class;
45
46
        return $serialisation;
47
    }
48
49
    public function deserializeObject(JsonDeserializationVisitor $visitor, $data, array $type, Context $context)
0 ignored issues
show
Unused Code introduced by
The parameter $visitor is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
50
    {
51
        if ($data === null) {
52
            return null;
53
        }
54
55
        if (!isset($data['::class'])) {
56
            throw new RuntimeException('The ::class property was missing for a dynamic object type.');
57
        }
58
        $type['name'] = $data['::class'];
59
        unset($data['::class']);
60
        return $context->getNavigator()->accept($data, $type, $context);
61
    }
62
}