1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vlaswinkel\Lua\JMS; |
4
|
|
|
|
5
|
|
|
use JMS\Serializer\Context; |
6
|
|
|
use JMS\Serializer\GraphNavigator; |
7
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
8
|
|
|
use JMS\Serializer\VisitorInterface; |
9
|
|
|
use PhpCollection\Map; |
10
|
|
|
use PhpCollection\Sequence; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class LuaPhpCollectionHandler |
14
|
|
|
* |
15
|
|
|
* @see https://github.com/schmittjoh/serializer/blob/1.1.0/src/JMS/Serializer/Handler/PhpCollectionHandler.php |
16
|
|
|
* |
17
|
|
|
* @author Johannes M. Schmitt <[email protected]> |
18
|
|
|
* @author Koen Vlaswinkel <[email protected]> |
19
|
|
|
* @package Vlaswinkel\Lua\JMS |
20
|
|
|
*/ |
21
|
|
|
class LuaPhpCollectionHandler implements SubscribingHandlerInterface { |
22
|
|
|
public static function getSubscribingMethods() { |
23
|
|
|
$methods = []; |
24
|
|
|
$collectionTypes = [ |
25
|
|
|
'PhpCollection\Sequence' => 'Sequence', |
26
|
|
|
'PhpCollection\Map' => 'Map', |
27
|
|
|
]; |
28
|
|
|
foreach ($collectionTypes as $type => $shortName) { |
29
|
|
|
$methods[] = [ |
30
|
|
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
31
|
|
|
'type' => $type, |
32
|
|
|
'format' => 'lua', |
33
|
|
|
'method' => 'serialize' . $shortName, |
34
|
|
|
]; |
35
|
|
|
$methods[] = [ |
36
|
|
|
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION, |
37
|
|
|
'type' => $type, |
38
|
|
|
'format' => 'lua', |
39
|
|
|
'method' => 'deserialize' . $shortName, |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
return $methods; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function serializeMap(VisitorInterface $visitor, Map $map, array $type, Context $context) { |
46
|
|
|
$type['name'] = 'array'; |
47
|
|
|
return $visitor->visitArray(iterator_to_array($map), $type, $context); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function deserializeMap(VisitorInterface $visitor, $data, array $type, Context $context) { |
51
|
|
|
$type['name'] = 'array'; |
52
|
|
|
return new Map($visitor->visitArray($data, $type, $context)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function serializeSequence(VisitorInterface $visitor, Sequence $sequence, array $type, Context $context) { |
56
|
|
|
// We change the base type, and pass through possible parameters. |
57
|
|
|
$type['name'] = 'array'; |
58
|
|
|
return $visitor->visitArray($sequence->all(), $type, $context); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function deserializeSequence(VisitorInterface $visitor, $data, array $type, Context $context) { |
62
|
|
|
// See above. |
63
|
|
|
$type['name'] = 'array'; |
64
|
|
|
return new Sequence($visitor->visitArray($data, $type, $context)); |
65
|
|
|
} |
66
|
|
|
} |