|
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
|
|
|
} |