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
|
10 |
|
public static function serialize(DataStructure $dataStructure) { |
21
|
10 |
|
return static::buildDataStructure($dataStructure); |
22
|
|
|
} |
23
|
|
|
|
24
|
6 |
|
public static function serializeItem(AbstractMapper $mapper, ...$data) { |
25
|
6 |
|
$item = new Item($mapper, ...$data); |
26
|
|
|
|
27
|
6 |
|
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
|
10 |
|
protected static function buildDataStructure(DataStructure $dataStructure) { |
49
|
10 |
|
$rawResponse = $dataStructure->build(); |
50
|
|
|
|
51
|
10 |
|
return static::processElements($rawResponse); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param array|null $rawResponse |
56
|
|
|
* @return array|null |
57
|
|
|
*/ |
58
|
10 |
|
protected static function processElements($rawResponse) { |
59
|
10 |
|
if (!\is_array($rawResponse)) { |
60
|
6 |
|
return null; |
61
|
|
|
} |
62
|
|
|
|
63
|
10 |
|
$response = []; |
64
|
|
|
|
65
|
10 |
|
foreach ($rawResponse as $property => $value) { |
66
|
10 |
|
if ($value instanceof DataStructure) { |
67
|
7 |
|
$response[$property] = static::buildDataStructure($value); |
68
|
7 |
|
continue; |
69
|
|
|
} |
70
|
|
|
|
71
|
10 |
|
if (\is_array($value)) { |
72
|
10 |
|
$response[$property] = static::processElements($value); |
73
|
10 |
|
continue; |
74
|
|
|
} |
75
|
|
|
|
76
|
10 |
|
$response[$property] = $value; |
77
|
|
|
} |
78
|
|
|
|
79
|
10 |
|
return $response; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param AbstractObjectMapper $mapper |
84
|
|
|
* @param FlexibleData $flexibleData |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
1 |
|
public static function deserialize( |
88
|
|
|
AbstractObjectMapper $mapper, |
89
|
|
|
FlexibleData $flexibleData |
90
|
|
|
) { |
91
|
1 |
|
return $mapper->map($flexibleData); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param callable $mappingFunction |
96
|
|
|
* @param FlexibleData $flexibleData |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
1 |
|
public static function deserializeCallable( |
100
|
|
|
callable $mappingFunction, |
101
|
|
|
FlexibleData $flexibleData |
102
|
|
|
) { |
103
|
1 |
|
return $mappingFunction($flexibleData); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|