Completed
Pull Request — master (#3)
by
unknown
06:22
created

JsonParser   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A filterNullValuesFromArray() 0 12 4
A parse() 0 5 1
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
/**
12
 * Class JsonParser
13
 * @package flipbox\craft\rest
14
 */
15
class JsonParser extends \yii\web\JsonParser
16
{
17
18
    /**
19
     * @inheritdoc
20
     */
21
    public function parse($rawBody, $contentType)
22
    {
23
        $bodyParams = parent::parse($rawBody, $contentType);
24
        $bodyParams = $this->filterNullValuesFromArray($bodyParams);
25
        return $bodyParams;
26
    }
27
28
    /**
29
     * Filters null values from an array.
30
     * @param array $arr
31
     * @return array
32
     */
33
    public function filterNullValuesFromArray(array $arr): array
34
    {
35
        foreach ($arr as $key => $value) {
36
            if($value === null) {
37
                $arr[$key] = '';
38
            }
39
            if(is_array($value)) {
40
                $arr[$key] = $this->filterNullValuesFromArray($value);
41
            }
42
        }
43
44
        return $arr;
45
46
    }
47
}