|
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' => 'json', |
|
27
|
|
|
'type' => $format, |
|
28
|
|
|
'method' => 'deserializeObject', |
|
29
|
|
|
]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return $methods; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function serializeObject(JsonSerializationVisitor $visitor, $object, array $type, Context $context) |
|
|
|
|
|
|
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) |
|
|
|
|
|
|
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
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.