Passed
Push — master ( 617266...2422af )
by Siim
10:33
created

AbstractFilter::filterDataToArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: siim
5
 * Date: 27.01.19
6
 * Time: 16:59
7
 */
8
9
namespace Sf4\Api\Dto\Filter;
10
11
use Sf4\Api\Dto\AbstractDto;
12
13
abstract class AbstractFilter extends AbstractDto implements FilterInterface
14
{
15
    /**
16
     * @param array $data
17
     */
18
    public function populate(array $data): void
19
    {
20
        foreach ($data as $key => $value) {
21
            if (isset($value->type) && isset($value->value)) {
22
                $filterItem = new BaseFilterItem();
23
                $filterItem->setType($value->type);
24
                $filterItem->setValue($value->value);
25
                $this->$key = $filterItem;
26
            }
27
        }
28
    }
29
30
    /**
31
     * @return array
32
     */
33
    public function toArray(): array
34
    {
35
        $data = parent::toArray();
36
37
        return $this->filterDataToArray($data);
38
    }
39
40
    /**
41
     * @param array $data
42
     * @return array
43
     */
44
    public function filterDataToArray(array $data): array
45
    {
46
        foreach ($data as $key => $filterItem) {
47
            if ($filterItem instanceof FilterItemInterface) {
48
                $data[$key] = [
49
                    'type' => $filterItem->getType(),
50
                    'value' => $filterItem->getValue()
51
                ];
52
            }
53
        }
54
55
        return $data;
56
    }
57
}
58