LuaArrayCollectionHandler   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 44
c 0
b 0
f 0
ccs 0
cts 38
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getSubscribingMethods() 0 25 2
A serializeCollection() 0 10 1
A deserializeCollection() 0 5 1
1
<?php
2
3
namespace Vlaswinkel\Lua\JMS;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\Common\Collections\Collection;
7
use JMS\Serializer\Context;
8
use JMS\Serializer\GraphNavigator;
9
use JMS\Serializer\Handler\SubscribingHandlerInterface;
10
use JMS\Serializer\VisitorInterface;
11
12
/**
13
 * Class LuaArrayCollectionHandler
14
 *
15
 * @see     https://github.com/schmittjoh/serializer/blob/1.1.0/src/JMS/Serializer/Handler/ArrayCollectionHandler.php
16
 *
17
 * @author  Johannes M. Schmitt <[email protected]>
18
 * @author  Koen Vlaswinkel <[email protected]>
19
 * @package Vlaswinkel\JMS\Lua
20
 */
21
class LuaArrayCollectionHandler implements SubscribingHandlerInterface {
22
    public static function getSubscribingMethods() {
23
        $methods         = [];
24
        $collectionTypes = [
25
            'ArrayCollection',
26
            'Doctrine\Common\Collections\ArrayCollection',
27
            'Doctrine\ORM\PersistentCollection',
28
            'Doctrine\ODM\MongoDB\PersistentCollection',
29
            'Doctrine\ODM\PHPCR\PersistentCollection',
30
        ];
31
        foreach ($collectionTypes as $type) {
32
            $methods[] = [
33
                'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
34
                'type'      => $type,
35
                'format'    => 'lua',
36
                'method'    => 'serializeCollection',
37
            ];
38
            $methods[] = [
39
                'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
40
                'type'      => $type,
41
                'format'    => 'lua',
42
                'method'    => 'deserializeCollection',
43
            ];
44
        }
45
        return $methods;
46
    }
47
48
    public function serializeCollection(
49
        VisitorInterface $visitor,
50
        Collection $collection,
51
        array $type,
52
        Context $context
53
    ) {
54
        // We change the base type, and pass through possible parameters.
55
        $type['name'] = 'array';
56
        return $visitor->visitArray($collection->toArray(), $type, $context);
57
    }
58
59
    public function deserializeCollection(VisitorInterface $visitor, $data, array $type, Context $context) {
60
        // See above.
61
        $type['name'] = 'array';
62
        return new ArrayCollection($visitor->visitArray($data, $type, $context));
63
    }
64
}