Completed
Branch XPathConvertorRefactor (245ee2)
by Josh
12:06
created

SingleByteStringFunctions   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 89
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getRegexpGroups() 0 10 1
A getRegexps() 0 10 1
A convertContains() 0 4 1
A convertNotContains() 0 4 1
A convertNotStartsWith() 0 4 1
A convertStartsWith() 0 4 1
A convertStringLength() 0 4 1
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
}