Completed
Push — master ( 46c26b...c595fe )
by Siim
12:38
created

AbstractFilter::toArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 0
dl 0
loc 13
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
     * @throws \ReflectionException
18
     */
19
    public function populate(array $data): void
20
    {
21
        $newData = [];
22
        foreach($data as $key => $value) {
23
            if(isset($value->type) && isset($value->value)) {
24
                $filterItem = new BaseFilterItem();
25
                $filterItem->setType($value->type);
26
                $filterItem->setValue($value->value);
27
                $newData[$key] = $filterItem;
28
            }
29
        }
30
        parent::populate($newData);
31
    }
32
33
    /**
34
     * @return array
35
     */
36
    public function toArray(): array
37
    {
38
        $data = parent::toArray();
39
        foreach($data as $key => $filterItem) {
40
            if($filterItem instanceof FilterItemInterface) {
41
                $data[$key] = [
42
                    'type' => $filterItem->getType(),
43
                    'value' => $filterItem->getValue()
44
                ];
45
            }
46
        }
47
48
        return $data;
49
    }
50
}
51