Completed
Push — master ( 7a7c4e...586336 )
by Nate
11s
created

JsonParser::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 5
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright  Copyright (c) Flipbox Digital Limited
4
 * @license    https://flipboxfactory.com/software/transformer/license
5
 * @link       https://www.flipboxfactory.com/software/transformer/
6
 */
7
8
namespace flipbox\craft\rest;
9
10
/**
11
 * Class JsonParser
12
 * @package flipbox\craft\rest
13
 */
14
class JsonParser extends \yii\web\JsonParser
15
{
16
17
    /**
18
     * @inheritdoc
19
     */
20
    public function parse($rawBody, $contentType)
21
    {
22
        $bodyParams = parent::parse($rawBody, $contentType);
23
        $bodyParams = $this->filterNullValuesFromArray($bodyParams);
24
        return $bodyParams;
25
    }
26
27
    /**
28
     * Filters null values from an array.
29
     * @param array $arr
30
     * @return array
31
     */
32
    public function filterNullValuesFromArray(array $arr): array
33
    {
34
        foreach ($arr as $key => $value) {
35
            if ($value === null) {
36
                $arr[$key] = '';
37
            }
38
            if (is_array($value)) {
39
                $arr[$key] = $this->filterNullValuesFromArray($value);
40
            }
41
        }
42
43
        return $arr;
44
    }
45
}
46