|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package s9e\TextFormatter |
|
5
|
|
|
* @copyright Copyright (c) 2010-2018 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
|
|
|
public function getRegexpGroups() |
|
16
|
|
|
{ |
|
17
|
|
|
return [ |
|
18
|
|
|
'Addition' => 'Math', |
|
19
|
|
|
'Division' => 'Math', |
|
20
|
|
|
'MathSub' => 'Math', |
|
21
|
|
|
'Multiplication' => 'Math', |
|
22
|
|
|
'Substraction' => 'Math' |
|
23
|
|
|
]; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getRegexps() |
|
30
|
|
|
{ |
|
31
|
|
|
$number = '((?&Attribute)|(?&MathSub)|(?&Number)|(?&Parameter))'; |
|
32
|
|
|
$math = '((?&Math)|' . substr($number, 1); |
|
33
|
|
|
|
|
34
|
|
|
return [ |
|
35
|
|
|
'Addition' => $number . ' \\+ ' . $math, |
|
36
|
|
|
'Division' => $number . ' div ' . $math, |
|
37
|
|
|
'MathSub' => '\\( ((?&Math)) \\)', |
|
38
|
|
|
'Multiplication' => $number . ' \\* ' . $math, |
|
39
|
|
|
'Substraction' => $number . ' - ' . $math |
|
40
|
|
|
]; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Convert an addition |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $expr1 |
|
47
|
|
|
* @param string $expr2 |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public function convertAddition($expr1, $expr2) |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->convertOperation($expr1, '+', $expr2); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Convert a division |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $expr1 |
|
59
|
|
|
* @param string $expr2 |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
public function convertDivision($expr1, $expr2) |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->convertOperation($expr1, '/', $expr2); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Convert a math subexpression |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $expr |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
public function convertMathSub($expr) |
|
74
|
|
|
{ |
|
75
|
|
|
return '(' . $this->convert($expr) . ')'; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Convert a multiplication |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $expr1 |
|
82
|
|
|
* @param string $expr2 |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
public function convertMultiplication($expr1, $expr2) |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->convertOperation($expr1, '*', $expr2); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Convert a substraction |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $expr1 |
|
94
|
|
|
* @param string $expr2 |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function convertSubstraction($expr1, $expr2) |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->convertOperation($expr1, '-', $expr2); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Convert an operation |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $expr1 |
|
106
|
|
|
* @param string $operator |
|
107
|
|
|
* @param string $expr2 |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function convertOperation($expr1, $operator, $expr2) |
|
111
|
|
|
{ |
|
112
|
|
|
$expr1 = $this->convert($expr1); |
|
113
|
|
|
$expr2 = $this->convert($expr2); |
|
114
|
|
|
|
|
115
|
|
|
// Prevent two consecutive minus signs to be interpreted as a post-decrement operator |
|
116
|
|
|
if ($operator === '-' && $expr2[0] === '-') |
|
117
|
|
|
{ |
|
118
|
|
|
$operator .= ' '; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $expr1 . $operator . $expr2; |
|
122
|
|
|
} |
|
123
|
|
|
} |