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\Item; |
11
|
|
|
use KingsonDe\Marshal\Data\ItemCallable; |
12
|
|
|
|
13
|
|
|
class Marshal { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param DataStructure $dataStructure |
17
|
|
|
* @return array|null |
18
|
|
|
*/ |
19
|
6 |
|
public static function serialize(DataStructure $dataStructure) { |
20
|
6 |
|
$rawResponse = $dataStructure->build(); |
21
|
|
|
|
22
|
6 |
|
return static::process($rawResponse); |
23
|
|
|
} |
24
|
|
|
|
25
|
3 |
|
public static function serializeItem(AbstractMapper $mapper, ...$data) { |
26
|
3 |
|
$item = new Item($mapper, ...$data); |
27
|
|
|
|
28
|
3 |
|
return static::serialize($item); |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
public static function serializeItemCallable(callable $mappingFunction, ...$data) { |
32
|
1 |
|
$item = new ItemCallable($mappingFunction, ...$data); |
33
|
|
|
|
34
|
1 |
|
return static::serialize($item); |
35
|
|
|
} |
36
|
|
|
|
37
|
3 |
|
public static function serializeCollection(AbstractMapper $mapper, ...$data) { |
38
|
3 |
|
$item = new Collection($mapper, ...$data); |
39
|
|
|
|
40
|
3 |
|
return static::serialize($item); |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
public static function serializeCollectionCallable(callable $mappingFunction, ...$data) { |
44
|
2 |
|
$item = new CollectionCallable($mappingFunction, ...$data); |
45
|
|
|
|
46
|
2 |
|
return static::serialize($item); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param array|null $rawResponse |
51
|
|
|
* @return array|null |
52
|
|
|
*/ |
53
|
6 |
|
private static function process($rawResponse) { |
54
|
6 |
|
if (!\is_array($rawResponse)) { |
55
|
3 |
|
return null; |
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
$response = []; |
59
|
|
|
|
60
|
6 |
|
foreach ($rawResponse as $property => $value) { |
61
|
6 |
|
if ($value instanceof DataStructure) { |
62
|
3 |
|
$response[$property] = static::serialize($value); |
|
|
|
|
63
|
3 |
|
continue; |
64
|
|
|
} |
65
|
|
|
|
66
|
6 |
|
if (\is_array($value)) { |
67
|
6 |
|
$response[$property] = static::process($value); |
68
|
6 |
|
continue; |
69
|
|
|
} |
70
|
|
|
|
71
|
6 |
|
$response[$property] = $value; |
72
|
|
|
} |
73
|
|
|
|
74
|
6 |
|
return $response; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.