1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace KingsonDe\Marshal; |
6
|
|
|
|
7
|
|
|
use KingsonDe\Marshal\Data\DataStructure; |
8
|
|
|
use KingsonDe\Marshal\Data\FlexibleData; |
9
|
|
|
use KingsonDe\Marshal\Exception\JsonDeserializeException; |
10
|
|
|
use KingsonDe\Marshal\Exception\JsonSerializeException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @method static string serializeItem(AbstractMapper $mapper, ...$data) |
14
|
|
|
* @method static string serializeItemCallable(callable $mappingFunction, ...$data) |
15
|
|
|
* @method static string serializeCollection(AbstractMapper $mapper, ...$data) |
16
|
|
|
* @method static string serializeCollectionCallable(callable $mappingFunction, ...$data) |
17
|
|
|
*/ |
18
|
|
|
class MarshalJson extends Marshal { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
protected static $encodingOptions = 0; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param int $encodingOptions |
27
|
1 |
|
* @link http://php.net/manual/en/function.json-encode.php |
28
|
1 |
|
*/ |
29
|
1 |
|
public static function setEncodingOptions(int $encodingOptions) { |
30
|
|
|
static::$encodingOptions = $encodingOptions; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param DataStructure $dataStructure |
35
|
|
|
* @return string |
36
|
4 |
|
* @throws \KingsonDe\Marshal\Exception\JsonSerializeException |
37
|
4 |
|
*/ |
38
|
|
|
public static function serialize(DataStructure $dataStructure) { |
39
|
4 |
|
$data = static::buildDataStructure($dataStructure); |
40
|
1 |
|
|
41
|
|
|
if (null === $data) { |
42
|
|
|
throw new JsonSerializeException('No data structure.'); |
43
|
3 |
|
} |
44
|
3 |
|
|
45
|
3 |
|
$json = json_encode( |
46
|
|
|
$data, |
47
|
|
|
static::$encodingOptions |
48
|
3 |
|
); |
49
|
1 |
|
|
50
|
|
|
if (false === $json) { |
51
|
|
|
throw new JsonSerializeException(json_last_error_msg()); |
52
|
2 |
|
} |
53
|
|
|
|
54
|
|
|
return $json; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $json |
59
|
|
|
* @return array |
60
|
|
|
* @throws \KingsonDe\Marshal\Exception\JsonDeserializeException |
61
|
|
|
*/ |
62
|
|
|
public static function deserializeJsonToData(string $json): array { |
63
|
|
|
$data = json_decode($json, true); |
64
|
|
|
|
65
|
|
|
if (null === $data) { |
66
|
|
|
throw new JsonDeserializeException('JSON could not be deserialized.'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $data; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $json |
74
|
|
|
* @param AbstractObjectMapper $mapper |
75
|
|
|
* @param mixed[] $additionalData |
76
|
|
|
* @return mixed |
77
|
|
|
*/ |
78
|
|
|
public static function deserializeJson( |
79
|
|
|
string $json, |
80
|
|
|
AbstractObjectMapper $mapper, |
81
|
|
|
...$additionalData |
82
|
|
|
) { |
83
|
|
|
return $mapper->map( |
84
|
|
|
new FlexibleData(static::deserializeJsonToData($json)), |
85
|
|
|
...$additionalData |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $json |
91
|
|
|
* @param callable $mappingFunction |
92
|
|
|
* @param mixed[] $additionalData |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
public static function deserializeJsonCallable( |
96
|
|
|
string $json, |
97
|
|
|
callable $mappingFunction, |
98
|
|
|
...$additionalData |
99
|
|
|
) { |
100
|
|
|
return $mappingFunction( |
101
|
|
|
new FlexibleData(static::deserializeJsonToData($json)), |
102
|
|
|
...$additionalData |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|