Completed
Push — master ( 9f0299...84da34 )
by Josh
15:47
created

Comparisons::convertComparison()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
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 12
	public function getRegexpGroups()
16
	{
17
		return [
18 12
			'Eq'  => 'Comparison',
19
			'Gt'  => 'Comparison',
20
			'Gte' => 'Comparison',
21
			'Lt'  => 'Comparison',
22
			'Lte' => 'Comparison'
23
		];
24
	}
25
26
	/**
27
	* {@inheritdoc}
28
	*/
29 12
	public function getRegexps()
30
	{
31 12
		$nonzero = '(0*[1-9]\\d*)';
32 12
		$number  = '(\\d+)';
33 12
		$scalar  = '((?&Math)|(?&Number)|(?&String))';
34
35
		return [
36 12
			'Eq'  => $scalar . ' (!?=) ' .  $scalar,
37 12
			'Gt'  => $scalar . ' > ' . $number,
38 12
			'Gte' => $scalar . ' >= ' . $nonzero,
39 12
			'Lt'  => $number . ' < ' . $scalar,
40 12
			'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 4
	public function convertEq($expr1, $operator, $expr2)
53
	{
54 4
		$operator = $operator[0] . '=';
55 4
		if ($this->runner->getType($expr1) === 'String' && $this->runner->getType($expr2) === 'String')
56
		{
57 1
			$operator .= '=';
58
		}
59
60 4
		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 1
	public function convertGt($expr1, $expr2)
71
	{
72 1
		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 1
	public function convertGte($expr1, $expr2)
83
	{
84 1
		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 1
	public function convertLt($expr1, $expr2)
95
	{
96 1
		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 1
	public function convertLte($expr1, $expr2)
107
	{
108 1
		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 8
	protected function convertComparison($expr1, $operator, $expr2)
120
	{
121 8
		return $this->convert($expr1) . $operator . $this->convert($expr2);
122
	}
123
}