|
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 Comparisons extends AbstractConvertor |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* {@inheritdoc} |
|
14
|
|
|
*/ |
|
15
|
|
|
public function getRegexpGroups() |
|
16
|
|
|
{ |
|
17
|
|
|
return [ |
|
18
|
|
|
'Eq' => 'Comparison', |
|
19
|
|
|
'Gt' => 'Comparison', |
|
20
|
|
|
'Gte' => 'Comparison', |
|
21
|
|
|
'Lt' => 'Comparison', |
|
22
|
|
|
'Lte' => 'Comparison' |
|
23
|
|
|
]; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getRegexps() |
|
30
|
|
|
{ |
|
31
|
|
|
$nonzero = '(0*[1-9]\\d*)'; |
|
32
|
|
|
$number = '(\\d+)'; |
|
33
|
|
|
$scalar = '((?&Math)|(?&Number)|(?&String))'; |
|
34
|
|
|
|
|
35
|
|
|
return [ |
|
36
|
|
|
'Eq' => $scalar . ' (!?=) ' . $scalar, |
|
37
|
|
|
'Gt' => $scalar . ' > ' . $number, |
|
38
|
|
|
'Gte' => $scalar . ' >= ' . $nonzero, |
|
39
|
|
|
'Lt' => $number . ' < ' . $scalar, |
|
40
|
|
|
'Lte' => $nonzero . ' <= ' . $scalar |
|
41
|
|
|
]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Convert an equality test |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $expr1 |
|
48
|
|
|
* @param string $operator |
|
49
|
|
|
* @param string $expr2 |
|
50
|
|
|
* @return string |
|
51
|
|
|
*/ |
|
52
|
|
|
public function convertEq($expr1, $operator, $expr2) |
|
53
|
|
|
{ |
|
54
|
|
|
$operator = $operator[0] . '='; |
|
55
|
|
|
if ($this->runner->getType($expr1) === 'String' && $this->runner->getType($expr2) === 'String') |
|
56
|
|
|
{ |
|
57
|
|
|
$operator .= '='; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $this->convertComparison($expr1, $operator, $expr2); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Convert a "greater than" comparison |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $expr1 |
|
67
|
|
|
* @param string $expr2 |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function convertGt($expr1, $expr2) |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->convertComparison($expr1, '>', $expr2); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Convert a "greater than or equal to" comparison |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $expr1 |
|
79
|
|
|
* @param string $expr2 |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public function convertGte($expr1, $expr2) |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->convertComparison($expr1, '>=', $expr2); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Convert a "less than" comparison |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $expr1 |
|
91
|
|
|
* @param string $expr2 |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
public function convertLt($expr1, $expr2) |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->convertComparison($expr1, '<', $expr2); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Convert a "less than or equal to" comparison |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $expr1 |
|
103
|
|
|
* @param string $expr2 |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
public function convertLte($expr1, $expr2) |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->convertComparison($expr1, '<=', $expr2); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Convert a comparison |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $expr1 |
|
115
|
|
|
* @param string $operator |
|
116
|
|
|
* @param string $expr2 |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function convertComparison($expr1, $operator, $expr2) |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->convert($expr1) . $operator . $this->convert($expr2); |
|
122
|
|
|
} |
|
123
|
|
|
} |