Completed
Push — master ( 920273...ad55a4 )
by Joachim
12:33
created

Payload::simplePayload()   D

Complexity

Conditions 9
Paths 4

Size

Total Lines 33
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 9

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 17
cts 17
cp 1
rs 4.909
c 0
b 0
f 0
cc 9
eloc 17
nc 4
nop 1
crap 9
1
<?php
2
namespace Loevgaard\AltaPay\Payload;
3
4
abstract class Payload implements PayloadInterface
5
{
6
    /**
7
     * @inheritdoc
8
     */
9
    public function getPayload(): array
10
    {
11 3
        return [];
12
    }
13 3
14
    /**
15
     * If the input array contains objects of PayloadInterface it will convert these to simple arrays
16
     * Also it will remove values that are null, empty string or empty arrays
17
     *
18
     * @param array $payload
19
     * @return array
20
     */
21
    public static function simplePayload(array $payload) : array
22 6
    {
23
        $payload = array_filter($payload, function ($val) {
24 6
            if ($val instanceof PayloadInterface) {
25 6
                $val = $val->getPayload();
26 6
            }
27 4
28 4
            // this will effectively remove empty arrays
29
            if (is_array($val) && empty($val)) {
30 6
                return false;
31
            }
32
33 6
            if(is_null($val)) {
34
                return false;
35 6
            }
36 6
37
            if($val === '') {
38
                return false;
39 3
            }
40
41
            return true;
42 9
        });
43
44 9
        foreach ($payload as $key => $val) {
45 9
            if (is_array($val)) {
46
                $payload[$key] = static::simplePayload($val);
47
            } elseif ($val instanceof PayloadInterface) {
48 3
                $payload[$key] = static::simplePayload($val->getPayload());
49
            }
50
        }
51 6
52
        return $payload;
53 6
    }
54
}
55