1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace JMS\Serializer\Handler; |
6
|
|
|
|
7
|
|
|
use ArrayIterator; |
8
|
|
|
use Generator; |
9
|
|
|
use Iterator; |
10
|
|
|
use JMS\Serializer\DeserializationContext; |
11
|
|
|
use JMS\Serializer\Functions; |
12
|
|
|
use JMS\Serializer\GraphNavigatorInterface; |
13
|
|
|
use JMS\Serializer\SerializationContext; |
14
|
|
|
use JMS\Serializer\Visitor\DeserializationVisitorInterface; |
15
|
|
|
use JMS\Serializer\Visitor\SerializationVisitorInterface; |
16
|
|
|
|
17
|
|
|
final class IteratorHandler implements SubscribingHandlerInterface |
18
|
|
|
{ |
19
|
|
|
private const SUPPORTED_FORMATS = ['json', 'xml']; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
public static function getSubscribingMethods() |
25
|
|
|
{ |
26
|
|
|
$methods = []; |
27
|
|
|
|
28
|
|
|
foreach (self::SUPPORTED_FORMATS as $format) { |
29
|
|
|
$methods[] = [ |
30
|
|
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
31
|
|
|
'type' => Iterator::class, |
32
|
|
|
'format' => $format, |
33
|
|
|
'method' => 'serializeIterable', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
$methods[] = [ |
37
|
|
|
'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION, |
38
|
|
|
'type' => Iterator::class, |
39
|
|
|
'format' => $format, |
40
|
|
|
'method' => 'deserializeIterator', |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
foreach (self::SUPPORTED_FORMATS as $format) { |
45
|
|
|
$methods[] = [ |
46
|
|
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
47
|
|
|
'type' => ArrayIterator::class, |
48
|
|
|
'format' => $format, |
49
|
|
|
'method' => 'serializeIterable', |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
$methods[] = [ |
53
|
|
|
'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION, |
54
|
|
|
'type' => ArrayIterator::class, |
55
|
|
|
'format' => $format, |
56
|
|
|
'method' => 'deserializeIterator', |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
foreach (self::SUPPORTED_FORMATS as $format) { |
61
|
|
|
$methods[] = [ |
62
|
|
|
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION, |
63
|
|
|
'type' => Generator::class, |
64
|
|
|
'format' => $format, |
65
|
|
|
'method' => 'serializeIterable', |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
$methods[] = [ |
69
|
|
|
'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION, |
70
|
|
|
'type' => Generator::class, |
71
|
|
|
'format' => $format, |
72
|
|
|
'method' => 'deserializeGenerator', |
73
|
|
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $methods; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return array|\ArrayObject|null |
81
|
|
|
*/ |
82
|
|
|
public function serializeIterable( |
83
|
|
|
SerializationVisitorInterface $visitor, |
84
|
|
|
iterable $iterable, |
85
|
|
|
array $type, |
86
|
|
|
SerializationContext $context |
87
|
|
|
): ?iterable { |
88
|
|
|
$type['name'] = 'array'; |
89
|
|
|
|
90
|
|
|
$context->stopVisiting($iterable); |
91
|
|
|
$result = $visitor->visitArray(Functions::iterableToArray($iterable), $type); |
92
|
|
|
$context->startVisiting($iterable); |
93
|
|
|
|
94
|
|
|
return $result; |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param mixed $data |
99
|
|
|
*/ |
100
|
|
|
public function deserializeIterator( |
101
|
|
|
DeserializationVisitorInterface $visitor, |
102
|
|
|
$data, |
103
|
|
|
array $type, |
104
|
|
|
DeserializationContext $context |
|
|
|
|
105
|
|
|
): \Iterator { |
106
|
|
|
$type['name'] = 'array'; |
107
|
|
|
|
108
|
|
|
return new ArrayIterator($visitor->visitArray($data, $type)); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param mixed $data |
113
|
|
|
*/ |
114
|
|
|
public function deserializeGenerator( |
115
|
|
|
DeserializationVisitorInterface $visitor, |
116
|
|
|
$data, |
117
|
|
|
array $type, |
118
|
|
|
DeserializationContext $context |
|
|
|
|
119
|
|
|
): Generator { |
120
|
|
|
return (static function () use (&$visitor, &$data, &$type): Generator { |
121
|
|
|
$type['name'] = 'array'; |
122
|
|
|
yield from $visitor->visitArray($data, $type); |
123
|
|
|
})(); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|