|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package s9e\TextFormatter |
|
5
|
|
|
* @copyright Copyright (c) 2010-2019 The s9e Authors |
|
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace s9e\TextFormatter\Configurator\RendererGenerators\PHP\XPathConvertor\Convertors; |
|
9
|
|
|
|
|
10
|
|
|
class Math extends AbstractConvertor |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* {@inheritdoc} |
|
14
|
|
|
*/ |
|
15
|
80 |
|
public function getMatchers(): array |
|
16
|
|
|
{ |
|
17
|
80 |
|
$number = '((?&Attribute)|(?&MathSubExpr)|(?&Number)|(?&Parameter))'; |
|
18
|
80 |
|
$math = '((?&Math)|' . substr($number, 1); |
|
19
|
|
|
|
|
20
|
|
|
return [ |
|
21
|
80 |
|
'Math:Addition' => $number . ' \\+ ' . $math, |
|
22
|
80 |
|
'Math:Division' => $number . ' div ' . $math, |
|
23
|
80 |
|
'Math:MathSubExpr' => '\\( ((?&Math)) \\)', |
|
24
|
80 |
|
'Math:Multiplication' => $number . ' \\* ' . $math, |
|
25
|
80 |
|
'Math:Substraction' => $number . ' - ' . $math |
|
26
|
|
|
]; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Convert an addition |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $expr1 |
|
33
|
|
|
* @param string $expr2 |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
8 |
|
public function parseAddition($expr1, $expr2) |
|
37
|
|
|
{ |
|
38
|
8 |
|
return $this->convertOperation($expr1, '+', $expr2); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Convert a division |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $expr1 |
|
45
|
|
|
* @param string $expr2 |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
4 |
|
public function parseDivision($expr1, $expr2) |
|
49
|
|
|
{ |
|
50
|
4 |
|
return $this->convertOperation($expr1, '/', $expr2); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Convert a math subexpression |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $expr |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
3 |
|
public function parseMathSubExpr($expr) |
|
60
|
|
|
{ |
|
61
|
3 |
|
return '(' . $this->recurse($expr) . ')'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Convert a multiplication |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $expr1 |
|
68
|
|
|
* @param string $expr2 |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
6 |
|
public function parseMultiplication($expr1, $expr2) |
|
72
|
|
|
{ |
|
73
|
6 |
|
return $this->convertOperation($expr1, '*', $expr2); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Convert a substraction |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $expr1 |
|
80
|
|
|
* @param string $expr2 |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
3 |
|
public function parseSubstraction($expr1, $expr2) |
|
84
|
|
|
{ |
|
85
|
3 |
|
return $this->convertOperation($expr1, '-', $expr2); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Convert an operation |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $expr1 |
|
92
|
|
|
* @param string $operator |
|
93
|
|
|
* @param string $expr2 |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
14 |
|
protected function convertOperation($expr1, $operator, $expr2) |
|
97
|
|
|
{ |
|
98
|
14 |
|
$expr1 = $this->recurse($expr1); |
|
99
|
14 |
|
$expr2 = $this->recurse($expr2); |
|
100
|
|
|
|
|
101
|
|
|
// Prevent two consecutive minus signs to be interpreted as a post-decrement operator |
|
102
|
14 |
|
if ($operator === '-' && $expr2[0] === '-') |
|
103
|
|
|
{ |
|
104
|
1 |
|
$operator .= ' '; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
14 |
|
return $expr1 . $operator . $expr2; |
|
108
|
|
|
} |
|
109
|
|
|
} |