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 SingleByteStringFunctions extends AbstractConvertor |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* {@inheritdoc} |
14
|
|
|
*/ |
15
|
|
|
public function getRegexpGroups() |
16
|
|
|
{ |
17
|
|
|
return [ |
18
|
|
|
'Contains' => 'Boolean', |
19
|
|
|
'EndsWith' => 'Boolean', |
20
|
|
|
'NotContains' => 'Boolean', |
21
|
|
|
'NotEndsWith' => 'Boolean', |
22
|
|
|
'NotStartsWith' => 'Boolean', |
23
|
|
|
'StartsWith' => 'Boolean', |
24
|
|
|
'StringLength' => 'Number' |
25
|
|
|
]; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function getRegexps() |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
|
|
'Contains' => 'contains \\( ((?&String)) , ((?&String)) \\)', |
35
|
|
|
'EndsWith' => 'ends-with \\( ((?&String)) , ((?&String)) \\)', |
36
|
|
|
'NotContains' => 'not \\( contains \\( ((?&String)) , ((?&String)) \\) \\)', |
37
|
|
|
'NotEndsWith' => 'not \\( ends-with \\( ((?&String)) , ((?&String)) \\) \\)', |
38
|
|
|
'NotStartsWith' => 'not \\( starts-with \\( ((?&String)) , ((?&String)) \\) \\)', |
39
|
|
|
'StartsWith' => 'starts-with \\( ((?&String)) , ((?&String)) \\)', |
40
|
|
|
'StringLength' => 'string-length \\( ((?&String))? \\)' |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Convert a call to contains() |
46
|
|
|
* |
47
|
|
|
* @param string $haystack Expression for the haystack part of the call |
48
|
|
|
* @param string $needle Expression for the needle part of the call |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function convertContains($haystack, $needle) |
52
|
|
|
{ |
53
|
|
|
return $this->generateContains($haystack, $needle, true); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Convert a call to ends-with() |
58
|
|
|
* |
59
|
|
|
* @param string $string Expression for the string part of the call |
60
|
|
|
* @param string $substring Expression for the substring part of the call |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function convertEndsWith($string, $substring) |
64
|
|
|
{ |
65
|
|
|
return $this->generateEndsWith($string, $substring, true); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Convert a call to not(contains()) |
70
|
|
|
* |
71
|
|
|
* @param string $haystack Expression for the haystack part of the call |
72
|
|
|
* @param string $needle Expression for the needle part of the call |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function convertNotContains($haystack, $needle) |
76
|
|
|
{ |
77
|
|
|
return $this->generateContains($haystack, $needle, false); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Convert a call to not(ends-with()) |
82
|
|
|
* |
83
|
|
|
* @param string $string Expression for the string part of the call |
84
|
|
|
* @param string $substring Expression for the substring part of the call |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function convertNotEndsWith($string, $substring) |
88
|
|
|
{ |
89
|
|
|
return $this->generateEndsWith($string, $substring, false); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Convert a call to not(starts-with()) |
94
|
|
|
* |
95
|
|
|
* @param string $string Expression for the string part of the call |
96
|
|
|
* @param string $substring Expression for the substring part of the call |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function convertNotStartsWith($string, $substring) |
100
|
|
|
{ |
101
|
|
|
return $this->generateStartsWith($string, $substring, false); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Convert a call to starts-with() |
106
|
|
|
* |
107
|
|
|
* @param string $string Expression for the string part of the call |
108
|
|
|
* @param string $substring Expression for the substring part of the call |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function convertStartsWith($string, $substring) |
112
|
|
|
{ |
113
|
|
|
return $this->generateStartsWith($string, $substring, true); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Convert a call to string-length() |
118
|
|
|
* |
119
|
|
|
* @param string $expr |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function convertStringLength($expr = '.') |
123
|
|
|
{ |
124
|
|
|
return "preg_match_all('(.)su'," . $this->convert($expr) . ')'; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Generate the code for a call to contains() |
129
|
|
|
* |
130
|
|
|
* @param string $haystack Expression for the haystack part of the call |
131
|
|
|
* @param string $needle Expression for the needle part of the call |
132
|
|
|
* @param bool $bool Return value for a positive match |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
protected function generateContains($haystack, $needle, $bool) |
136
|
|
|
{ |
137
|
|
|
$operator = ($bool) ? '!==' : '==='; |
138
|
|
|
|
139
|
|
|
return '(strpos(' . $this->convert($haystack) . ',' . $this->convert($needle) . ')' . $operator . 'false)'; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Generate the code for a call to ends-with() |
144
|
|
|
* |
145
|
|
|
* @param string $string Expression for the string part of the call |
146
|
|
|
* @param string $substring Expression for the substring part of the call |
147
|
|
|
* @param bool $bool Return value for a positive match |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
protected function generateEndsWith($string, $substring, $bool) |
151
|
|
|
{ |
152
|
|
|
return (preg_match('(^(?:\'[^\']+\'|"[^"]+")$)D', $substring)) |
153
|
|
|
? $this->generateEndsWithLiteral($string, $substring, $bool) |
154
|
|
|
: $this->generateEndsWithExpression($string, $substring, $bool); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Generate the code for a call to ends-with() where the second argument is a literal string |
159
|
|
|
* |
160
|
|
|
* @param string $string Expression for the string part of the call |
161
|
|
|
* @param string $substring Expression for a literal substring |
162
|
|
|
* @param bool $bool Return value for a positive match |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
protected function generateEndsWithLiteral($string, $substring, $bool) |
166
|
|
|
{ |
167
|
|
|
$operator = ($bool) ? '===' : '!=='; |
168
|
|
|
|
169
|
|
|
return '(substr(' . $this->convert($string) . ',-' . (strlen($substring) - 2) . ')' . $operator . $this->convert($substring) . ')'; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Generate the code for a call to ends-with() |
174
|
|
|
* |
175
|
|
|
* @param string $string Expression for the string part of the call |
176
|
|
|
* @param string $substring Expression for the substring part of the call |
177
|
|
|
* @param bool $bool Return value for a positive match |
178
|
|
|
* @return string |
179
|
|
|
*/ |
180
|
|
|
protected function generateEndsWithExpression($string, $substring, $bool) |
181
|
|
|
{ |
182
|
|
|
$operator = ($bool) ? '' : '!'; |
183
|
|
|
|
184
|
|
|
return $operator . "preg_match('('.preg_quote(" . $this->convert($substring) . ").'$)D'," . $this->convert($string) . ')'; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Generate the code for a call to starts-with() |
189
|
|
|
* |
190
|
|
|
* @param string $string Expression for the string part of the call |
191
|
|
|
* @param string $substring Expression for the substring part of the call |
192
|
|
|
* @param bool $bool Return value for a positive match |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
|
|
protected function generateStartsWith($string, $substring, $bool) |
196
|
|
|
{ |
197
|
|
|
$operator = ($bool) ? '===' : '!=='; |
198
|
|
|
|
199
|
|
|
return '(strpos(' . $this->convert($string) . ',' . $this->convert($substring) . ')' . $operator . '0)'; |
200
|
|
|
} |
201
|
|
|
} |