Completed
Push — master ( 3fc573...634850 )
by WEBEWEB
11:27
created

QueryBuilderFilterSet::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 0
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\API;
13
14
use WBW\Bundle\JQuery\QueryBuilderBundle\Normalizer\QueryBuilderNormalizer;
15
16
/**
17
 * QueryBuilder filter set.
18
 *
19
 * @author webeweb <https://github.com/webeweb/>
20
 * @package WBW\Bundle\JQuery\QueryBuilderBundle\API
21
 */
22
class QueryBuilderFilterSet implements QueryBuilderFilterSetInterface {
23
24
    /**
25
     * Decorators.
26
     *
27
     * @var QueryBuilderDecoratorInterface[]
28
     */
29
    private $decorators;
30
31
    /**
32
     * Filters.
33
     *
34
     * @var QueryBuilderFilterInterface[]
35
     */
36
    private $filters;
37
38
    /**
39
     * Constructor.
40
     */
41
    public function __construct() {
42
        $this->setDecorators([]);
43
        $this->setFilters([]);
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49
    public function addFilter(QueryBuilderFilterInterface $filter) {
50
        if (true === ($filter instanceof QueryBuilderDecoratorInterface)) {
51
            $this->decorators[$filter->getId()] = $filter;
52
        }
53
        $this->filters[$filter->getId()] = $filter;
54
        return $this;
55
    }
56
57
    /**
58
     * Get a decorator.
59
     *
60
     * @param string $id The id.
61
     * @return QueryBuilderDecoratorInterface Returns the decorator in case of success, null otherwise.
62
     */
63
    public function getDecorator($id) {
64
        if (false === array_key_exists($id, $this->decorators)) {
65
            return null;
66
        }
67
        return $this->decorators[$id];
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73
    public function getDecorators() {
74
        return $this->decorators;
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     */
80
    public function getFilters() {
81
        return $this->filters;
82
    }
83
84
    /**
85
     * Serialize this instance.
86
     *
87
     * @return array Returns an array representing this instance.
88
     */
89
    public function jsonSerialize() {
90
        return QueryBuilderNormalizer::normalizeQueryBuilderFilterSet($this);
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     */
96
    public function removeFilter(QueryBuilderFilterInterface $filter) {
97
        if (true === array_key_exists($filter->getId(), $this->filters)) {
98
            unset($this->filters[$filter->getId()]);
99
        }
100
        return $this;
101
    }
102
103
    /**
104
     * Set the decorators.
105
     *
106
     * @param QueryBuilderDecoratorInterface[] $decorators The decorators.
107
     * @return QueryBuilderFilterSetInterface Returns this filter set.
108
     */
109
    protected function setDecorators(array $decorators) {
110
        $this->decorators = $decorators;
111
        return $this;
112
    }
113
114
    /**
115
     * Set the filters.
116
     *
117
     * @param QueryBuilderFilterInterface[] $filters The filters.
118
     * @return QueryBuilderFilterSetInterface Returns this filter set.
119
     */
120
    protected function setFilters(array $filters) {
121
        $this->filters = $filters;
122
        return $this;
123
    }
124
}
125