1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace JMS\Serializer\Handler; |
6
|
|
|
|
7
|
|
|
use JMS\Serializer\Exception\LogicException; |
8
|
|
|
use JMS\Serializer\Exception\RuntimeException; |
9
|
|
|
use JMS\Serializer\GraphNavigatorInterface; |
10
|
|
|
|
11
|
|
|
class HandlerRegistry implements HandlerRegistryInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var callable[] |
15
|
|
|
*/ |
16
|
329 |
|
protected $handlers; |
17
|
|
|
|
18
|
329 |
|
public static function getDefaultMethod(int $direction, string $type, string $format): string |
19
|
284 |
|
{ |
20
|
|
|
if (false !== $pos = strrpos($type, '\\')) { |
21
|
|
|
$type = substr($type, $pos + 1); |
22
|
|
|
} |
23
|
329 |
|
|
24
|
329 |
|
switch ($direction) { |
25
|
|
|
case GraphNavigatorInterface::DIRECTION_DESERIALIZATION: |
26
|
284 |
|
return 'deserialize' . $type . 'From' . $format; |
27
|
284 |
|
|
28
|
|
|
case GraphNavigatorInterface::DIRECTION_SERIALIZATION: |
29
|
|
|
return 'serialize' . $type . 'To' . $format; |
30
|
|
|
|
31
|
|
|
default: |
32
|
|
|
throw new LogicException(sprintf('The direction %s does not exist; see GraphNavigatorInterface::DIRECTION_??? constants.', json_encode($direction))); |
33
|
|
|
} |
34
|
338 |
|
} |
35
|
|
|
|
36
|
338 |
|
public function __construct(array $handlers = []) |
37
|
338 |
|
{ |
38
|
|
|
$this->handlers = $handlers; |
39
|
329 |
|
} |
40
|
|
|
|
41
|
329 |
|
public function registerSubscribingHandler(SubscribingHandlerInterface $handler): void |
42
|
329 |
|
{ |
43
|
|
|
foreach ($handler->getSubscribingMethods() as $methodData) { |
44
|
|
|
if (!isset($methodData['type'], $methodData['format'])) { |
45
|
|
|
throw new RuntimeException(sprintf('For each subscribing method a "type" and "format" attribute must be given, but only got "%s" for %s.', implode('" and "', array_keys($methodData)), \get_class($handler))); |
46
|
329 |
|
} |
47
|
329 |
|
|
48
|
329 |
|
$directions = [GraphNavigatorInterface::DIRECTION_DESERIALIZATION, GraphNavigatorInterface::DIRECTION_SERIALIZATION]; |
49
|
|
|
if (isset($methodData['direction'])) { |
50
|
|
|
$directions = [$methodData['direction']]; |
51
|
329 |
|
} |
52
|
329 |
|
|
53
|
329 |
|
foreach ($directions as $direction) { |
54
|
|
|
$method = $methodData['method'] ?? self::getDefaultMethod($direction, $methodData['type'], $methodData['format']); |
55
|
|
|
$this->registerHandler($direction, $methodData['type'], $methodData['format'], [$handler, $method]); |
56
|
329 |
|
} |
57
|
|
|
} |
58
|
334 |
|
} |
59
|
|
|
|
60
|
334 |
|
/** |
61
|
334 |
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
225 |
|
public function registerHandler(int $direction, string $typeName, string $format, $handler): void |
64
|
|
|
{ |
65
|
225 |
|
$this->handlers[$direction][$typeName][$format] = $handler; |
66
|
197 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
55 |
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function getHandler(int $direction, string $typeName, string $format) |
72
|
|
|
{ |
73
|
|
|
if (!isset($this->handlers[$direction][$typeName][$format])) { |
74
|
|
|
return null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->handlers[$direction][$typeName][$format]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getHandlers(): array |
81
|
|
|
{ |
82
|
|
|
return $this->handlers; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|