Passed
Push — master ( e0198f...45352d )
by Martin
02:51
created

Marshal::processElements()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 12
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 22
ccs 13
cts 13
cp 1
crap 5
rs 8.6737
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
        return static::buildDataStructure($dataStructure);
21
    }
22
23 3
    public static function serializeItem(AbstractMapper $mapper, ...$data) {
24 3
        $item = new Item($mapper, ...$data);
25
26 3
        return static::serialize($item);
27
    }
28
29 1
    public static function serializeItemCallable(callable $mappingFunction, ...$data) {
30 1
        $item = new ItemCallable($mappingFunction, ...$data);
31
32 1
        return static::serialize($item);
33
    }
34
35 3
    public static function serializeCollection(AbstractMapper $mapper, ...$data) {
36 3
        $item = new Collection($mapper, ...$data);
37
38 3
        return static::serialize($item);
39
    }
40
41 2
    public static function serializeCollectionCallable(callable $mappingFunction, ...$data) {
42 2
        $item = new CollectionCallable($mappingFunction, ...$data);
43
44 2
        return static::serialize($item);
45
    }
46
47 6
    protected static function buildDataStructure(DataStructure $dataStructure) {
48 6
        $rawResponse = $dataStructure->build();
49
50 6
        return static::processElements($rawResponse);
51
    }
52
53
    /**
54
     * @param array|null $rawResponse
55
     * @return array|null
56
     */
57 6
    protected static function processElements($rawResponse) {
58 6
        if (!\is_array($rawResponse)) {
59 3
            return null;
60
        }
61
62 6
        $response = [];
63
64 6
        foreach ($rawResponse as $property => $value) {
65 6
            if ($value instanceof DataStructure) {
66 3
                $response[$property] = static::buildDataStructure($value);
67 3
                continue;
68
            }
69
70 6
            if (\is_array($value)) {
71 6
                $response[$property] = static::processElements($value);
72 6
                continue;
73
            }
74
75 6
            $response[$property] = $value;
76
        }
77
78 6
        return $response;
79
    }
80
}
81