|
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
|
|
|
'NotContains' => 'Boolean', |
|
20
|
|
|
'NotStartsWith' => 'Boolean', |
|
21
|
|
|
'StartsWith' => 'Boolean', |
|
22
|
|
|
'StringLength' => 'Number' |
|
23
|
|
|
]; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getRegexps() |
|
30
|
|
|
{ |
|
31
|
|
|
return [ |
|
32
|
|
|
'Contains' => 'contains \\( ((?&String)) , ((?&String)) \\)', |
|
33
|
|
|
'NotContains' => 'not \\( contains \\( ((?&String)) , ((?&String)) \\) \\)', |
|
34
|
|
|
'NotStartsWith' => 'not \\( starts-with \\( ((?&String)) , ((?&String)) \\) \\)', |
|
35
|
|
|
'StartsWith' => 'starts-with \\( ((?&String)) , ((?&String)) \\)', |
|
36
|
|
|
'StringLength' => 'string-length \\( ((?&String))? \\)' |
|
37
|
|
|
]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Convert a call to contains() |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $haystack Expression for the haystack part of the call |
|
44
|
|
|
* @param string $needle Expression for the needle part of the call |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function convertContains($haystack, $needle) |
|
48
|
|
|
{ |
|
49
|
|
|
return '(strpos(' . $this->convert($haystack) . ',' . $this->convert($needle) . ')!==false)'; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Convert a call to not(contains()) |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $haystack Expression for the haystack part of the call |
|
56
|
|
|
* @param string $needle Expression for the needle part of the call |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function convertNotContains($haystack, $needle) |
|
60
|
|
|
{ |
|
61
|
|
|
return '(strpos(' . $this->convert($haystack) . ',' . $this->convert($needle) . ')===false)'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Convert a call to not(starts-with()) |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $string Expression for the string part of the call |
|
68
|
|
|
* @param string $substring Expression for the substring part of the call |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
public function convertNotStartsWith($string, $substring) |
|
72
|
|
|
{ |
|
73
|
|
|
return '(strpos(' . $this->convert($string) . ',' . $this->convert($substring) . ')!==0)'; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Convert a call to starts-with() |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $string Expression for the string part of the call |
|
80
|
|
|
* @param string $substring Expression for the substring part of the call |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
public function convertStartsWith($string, $substring) |
|
84
|
|
|
{ |
|
85
|
|
|
return '(strpos(' . $this->convert($string) . ',' . $this->convert($substring) . ')===0)'; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Convert a call to string-length() |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $expr |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
public function convertStringLength($expr = '.') |
|
95
|
|
|
{ |
|
96
|
|
|
return "preg_match_all('(.)su'," . $this->convert($expr) . ')'; |
|
97
|
|
|
} |
|
98
|
|
|
} |