FilterComponent   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 113
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 1

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getName() 0 4 1
A add() 0 4 1
A has() 0 10 3
A get() 0 10 3
A all() 0 4 1
A getAppliedFilters() 0 11 1
A getIterator() 0 4 1
A count() 0 4 1
A jsonSerialize() 0 4 1
1
<?php
2
3
namespace BenTools\OpenCubes\Component\Filter;
4
5
use BenTools\OpenCubes\Component\ComponentInterface;
6
use BenTools\OpenCubes\Component\Filter\Model\Filter;
7
use Countable;
8
use IteratorAggregate;
9
use JsonSerializable;
10
11
final class FilterComponent implements ComponentInterface, IteratorAggregate, Countable, JsonSerializable
12
{
13
14
    /**
15
     * @var Filter[]
16
     */
17
    private $filters = [];
18
19
    /**
20
     * FilterComponent constructor.
21
     * @param Filter[] $filters
22
     */
23
    public function __construct(array $filters = [])
24
    {
25
        foreach ($filters as $filter) {
26
            $this->add($filter);
27
        }
28
    }
29
30
    /**
31
     * @inheritDoc
32
     */
33
    public static function getName(): string
34
    {
35
        return 'filter';
36
    }
37
38
    /**
39
     * @param Filter $filter
40
     */
41
    public function add(Filter $filter): void
42
    {
43
        $this->filters[$filter->getField()] = $filter;
44
    }
45
46
    /**
47
     * @param string $field
48
     * @return bool
49
     */
50
    public function has(string $field): bool
51
    {
52
        foreach ($this->filters as $filter) {
53
            if ($field === $filter->getField()) {
54
                return true;
55
            }
56
        }
57
58
        return false;
59
    }
60
61
    /**
62
     * @param string $field
63
     * @return Filter
64
     * @throws \InvalidArgumentException
65
     */
66
    public function get(string $field): Filter
67
    {
68
        foreach ($this->filters as $filter) {
69
            if ($field === $filter->getField()) {
70
                return $filter;
71
            }
72
        }
73
74
        throw new \InvalidArgumentException(sprintf('Unknown filter %s', $field));
75
    }
76
77
    /**
78
     * @return Filter[]
79
     */
80
    public function all(): array
81
    {
82
        return $this->filters;
83
    }
84
85
    /**
86
     * @return Filter[]
87
     */
88
    public function getAppliedFilters(): array
89
    {
90
        return array_values(
91
            array_filter(
92
                $this->filters,
93
                function (Filter $filter) {
94
                    return $filter->isApplied();
95
                }
96
            )
97
        );
98
    }
99
100
    /**
101
     * @return Filter[]
102
     */
103
    public function getIterator(): iterable
104
    {
105
        return new \ArrayIterator($this->filters);
106
    }
107
108
    /**
109
     * @inheritDoc
110
     */
111
    public function count()
112
    {
113
        return count($this->filters);
114
    }
115
116
    /**
117
     * @inheritDoc
118
     */
119
    public function jsonSerialize(): array
120
    {
121
        return $this->all();
122
    }
123
}
124