Completed
Push — master ( 27f709...a3e52e )
by Josh
03:54
created

BooleanFunctions::getMatchers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 2
cts 2
cp 1
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 BooleanFunctions extends AbstractConvertor
11
{
12
	/**
13
	* {@inheritdoc}
14
	*/
15 80
	public function getMatchers(): array
16
	{
17
		return [
18 80
			'Boolean:BooleanParam'  => 'boolean \\( ((?&Parameter)) \\)',
19
			'Boolean:HasAttribute'  => 'boolean \\( ((?&Attribute)) \\)',
20
			'Boolean:HasAttributes' => 'boolean \\( @\\* \\)',
21
			'Boolean:Not'           => [
22
				// Only try matching generic not() invocations after special cases fail
23
				'order'  => 100,
24
				'regexp' => 'not \\( ((?&Boolean)|(?&Comparison)|(?&And)|(?&Or)) \\)'
25
			],
26
			'Boolean:NotAttribute'  => 'not \\( ((?&Attribute)) \\)',
27
			'Boolean:NotParam'      => 'not \\( ((?&Parameter)) \\)'
28
		];
29
	}
30
31
	/**
32
	* Convert a call to boolean() with a param
33
	*
34
	* @param  string $expr
35
	* @return string
36
	*/
37 2
	public function parseBooleanParam($expr)
38
	{
39 2
		return $this->recurse($expr) . "!==''";
40
	}
41
42
	/**
43
	* Convert a call to boolean() with an attribute
44
	*
45
	* @param  string $expr
46
	* @return string
47
	*/
48 1
	public function parseHasAttribute($expr)
49
	{
50 1
		$attrName = $this->getAttributeName($expr);
51
52 1
		return '$node->hasAttribute(' . var_export($attrName, true) . ')';
53
	}
54
55
	/**
56
	* Convert a call to boolean(@*)
57
	*
58
	* @return string
59
	*/
60 1
	public function parseHasAttributes()
61
	{
62 1
		return '$node->attributes->length';
63
	}
64
65
	/**
66
	* Convert a call to not() with a boolean expression
67
	*
68
	* @param  string $expr
69
	* @return string
70
	*/
71 1
	public function parseNot($expr)
72
	{
73 1
		return '!(' . $this->recurse($expr) . ')';
74
	}
75
76
	/**
77
	* Convert a call to not() with an attribute
78
	*
79
	* @param  string $expr
80
	* @return string
81
	*/
82 1
	public function parseNotAttribute($expr)
83
	{
84 1
		$attrName = $this->getAttributeName($expr);
85
86 1
		return '!$node->hasAttribute(' . var_export($attrName, true) . ')';
87
	}
88
89
	/**
90
	* Convert a call to not() with a param
91
	*
92
	* @param  string $expr
93
	* @return string
94
	*/
95 1
	public function parseNotParam($expr)
96
	{
97 1
		return $this->recurse($expr) . "===''";
98
	}
99
}