JsonParser   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 10
c 5
b 0
f 0
dl 0
loc 29
ccs 0
cts 17
cp 0
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A filterNullValuesFromArray() 0 12 4
A parse() 0 5 1
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipboxfactory/craft-rest/blob/master/LICENSE
6
 * @link       https://github.com/flipboxfactory/craft-rest
7
 */
8
9
namespace flipbox\craft\rest;
10
11
/**
12
 * @author Flipbox Factory <[email protected]>
13
 * @since 1.0.0
14
 */
15
class JsonParser extends \yii\web\JsonParser
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
                unset($arr[$key]);
37
            }
38
            if (is_array($value)) {
39
                $arr[$key] = $this->filterNullValuesFromArray($value);
40
            }
41
        }
42
43
        return $arr;
44
    }
45
}
46