Completed
Push — master ( c8e302...7e512b )
by Josh
03:51
created

FilterSyntaxMatcher::parseFalse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php declare(strict_types=1);
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\Helpers;
9
10
use s9e\TextFormatter\Configurator\Items\Regexp;
11
use s9e\TextFormatter\Configurator\RecursiveParser\AbstractRecursiveMatcher;
12
13
class FilterSyntaxMatcher extends AbstractRecursiveMatcher
14
{
15
	/**
16
	* {@inheritdoc}
17
	*/
18
	public function getMatchers(): array
19
	{
20
		return [
21
			'Array' => [
22
				'groups' => ['ArrayValue', 'FilterArg'],
23
				'regexp' => '\\[ ((?&ArrayElements))? \\]',
24
			],
25
			'ArrayElement' => [
26
				'regexp' => '(?:((?&ArrayKey)) => )?((?&ArrayValue))',
27
			],
28
			'ArrayElements' => [
29
				'regexp' => '((?&ArrayElement))(?: , ((?&ArrayElements)))?',
30
			],
31
			'DoubleQuotedString' => [
32
				'groups' => ['ArrayKey', 'ArrayValue', 'FilterArg'],
33
				'regexp' => '"((?:[^\\\\"]|\\\\.)*)"',
34
			],
35
			'False' => [
36
				'groups' => ['ArrayKey', 'ArrayValue', 'FilterArg'],
37
				'regexp' => '[Ff][Aa][Ll][Ss][Ee]',
38
			],
39
			'FilterArgs' => [
40
				'regexp' => '((?&FilterArg))(?: , ((?&FilterArgs)))?',
41
			],
42
			'FilterCallback' => [
43
				'regexp' => '([#:\\\\\\w]+)(?: \\( ((?&FilterArgs)?) \\))?',
44
			],
45
			'Float' => [
46
				'groups' => ['ArrayKey', 'ArrayValue', 'FilterArg'],
47
				'regexp' => '([-+]?(?:\\.[0-9]+|[0-9]+\\.[0-9]*|[0-9]+(?=[Ee]))(?:[Ee]-?[0-9]+)?)',
48
			],
49
			'Integer' => [
50
				'groups' => ['ArrayKey', 'ArrayValue', 'FilterArg'],
51
				'regexp' => '(-?(?:0[Bb][01]+|0[Xx][0-9A-Fa-f]+|[0-9]+))',
52
			],
53
			'Null' => [
54
				'groups' => ['ArrayKey', 'ArrayValue', 'FilterArg'],
55
				'regexp' => '[Nn][Uu][Ll][Ll]',
56
			],
57
			'Param' => [
58
				'groups' => ['FilterArg'],
59
				'regexp' => '\\$(\\w+(?:\\.\\w+)*)',
60
			],
61
			'Regexp' => [
62
				'groups' => ['ArrayValue', 'FilterArg'],
63
				'regexp' => '(/(?:[^\\\\/]|\\\\.)*/)([Sgimsu]*)',
64
			],
65
			'SingleQuotedString' => [
66
				'groups' => ['ArrayKey', 'ArrayValue', 'FilterArg'],
67
				'regexp' => "'((?:[^\\\\']|\\\\.)*)'",
68
			],
69
			'True' => [
70
				'groups' => ['ArrayKey', 'ArrayValue', 'FilterArg'],
71
				'regexp' => '[Tt][Rr][Uu][Ee]'
72
			]
73
		];
74
	}
75
76
	/**
77
	* @param  string $elements
78
	* @return array
79
	*/
80
	public function parseArray(string $elements = ''): array
81
	{
82
		$array = [];
83
		if ($elements !== '')
84
		{
85
			foreach ($this->recurse($elements, 'ArrayElements') as $element)
86
			{
87
				if (array_key_exists('key', $element))
88
				{
89
					$array[$element['key']] = $element['value'];
90
				}
91
				else
92
				{
93
					$array[] = $element['value'];
94
				}
95
			}
96
		}
97
98
		return $array;
99
	}
100
101
	/**
102
	* @param  string $key
103
	* @param  string $value
104
	* @return array
105
	*/
106
	public function parseArrayElement(string $key, string $value): array
107
	{
108
		$element = ['value' => $this->recurse($value, 'ArrayValue')];
109
		if ($key !== '')
110
		{
111
			$element['key'] = $this->recurse($key, 'ArrayKey');
112
		}
113
114
		return $element;
115
	}
116
117
	/**
118
	* @param  string $firstElement
119
	* @param  string $otherElements
120
	* @return array
121
	*/
122
	public function parseArrayElements(string $firstElement, string $otherElements = null)
123
	{
124
		$elements = [$this->recurse($firstElement, 'ArrayElement')];
125
		if (isset($otherElements))
126
		{
127
			$elements = array_merge($elements, $this->recurse($otherElements, 'ArrayElements'));
128
		}
129
130
		return $elements;
131
	}
132
133
	/**
134
	* @param  string $str
135
	* @return string
136
	*/
137
	public function parseDoubleQuotedString(string $str): string
138
	{
139
		return stripcslashes($str);
140
	}
141
142
	/**
143
	* @return bool
144
	*/
145
	public function parseFalse(): bool
146
	{
147
		return false;
148
	}
149
150
	/**
151
	* @param  string $callback
152
	* @param  string $args
153
	* @return array
154
	*/
155
	public function parseFilterCallback(string $callback, string $args = null): array
156
	{
157
		$config = ['filter' => $callback];
158
		if (isset($args))
159
		{
160
			$config['params'] = ($args === '') ? [] : $this->recurse($args, 'FilterArgs');
161
		}
162
163
		return $config;
164
	}
165
166
	/**
167
	* @param  string $firstArg
168
	* @param  string $otherArgs
169
	* @return array
170
	*/
171
	public function parseFilterArgs(string $firstArg, string $otherArgs = null)
172
	{
173
		$parsedArg = $this->parser->parse($firstArg, 'FilterArg');
174
175
		$type = ($parsedArg['match'] === 'Param') ? 'Name' : 'Value';
176
		$args = [[$type, $parsedArg['value']]];
177
		if (isset($otherArgs))
178
		{
179
			$args = array_merge($args, $this->recurse($otherArgs, 'FilterArgs'));
180
		}
181
182
		return $args;
183
	}
184
185
	/**
186
	* @return null
187
	*/
188
	public function parseNull()
189
	{
190
		return null;
191
	}
192
193
	/**
194
	* @param  string $str
195
	* @return float
196
	*/
197
	public function parseFloat(string $str): float
198
	{
199
		return (float) $str;
200
	}
201
202
	/**
203
	* @param  string $str
204
	* @return integer
205
	*/
206
	public function parseInteger(string $str): int
207
	{
208
		return intval($str, 0);
209
	}
210
211
	/**
212
	* @param  string $str
213
	* @return string
214
	*/
215
	public function parseParam(string $str): string
216
	{
217
		return $str;
218
	}
219
220
	/**
221
	* @param  string $regexp
222
	* @param  string $flags
223
	* @return Regexp
224
	*/
225
	public function parseRegexp(string $regexp, string $flags): Regexp
226
	{
227
		$regexp .= str_replace('g', '', $flags);
228
229
		return new Regexp($regexp, true);
230
	}
231
232
	/**
233
	* @param  string $str
234
	* @return string
235
	*/
236
	public function parseSingleQuotedString(string $str): string
237
	{
238
		return preg_replace("(\\\\([\\\\']))", '$1', $str);
239
	}
240
241
	/**
242
	* @return bool
243
	*/
244
	public function parseTrue(): bool
245
	{
246
		return true;
247
	}
248
}