|
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 BooleanFunctions extends AbstractConvertor |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* {@inheritdoc} |
|
14
|
|
|
*/ |
|
15
|
|
|
public function getRegexpGroups() |
|
16
|
|
|
{ |
|
17
|
|
|
return [ |
|
18
|
|
|
'BooleanParam' => 'Boolean', |
|
19
|
|
|
'HasAttribute' => 'Boolean', |
|
20
|
|
|
'HasAttributes' => 'Boolean', |
|
21
|
|
|
'Not' => 'Boolean', |
|
22
|
|
|
'NotAttribute' => 'Boolean', |
|
23
|
|
|
'NotParam' => 'Boolean' |
|
24
|
|
|
]; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
|
|
public function getRegexps() |
|
31
|
|
|
{ |
|
32
|
|
|
return [ |
|
33
|
|
|
'BooleanParam' => 'boolean \\( ((?&Parameter)) \\)', |
|
34
|
|
|
'HasAttribute' => 'boolean \\( ((?&Attribute)) \\)', |
|
35
|
|
|
'HasAttributes' => 'boolean \\( @\\* \\)', |
|
36
|
|
|
'Not' => 'not \\( ((?&Boolean)|(?&Comparison)|(?&And)|(?&Or)) \\)', |
|
37
|
|
|
'NotAttribute' => 'not \\( ((?&Attribute)) \\)', |
|
38
|
|
|
'NotParam' => 'not \\( ((?&Parameter)) \\)' |
|
39
|
|
|
]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Convert a call to boolean() with a param |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $expr |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public function convertBooleanParam($expr) |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->convert($expr) . "!==''"; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Convert a call to boolean() with an attribute |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $expr |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function convertHasAttribute($expr) |
|
60
|
|
|
{ |
|
61
|
|
|
$attrName = $this->getAttributeName($expr); |
|
62
|
|
|
|
|
63
|
|
|
return '$node->hasAttribute(' . var_export($attrName, true) . ')'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Convert a call to boolean(@*) |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
public function convertHasAttributes() |
|
72
|
|
|
{ |
|
73
|
|
|
return '$node->attributes->length'; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Convert a call to not() with a boolean expression |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $expr |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public function convertNot($expr) |
|
83
|
|
|
{ |
|
84
|
|
|
return '!(' . $this->convert($expr) . ')'; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Convert a call to not() with an attribute |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $expr |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function convertNotAttribute($expr) |
|
94
|
|
|
{ |
|
95
|
|
|
$attrName = $this->getAttributeName($expr); |
|
96
|
|
|
|
|
97
|
|
|
return '!$node->hasAttribute(' . var_export($attrName, true) . ')'; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Convert a call to not() with a param |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $expr |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
|
|
public function convertNotParam($expr) |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->convert($expr) . "===''"; |
|
109
|
|
|
} |
|
110
|
|
|
} |