Completed
Push — master ( 3723ad...8fd218 )
by WEBEWEB
01:56
created

QueryBuilderFilterSet::getFilter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the jquery-querybuilder-bundle package.
5
 *
6
 * (c) 2017 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\JQuery\QueryBuilderBundle\Model;
13
14
use WBW\Bundle\JQuery\QueryBuilderBundle\API\QueryBuilderDecoratorInterface;
15
use WBW\Bundle\JQuery\QueryBuilderBundle\API\QueryBuilderFilterInterface;
16
use WBW\Bundle\JQuery\QueryBuilderBundle\API\QueryBuilderFilterSetInterface;
17
use WBW\Bundle\JQuery\QueryBuilderBundle\Serializer\JsonSerializer;
18
19
/**
20
 * QueryBuilder filter set.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Bundle\JQuery\QueryBuilderBundle\Model
24
 */
25
class QueryBuilderFilterSet implements QueryBuilderFilterSetInterface {
26
27
    /**
28
     * Filters.
29
     *
30
     * @var QueryBuilderFilterInterface[]
31
     */
32
    private $filters;
33
34
    /**
35
     * Constructor.
36
     */
37
    public function __construct() {
38
        $this->setFilters([]);
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44
    public function addFilter(QueryBuilderFilterInterface $filter): QueryBuilderFilterSetInterface {
45
        $this->filters[$filter->getId()] = $filter;
46
        return $this;
47
    }
48
49
    /**
50
     * {@inheritDoc}
51
     */
52
    public function getDecorator(string $id): ?QueryBuilderDecoratorInterface {
53
54
        $filter = $this->getFilter($id);
55
        if (null === $filter) {
56
            return null;
57
        }
58
59
        return $filter->getDecorator();
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    public function getFilter(string $id): ?QueryBuilderFilterInterface {
66
67
        if (false === array_key_exists($id, $this->filters)) {
68
            return null;
69
        }
70
71
        return $this->filters[$id];
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77
    public function getFilters(): array {
78
        return $this->filters;
79
    }
80
81
    /**
82
     * Serialize this instance.
83
     *
84
     * @return array Returns an array representing this instance.
85
     */
86
    public function jsonSerialize(): array {
87
        return JsonSerializer::serializeQueryBuilderFilterSet($this);
88
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93
    public function removeFilter(QueryBuilderFilterInterface $filter): QueryBuilderFilterSetInterface {
94
        if (true === array_key_exists($filter->getId(), $this->filters)) {
95
            unset($this->filters[$filter->getId()]);
96
        }
97
        return $this;
98
    }
99
100
    /**
101
     * Set the filters.
102
     *
103
     * @param QueryBuilderFilterInterface[] $filters The filters.
104
     * @return QueryBuilderFilterSetInterface Returns this filter set.
105
     */
106
    protected function setFilters(array $filters): QueryBuilderFilterSetInterface {
107
        $this->filters = $filters;
108
        return $this;
109
    }
110
}
111