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