Completed
Branch XPathConvertorRefactor (245ee2)
by Josh
12:06
created

Comparisons::convertLt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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
		return [
32
			'Eq'  => '((?&Math)|(?&Number)|(?&String)) (!?=) ((?&Math)|(?&Number)|(?&String))',
33
			'Gt'  => '((?&Math)|(?&Number)|(?&String)) > (\\d+)',
34
			'Gte' => '((?&Math)|(?&Number)|(?&String)) >= (0*[1-9]\\d*)',
35
			'Lt'  => '(\\d+) < ((?&Math)|(?&Number)|(?&String))',
36
			'Lte' => '(0*[1-9]\\d*) <= ((?&Math)|(?&Number)|(?&String))'
37
		];
38
	}
39
40
	/**
41
	* Convert an equality test
42
	*
43
	* @param  string $expr1
44
	* @param  string $operator
45
	* @param  string $expr2
46
	* @return string
47
	*/
48
	public function convertEq($expr1, $operator, $expr2)
49
	{
50
		return $this->convertComparison($expr1, $operator[0] . '=', $expr2);
51
	}
52
53
	/**
54
	* Convert a "greater than" comparison
55
	*
56
	* @param  string $expr1
57
	* @param  string $expr2
58
	* @return string
59
	*/
60
	public function convertGt($expr1, $expr2)
61
	{
62
		return $this->convertComparison($expr1, '>', $expr2);
63
	}
64
65
	/**
66
	* Convert a "greater than or equal to" comparison
67
	*
68
	* @param  string $expr1
69
	* @param  string $expr2
70
	* @return string
71
	*/
72
	public function convertGte($expr1, $expr2)
73
	{
74
		return $this->convertComparison($expr1, '>=', $expr2);
75
	}
76
77
	/**
78
	* Convert a "less than" comparison
79
	*
80
	* @param  string $expr1
81
	* @param  string $expr2
82
	* @return string
83
	*/
84
	public function convertLt($expr1, $expr2)
85
	{
86
		return $this->convertComparison($expr1, '<', $expr2);
87
	}
88
89
	/**
90
	* Convert a "less than or equal to" comparison
91
	*
92
	* @param  string $expr1
93
	* @param  string $expr2
94
	* @return string
95
	*/
96
	public function convertLte($expr1, $expr2)
97
	{
98
		return $this->convertComparison($expr1, '<=', $expr2);
99
	}
100
101
	/**
102
	* Convert a comparison
103
	*
104
	* @param  string $expr1
105
	* @param  string $operator
106
	* @param  string $expr2
107
	* @return string
108
	*/
109
	protected function convertComparison($expr1, $operator, $expr2)
110
	{
111
		return $this->convert($expr1) . $operator . $this->convert($expr2);
112
	}
113
}