1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* GpsLab component. |
5
|
|
|
* |
6
|
|
|
* @author Peter Gribanov <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
8
|
|
|
* @license http://opensource.org/licenses/MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace GpsLab\Component\Payload\Serializer; |
12
|
|
|
|
13
|
|
|
use GpsLab\Component\Payload\Payload; |
14
|
|
|
use Symfony\Component\Serializer\Exception\UnsupportedException; |
15
|
|
|
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface; |
16
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Not good, because we are losing the \DateTime and other ValueObjects. |
20
|
|
|
* There will be problems if the names of the serializable classes change. |
21
|
|
|
*/ |
22
|
|
|
class PayloadSerializer implements NormalizerInterface, DenormalizerInterface |
23
|
|
|
{ |
24
|
|
|
const PATTERN = '%s|%s'; |
25
|
|
|
const REGEXP = '/^ |
26
|
|
|
(?<class>[a-zA-Z_][a-zA-Z0-9_]*)| # class name |
27
|
|
|
(?<payload>.+) # payload |
28
|
|
|
$/x'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
private $json_options = 0; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param int $json_options |
37
|
|
|
*/ |
38
|
|
|
public function __construct($json_options = 0) |
39
|
|
|
{ |
40
|
|
|
$this->json_options = $json_options; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param mixed $data |
45
|
|
|
* @param string|null $format |
46
|
|
|
* |
47
|
|
|
* @return bool |
48
|
|
|
*/ |
49
|
|
|
public function supportsNormalization($data, $format = null) |
50
|
|
|
{ |
51
|
|
|
return $data instanceof Payload; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param Payload $object |
56
|
|
|
* @param string|null $format |
57
|
|
|
* @param array $context |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function normalize($object, $format = null, array $context = []) |
62
|
|
|
{ |
63
|
|
|
return sprintf(self::PATTERN, get_class($object), json_encode($object->payload(), $this->json_options)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $data |
68
|
|
|
* @param string $type |
69
|
|
|
* @param string|null $format |
70
|
|
|
* @param array $context |
71
|
|
|
* |
72
|
|
|
* @return Payload |
73
|
|
|
*/ |
74
|
|
|
public function denormalize($data, $type, $format = null, array $context = []) |
75
|
|
|
{ |
76
|
|
|
if (!preg_match(self::REGEXP, $data, $match)) { |
77
|
|
|
throw new UnsupportedException(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return new $match['class'](json_decode($match['payload'], true)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $data |
85
|
|
|
* @param string $type |
86
|
|
|
* @param string|null $format |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
91
|
|
|
{ |
92
|
|
|
return $type === Payload::class && preg_match(self::REGEXP, $data); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|