|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ChumakovAnton\Calculator; |
|
6
|
|
|
|
|
7
|
|
|
use ChumakovAnton\Calculator\Exception\InvalidArgumentException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ExpressionCalculator |
|
11
|
|
|
* @package ChumakovAnton\Calculator |
|
12
|
|
|
*/ |
|
13
|
|
|
class ExpressionCalculator implements Calculator |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @param string $input |
|
17
|
|
|
* @return float |
|
18
|
|
|
* @throws InvalidArgumentException |
|
19
|
|
|
* @throws Exception\DivisionByZeroException |
|
20
|
|
|
*/ |
|
21
|
|
|
public function process(string $input = ''): float |
|
22
|
|
|
{ |
|
23
|
|
|
self::validateInput($input); |
|
24
|
|
|
$matches = []; |
|
25
|
|
|
preg_match_all('/(\-?\d+)([\+\-\*\/])?/', $input, $matches); |
|
26
|
|
|
$rootExpression = null; |
|
27
|
|
|
$expressionPoint = null; |
|
28
|
|
|
foreach ($matches[0] as $key => $value) { |
|
29
|
|
|
$expression = new ExpressionStackItem(new DefaultOperation($matches[2][$key]), (float)$matches[1][$key]); |
|
30
|
|
|
|
|
31
|
|
|
if (null === $rootExpression) { |
|
32
|
|
|
$rootExpression = $expression; |
|
33
|
|
|
$expressionPoint = $rootExpression; |
|
34
|
|
|
continue; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$isHigherPriority = $expression->getOperationPriority() > $expressionPoint->getOperationPriority(); |
|
38
|
|
|
$isLowerPriority = $expression->getOperationPriority() < $expressionPoint->getOperationPriority(); |
|
39
|
|
|
|
|
40
|
|
|
if ($isHigherPriority) { |
|
41
|
|
|
$expressionPoint = $expressionPoint->appendSubExpression($expression); |
|
42
|
|
|
} else { |
|
43
|
|
|
$expressionPoint->appendNextExpression($expression); |
|
44
|
|
|
|
|
45
|
|
|
if ($isLowerPriority) { |
|
46
|
|
|
$expressionPoint = $expressionPoint->getParentExpression(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$expressionPoint->calculate(); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
return $rootExpression->calculate(); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $input |
|
57
|
|
|
* @throws InvalidArgumentException |
|
58
|
|
|
*/ |
|
59
|
|
|
protected static function validateInput(string $input): void |
|
60
|
|
|
{ |
|
61
|
|
|
if (empty($input)) { |
|
62
|
|
|
throw new InvalidArgumentException('Empty input string'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (preg_match('/([\+\-\*\/]){2,}/', $input) === 1) { |
|
66
|
|
|
throw new InvalidArgumentException('Double arithmetic operation'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (!is_numeric($input[0]) && $input[0] !== '-') { |
|
70
|
|
|
throw new InvalidArgumentException('First symbol is incorrect'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (!is_numeric($input[strlen($input) - 1])) { |
|
74
|
|
|
throw new InvalidArgumentException('Last symbol is incorrect. Empty right operand'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if (preg_match('/([^\d\+\-\*\/])+/', $input) === 1) { |
|
78
|
|
|
throw new InvalidArgumentException('Input string has incorrect characters'); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.