RegexFactory::end()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * @package    Fuel\Common
4
 * @version    2.0
5
 * @author     Fuel Development Team
6
 * @license    MIT License
7
 * @copyright  2010 - 2015 Fuel Development Team
8
 * @link       http://fuelphp.com
9
 */
10
11
namespace Fuel\Common;
12
13
/**
14
 * Class for building regular expressions
15
 *
16
 * @package Fuel\Common
17
 *
18
 * @since 2.0
19
 */
20
class RegexFactory
21
{
22
23
	/**
24
	 * Contains the expression being built
25
	 *
26
	 * @var string
27
	 */
28
	protected $expression;
29
30
	/**
31
	 * Keeps track of any open delimiters to ensure everything is closed correctly
32
	 *
33
	 * @var int
34
	 */
35
	protected $openDelimiters;
36
37
	/**
38
	 * Contains the status of any flags that have been added
39
	 *
40
	 * @var string
41
	 */
42
	protected $flags;
43
44
	public function __construct()
45
	{
46
		$this->reset();
47
	}
48
49
	/**
50
	 * Resets the string being built
51
	 *
52
	 * @return $this
53
	 *
54
	 * @since 2.0
55
	 */
56
	public function reset()
57
	{
58
		$this->expression = '';
59
		$this->openDelimiters = 0;
60
		$this->flags = '';
61
62
		return $this;
63
	}
64
65
	/**
66
	 * Gets the built expression
67
	 *
68
	 * @param bool $delimit Set to false to disable adding the delimiters to the string
69
	 *
70
	 * @return string
71
	 *
72
	 * @since 2.0
73
	 */
74
	public function get($delimit = true)
75
	{
76
		// If there are still open delimiters throw an exception
77
		if ($this->openDelimiters !== 0)
78
		{
79
			throw new \LogicException('A delimiter has not been closed!');
80
		}
81
82
		$expression = $this->expression;
83
84
		if ($delimit)
85
		{
86
			$expression = '/' . $expression . '/'  . $this->flags;
87
		}
88
89
		return $expression;
90
	}
91
92
	/**
93
	 * Calls get() to return the built regex
94
	 *
95
	 * @return string
96
	 *
97
	 * @since 2.0
98
	 */
99
	public function __toString()
100
	{
101
		return $this->get();
102
	}
103
104
	/**
105
	 * Adds any given value to the expression being built
106
	 *
107
	 * @param string $value
108
	 *
109
	 * @return $this
110
	 *
111
	 * @since 2.0
112
	 */
113
	public function value($value)
114
	{
115
		$this->expression .= $value;
116
117
		return $this;
118
	}
119
120
	/**
121
	 * Starts a group capture
122
	 *
123
	 * @return $this
124
	 *
125
	 * @since 2.0
126
	 */
127
	public function startGroupCapture()
128
	{
129
		$this->expression .= '(';
130
		$this->openDelimiters++;
131
132
		return $this;
133
	}
134
135
	/**
136
	 * Ends a group capture
137
	 *
138
	 * @return $this
139
	 *
140
	 * @since 2.0
141
	 */
142
	public function endGroupCapture()
143
	{
144
		$this->value(')');
145
		$this->openDelimiters--;
146
147
		return $this;
148
	}
149
150
	/**
151
	 * Starts a range indicator
152
	 *
153
	 * @return $this
154
	 *
155
	 * @since 2.0
156
	 */
157
	public function startRange()
158
	{
159
		$this->value('[');
160
		$this->openDelimiters++;
161
162
		return $this;
163
	}
164
165
	/**
166
	 * Ends a range indicator
167
	 *
168
	 * @return $this
169
	 *
170
	 * @since 2.0
171
	 */
172
	public function endRange()
173
	{
174
		$this->value(']');
175
		$this->openDelimiters--;
176
177
		return $this;
178
	}
179
180
	/**
181
	 * Adds a a-z range
182
	 *
183
	 * @return $this
184
	 *
185
	 * @since 2.0
186
	 */
187
	public function lowercase()
188
	{
189
		$this->value('a-z');
190
191
		return $this;
192
	}
193
194
	/**
195
	 * Adds a A-Z range
196
	 *
197
	 * @return $this
198
	 *
199
	 * @since 2.0
200
	 */
201
	public function uppercase()
202
	{
203
		$this->value('A-Z');
204
205
		return $this;
206
	}
207
208
	/**
209
	 * Adds a 0-9 range
210
	 *
211
	 * @return $this
212
	 *
213
	 * @since 2.0
214
	 */
215
	public function numeric()
216
	{
217
		$this->value('0-9');
218
219
		return $this;
220
	}
221
222
	/**
223
	 * Adds an "any" matcher
224
	 *
225
	 * @return $this
226
	 *
227
	 * @since 2.0
228
	 */
229
	public function any()
230
	{
231
		$this->value('.');
232
233
		return $this;
234
	}
235
236
	/**
237
	 * Matches against the start of the string
238
	 *
239
	 * @return $this
240
	 *
241
	 * @since 2.0
242
	 */
243
	public function start()
244
	{
245
		$this->value('^');
246
247
		return $this;
248
	}
249
250
	/**
251
	 * Matches the end of the string
252
	 *
253
	 * @return $this
254
	 *
255
	 * @since 2.0
256
	 */
257
	public function end()
258
	{
259
		$this->value('$');
260
261
		return $this;
262
	}
263
264
	/**
265
	 * Matches none of one of the preceding statements
266
	 *
267
	 * @return $this
268
	 *
269
	 * @since 2.0
270
	 */
271
	public function noneOrOne()
272
	{
273
		$this->value('?');
274
275
		return $this;
276
	}
277
278
	/**
279
	 * Matches none or more of the preceding statement
280
	 *
281
	 * @return $this
282
	 *
283
	 * @since 2.0
284
	 */
285
	public function noneOrMany()
286
	{
287
		$this->value('*');
288
289
		return $this;
290
	}
291
292
	/**
293
	 * Matches one or more of the preceding statement
294
	 *
295
	 * @return $this
296
	 *
297
	 * @since 2.0
298
	 */
299
	public function oneOrMore()
300
	{
301
		$this->value('+');
302
303
		return $this;
304
	}
305
306
	/**
307
	 * Adds an or "|"
308
	 *
309
	 * @return $this
310
	 *
311
	 * @since 2.0
312
	 */
313
	public function addOr()
314
	{
315
		$this->value('|');
316
317
		return $this;
318
	}
319
320
	/**
321
	 * Adds a {} to match a quantity
322
	 *
323
	 * @param int      $min
324
	 * @param int|null $max
325
	 *
326
	 * @return $this
327
	 *
328
	 * @since 2.0
329
	 */
330
	public function matchQuantity($min, $max = null)
331
	{
332
		$match = '{'.$min;
333
334
		if ($max !== null)
335
		{
336
			$match .= ',' . $max;
337
		}
338
339
		$match .= '}';
340
341
		$this->value($match);
342
343
		return $this;
344
	}
345
346
	/**
347
	 * Makes the regex case insensitive by adding the i flag
348
	 *
349
	 * @return $this
350
	 *
351
	 * @since 2.0
352
	 */
353
	public function caseInsensitive()
354
	{
355
		$this->flags .= 'i';
356
357
		return $this;
358
	}
359
360
	/**
361
	 * Ignores whitespace in the regex by adding the x flag
362
	 *
363
	 * @return $this
364
	 *
365
	 * @since 2.0
366
	 */
367
	public function ignoreWhitespace()
368
	{
369
		$this->flags .= 'x';
370
371
		return $this;
372
	}
373
374
	/**
375
	 * Makes #{} substitutions happen only once by adding the o flag
376
	 *
377
	 * @return $this
378
	 *
379
	 * @since 2.0
380
	 */
381
	public function singleSubstitution()
382
	{
383
		$this->flags .= 'o';
384
385
		return $this;
386
	}
387
388
	/**
389
	 * Makes dot match newline characters using the m flag
390
	 *
391
	 * @return $this
392
	 *
393
	 * @since 2.0
394
	 */
395
	public function dotNewline()
396
	{
397
		$this->flags .= 'm';
398
399
		return $this;
400
	}
401
402
}
403