|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
|
4
|
|
|
|
|
5
|
|
|
namespace KingsonDe\Marshal; |
|
6
|
|
|
|
|
7
|
|
|
use KingsonDe\Marshal\Data\Collection; |
|
8
|
|
|
use KingsonDe\Marshal\Data\CollectionCallable; |
|
9
|
|
|
use KingsonDe\Marshal\Data\DataStructure; |
|
10
|
|
|
use KingsonDe\Marshal\Data\FlexibleData; |
|
11
|
|
|
use KingsonDe\Marshal\Data\Item; |
|
12
|
|
|
use KingsonDe\Marshal\Data\ItemCallable; |
|
13
|
|
|
|
|
14
|
|
|
class Marshal { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param DataStructure $dataStructure |
|
18
|
|
|
* @return array|null |
|
19
|
|
|
*/ |
|
20
|
11 |
|
public static function serialize(DataStructure $dataStructure) { |
|
21
|
11 |
|
return static::buildDataStructure($dataStructure); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
7 |
|
public static function serializeItem(AbstractMapper $mapper, ...$data) { |
|
25
|
7 |
|
$item = new Item($mapper, ...$data); |
|
26
|
|
|
|
|
27
|
7 |
|
return static::serialize($item); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
1 |
|
public static function serializeItemCallable(callable $mappingFunction, ...$data) { |
|
31
|
1 |
|
$item = new ItemCallable($mappingFunction, ...$data); |
|
32
|
|
|
|
|
33
|
1 |
|
return static::serialize($item); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
3 |
|
public static function serializeCollection(AbstractMapper $mapper, ...$data) { |
|
37
|
3 |
|
$item = new Collection($mapper, ...$data); |
|
38
|
|
|
|
|
39
|
3 |
|
return static::serialize($item); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
2 |
|
public static function serializeCollectionCallable(callable $mappingFunction, ...$data) { |
|
43
|
2 |
|
$item = new CollectionCallable($mappingFunction, ...$data); |
|
44
|
|
|
|
|
45
|
2 |
|
return static::serialize($item); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
11 |
|
protected static function buildDataStructure(DataStructure $dataStructure) { |
|
49
|
11 |
|
$rawResponse = $dataStructure->build(); |
|
50
|
|
|
|
|
51
|
11 |
|
return static::processElements($rawResponse); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param array|null $rawResponse |
|
56
|
|
|
* @return array|null |
|
57
|
|
|
*/ |
|
58
|
11 |
|
protected static function processElements($rawResponse) { |
|
59
|
11 |
|
if (!\is_array($rawResponse)) { |
|
60
|
7 |
|
return null; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
11 |
|
$response = []; |
|
64
|
|
|
|
|
65
|
11 |
|
foreach ($rawResponse as $property => $value) { |
|
66
|
11 |
|
if ($value instanceof DataStructure) { |
|
67
|
8 |
|
$response[$property] = static::buildDataStructure($value); |
|
68
|
8 |
|
continue; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
11 |
|
if (\is_array($value)) { |
|
72
|
11 |
|
$response[$property] = static::processElements($value); |
|
73
|
11 |
|
continue; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
11 |
|
$response[$property] = $value; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
11 |
|
return $response; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param AbstractObjectMapper $mapper |
|
84
|
|
|
* @param FlexibleData $flexibleData |
|
85
|
|
|
* @param mixed[] $additionalData |
|
86
|
|
|
* @return mixed |
|
87
|
|
|
*/ |
|
88
|
2 |
|
public static function deserialize( |
|
89
|
|
|
AbstractObjectMapper $mapper, |
|
90
|
|
|
FlexibleData $flexibleData, |
|
91
|
|
|
...$additionalData |
|
92
|
|
|
) { |
|
93
|
2 |
|
return $mapper->map($flexibleData, ...$additionalData); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param callable $mappingFunction |
|
98
|
|
|
* @param FlexibleData $flexibleData |
|
99
|
|
|
* @param mixed[] $additionalData |
|
100
|
|
|
* @return mixed |
|
101
|
|
|
*/ |
|
102
|
1 |
|
public static function deserializeCallable( |
|
103
|
|
|
callable $mappingFunction, |
|
104
|
|
|
FlexibleData $flexibleData, |
|
105
|
|
|
...$additionalData |
|
106
|
|
|
) { |
|
107
|
1 |
|
return $mappingFunction($flexibleData, ...$additionalData); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|