1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ChumakovAnton\Calculator; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class ExpressionStackItem |
9
|
|
|
* @package ChumakovAnton\Calculator |
10
|
|
|
*/ |
11
|
|
|
class ExpressionStackItem |
12
|
|
|
{ |
13
|
|
|
/** @var float */ |
14
|
|
|
protected $operand; |
15
|
|
|
|
16
|
|
|
/** @var Operation */ |
17
|
|
|
protected $operation; |
18
|
|
|
|
19
|
|
|
/** @var ExpressionStackItem */ |
20
|
|
|
protected $subExpression; |
21
|
|
|
|
22
|
|
|
/** @var ExpressionStackItem */ |
23
|
|
|
protected $nextExpression; |
24
|
|
|
|
25
|
|
|
/** @var ExpressionStackItem */ |
26
|
|
|
protected $parentExpression; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* ExpressionStackItem constructor. |
30
|
|
|
* @param Operation $operation |
31
|
|
|
* @param float $operand |
32
|
|
|
*/ |
33
|
|
|
public function __construct(Operation $operation, float $operand = 0) |
34
|
|
|
{ |
35
|
|
|
$this->operand = $operand; |
36
|
|
|
$this->operation = $operation; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return int |
41
|
|
|
*/ |
42
|
|
|
public function getOperationPriority(): int |
43
|
|
|
{ |
44
|
|
|
return $this->operation->getPriority(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return ExpressionStackItem |
49
|
|
|
*/ |
50
|
|
|
public function getNextExpression(): ?ExpressionStackItem |
51
|
|
|
{ |
52
|
|
|
return $this->nextExpression; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param ExpressionStackItem $nextExpression |
57
|
|
|
* @return ExpressionStackItem|null |
58
|
|
|
*/ |
59
|
|
|
public function setNextExpression(?ExpressionStackItem $nextExpression): ?ExpressionStackItem |
60
|
|
|
{ |
61
|
|
|
$this->nextExpression = $nextExpression; |
62
|
|
|
return $this->nextExpression; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return ExpressionStackItem |
67
|
|
|
*/ |
68
|
|
|
public function getParentExpression(): ?ExpressionStackItem |
69
|
|
|
{ |
70
|
|
|
return $this->parentExpression; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return float |
75
|
|
|
*/ |
76
|
|
|
protected function getOperand(): float |
77
|
|
|
{ |
78
|
|
|
return $this->operand; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param float $operand |
83
|
|
|
*/ |
84
|
|
|
public function setOperand(float $operand): void |
85
|
|
|
{ |
86
|
|
|
$this->operand = $operand; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param Operation $operation |
91
|
|
|
*/ |
92
|
|
|
public function setOperation(Operation $operation): void |
93
|
|
|
{ |
94
|
|
|
$this->operation = $operation; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return Operation|null |
99
|
|
|
*/ |
100
|
|
|
public function getOperation(): ?Operation |
101
|
|
|
{ |
102
|
|
|
return $this->operation; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return null|ExpressionStackItem |
107
|
|
|
*/ |
108
|
|
|
public function getSubExpression(): ?ExpressionStackItem |
109
|
|
|
{ |
110
|
|
|
return $this->subExpression; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param null|ExpressionStackItem $subExpression |
115
|
|
|
* @return ExpressionStackItem|null |
116
|
|
|
*/ |
117
|
|
|
protected function setSubExpression(?ExpressionStackItem $subExpression): ?ExpressionStackItem |
118
|
|
|
{ |
119
|
|
|
$this->subExpression = $subExpression; |
120
|
|
|
return $this->subExpression; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
protected function excludeSubExpression(): void |
124
|
|
|
{ |
125
|
|
|
$this->setSubExpression($this->subExpression->getSubExpression()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
protected function excludeNextExpression(): void |
129
|
|
|
{ |
130
|
|
|
$this->setSubExpression($this->nextExpression->getSubExpression()); |
131
|
|
|
$this->setNextExpression($this->nextExpression->getNextExpression()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param ExpressionStackItem $expressionStackItem |
136
|
|
|
* @return ExpressionStackItem|null |
137
|
|
|
*/ |
138
|
|
|
public function appendSubExpression(ExpressionStackItem $expressionStackItem): ?ExpressionStackItem |
139
|
|
|
{ |
140
|
|
|
$expressionStackItem->parentExpression = $this; |
141
|
|
|
return $this->setSubExpression($expressionStackItem); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param ExpressionStackItem $expressionStackItem |
146
|
|
|
* @return ExpressionStackItem|null |
147
|
|
|
*/ |
148
|
|
|
public function appendNextExpression(ExpressionStackItem $expressionStackItem): ?ExpressionStackItem |
149
|
|
|
{ |
150
|
|
|
$expressionStackItem->parentExpression = $this; |
151
|
|
|
return $this->setNextExpression($expressionStackItem); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @return float |
156
|
|
|
* @throws Exception\DivisionByZeroException |
157
|
|
|
*/ |
158
|
|
|
public function calculate(): float |
159
|
|
|
{ |
160
|
|
|
while (!empty($this->nextExpression) || !empty($this->subExpression)) { |
161
|
|
|
if (!empty($this->subExpression)) { |
162
|
|
|
$this->subExpression->calculate(); |
163
|
|
|
$this->executeOperation($this->subExpression); |
164
|
|
|
$this->excludeSubExpression(); |
165
|
|
|
} |
166
|
|
|
if (!empty($this->nextExpression)) { |
167
|
|
|
$this->executeOperation($this->nextExpression); |
168
|
|
|
$this->excludeNextExpression(); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
return $this->operand; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @param ExpressionStackItem $expressionStackItem |
176
|
|
|
* @throws Exception\DivisionByZeroException |
177
|
|
|
*/ |
178
|
|
|
protected function executeOperation(ExpressionStackItem $expressionStackItem): void |
179
|
|
|
{ |
180
|
|
|
$result = $this->operation->execute($this->operand, $expressionStackItem->getOperand()); |
181
|
|
|
$this->setOperand($result); |
182
|
|
|
$this->setOperation($expressionStackItem->getOperation()); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|