1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/old-town/old-town-workflow |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace OldTown\Workflow\Query; |
7
|
|
|
|
8
|
|
|
use OldTown\Workflow\Exception\ArgumentNotNumericException; |
9
|
|
|
use OldTown\Workflow\Exception\InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class NestedExpression |
13
|
|
|
* @package OldTown\Workflow\Query |
14
|
|
|
*/ |
15
|
|
|
class NestedExpression extends AbstractExpression |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
* @var integer |
20
|
|
|
*/ |
21
|
|
|
const AND_OPERATOR = 6; |
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
* @var integer |
25
|
|
|
*/ |
26
|
|
|
const OR_OPERATOR = 7; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* |
30
|
|
|
* |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
private $expressionOperator = self::AND_OPERATOR; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var AbstractExpression[] |
37
|
|
|
*/ |
38
|
|
|
private $expressions = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param array $expressions |
42
|
|
|
* @param null $expressionOperator |
43
|
|
|
* @throws ArgumentNotNumericException |
44
|
|
|
* @throws InvalidArgumentException |
45
|
|
|
*/ |
46
|
3 |
|
public function __construct(array $expressions = null, $expressionOperator = null) |
47
|
|
|
{ |
48
|
3 |
|
if (null !== $expressions) { |
49
|
3 |
|
$this->setExpressions($expressions); |
50
|
3 |
|
} |
51
|
|
|
|
52
|
3 |
|
if (null !== $expressionOperator) { |
53
|
3 |
|
$this->setExpressionOperator($expressionOperator); |
54
|
3 |
|
} |
55
|
3 |
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Флаг определяющий есть ли вложенные свойства |
60
|
|
|
* |
61
|
|
|
* @return boolean |
62
|
|
|
*/ |
63
|
3 |
|
public function isNested() |
64
|
|
|
{ |
65
|
3 |
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Возвращает тип вложенного выражения |
70
|
|
|
* |
71
|
|
|
* @return int |
72
|
|
|
*/ |
73
|
3 |
|
public function getExpressionOperator() |
74
|
|
|
{ |
75
|
3 |
|
return $this->expressionOperator; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Устанавливает тип вложенного выражения |
80
|
|
|
* |
81
|
|
|
* @param int $expressionOperator |
82
|
|
|
* @return $this |
83
|
|
|
* @throws ArgumentNotNumericException |
84
|
|
|
*/ |
85
|
3 |
View Code Duplication |
public function setExpressionOperator($expressionOperator) |
|
|
|
|
86
|
|
|
{ |
87
|
3 |
|
if (!is_numeric($expressionOperator)) { |
88
|
|
|
$errMsg = sprintf('Аргумент должен быть числом. Актуальное значение %s', $expressionOperator); |
89
|
|
|
throw new ArgumentNotNumericException($errMsg); |
90
|
|
|
} |
91
|
|
|
|
92
|
3 |
|
$this->expressionOperator = (integer)$expressionOperator; |
93
|
|
|
|
94
|
3 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Возвращает выражения |
99
|
|
|
* |
100
|
|
|
* @return AbstractExpression[] |
101
|
|
|
*/ |
102
|
3 |
|
public function getExpressions() |
103
|
|
|
{ |
104
|
3 |
|
return $this->expressions; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Устанавливает выражения |
109
|
|
|
* |
110
|
|
|
* @param AbstractExpression[] $expressions |
111
|
|
|
* @return $this |
112
|
|
|
* @throws InvalidArgumentException |
113
|
|
|
*/ |
114
|
3 |
|
public function setExpressions(array $expressions = []) |
115
|
|
|
{ |
116
|
3 |
|
$this->validExpressions($expressions); |
117
|
3 |
|
$this->expressions = $expressions; |
118
|
|
|
|
119
|
3 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Проверка корректности колекции выражений |
124
|
|
|
* |
125
|
|
|
* @param array $expressions |
126
|
|
|
* @return void |
127
|
|
|
* @throws InvalidArgumentException |
128
|
|
|
*/ |
129
|
3 |
|
protected function validExpressions(array $expressions = []) |
130
|
|
|
{ |
131
|
3 |
|
foreach ($expressions as $expression) { |
132
|
3 |
|
if (!$expression instanceof AbstractExpression) { |
133
|
|
|
$errMsg = 'Некорректная коллекция объектов запросов в workflow'; |
134
|
|
|
throw new InvalidArgumentException($errMsg); |
135
|
|
|
} |
136
|
3 |
|
} |
137
|
3 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Колличество выражений |
141
|
|
|
* |
142
|
|
|
* @return int |
143
|
|
|
*/ |
144
|
|
|
public function getExpressionCount() |
145
|
|
|
{ |
146
|
|
|
$expressionCount = count($this->expressions); |
147
|
|
|
|
148
|
|
|
return $expressionCount; |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.