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

AbstractFilter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A filterDataToArray() 0 12 3
A toArray() 0 5 1
A populate() 0 8 4
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