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

QueryBuilderRuleSet::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the jquery-querybuilder-bundle package.
5
 *
6
 * (c) 2019 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 InvalidArgumentException;
15
use WBW\Bundle\JQuery\QueryBuilderBundle\API\QueryBuilderConditionInterface;
16
use WBW\Bundle\JQuery\QueryBuilderBundle\API\QueryBuilderRuleInterface;
17
use WBW\Bundle\JQuery\QueryBuilderBundle\API\QueryBuilderRuleSetInterface;
18
19
/**
20
 * QueryBuilder rule set.
21
 *
22
 * @author webeweb <https://github.com/webeweb/>
23
 * @package WBW\Bundle\JQuery\QueryBuilderBundle\Model
24
 */
25
class QueryBuilderRuleSet implements QueryBuilderConditionInterface, QueryBuilderRuleSetInterface {
26
27
    /**
28
     * Condition.
29
     *
30
     * @var string|null
31
     */
32
    private $condition;
33
34
    /**
35
     * Rules.
36
     *
37
     * @var array
38
     */
39
    private $rules;
40
41
    /**
42
     * Valid.
43
     *
44
     * @var bool
45
     */
46
    private $valid;
47
48
    /**
49
     * Constructor.
50
     */
51
    public function __construct() {
52
        $this->setRules([]);
53
        $this->setValid(false);
54
    }
55
56
    /**
57
     * Add a rule.
58
     *
59
     * @param QueryBuilderRuleInterface $rule The rule.
60
     * @return QueryBuilderRuleSetInterface Returns this rule set.
61
     */
62
    public function addRule(QueryBuilderRuleInterface $rule): QueryBuilderRuleSetInterface {
63
        $this->rules[] = $rule;
64
        return $this;
65
    }
66
67
    /**
68
     * Add a rule set.
69
     *
70
     * @param QueryBuilderRuleSetInterface $rule The rule.
71
     * @return QueryBuilderRuleSetInterface Returns this rule set.
72
     */
73
    public function addRuleSet(QueryBuilderRuleSetInterface $rule): QueryBuilderRuleSetInterface {
74
        $this->rules[] = $rule;
75
        return $this;
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81
    public function getCondition(): ?string {
82
        return $this->condition;
83
    }
84
85
    /**
86
     * {@inheritDoc}
87
     */
88
    public function getRules(): array {
89
        return $this->rules;
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95
    public function getValid(): bool {
96
        return $this->valid;
97
    }
98
99
    /**
100
     * Set the condition.
101
     *
102
     * @param string|null $condition The condition.
103
     * @return QueryBuilderRuleSetInterface Returns this rule set.
104
     * @throws InvalidArgumentException Throws an invalid argument exception if the condition is invalid.
105
     */
106
    public function setCondition(?string $condition): QueryBuilderRuleSetInterface {
107
        if (null !== $condition && false === in_array($condition, QueryBuilderEnumerator::enumConditions())) {
108
            throw new InvalidArgumentException(sprintf('The condition "%s" is invalid', $condition));
109
        }
110
        $this->condition = $condition;
111
        return $this;
112
    }
113
114
    /**
115
     * Set the rules.
116
     *
117
     * @param array $rules The rules.
118
     * @return QueryBuilderRuleSet Returns this rule set.
119
     */
120
    protected function setRules(array $rules): QueryBuilderRuleSetInterface {
121
        $this->rules = $rules;
122
        return $this;
123
    }
124
125
    /**
126
     * Set the valid.
127
     *
128
     * @param bool $valid The valid.
129
     * @return QueryBuilderRuleSetInterface Returns this rule set.
130
     */
131
    public function setValid(bool $valid): QueryBuilderRuleSetInterface {
132
        $this->valid = $valid;
133
        return $this;
134
    }
135
}
136