1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Greppy package. |
5
|
|
|
* |
6
|
|
|
* (c) Daniel Ribeiro <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Relax\Greppy; |
13
|
|
|
|
14
|
|
|
use Relax\Greppy\Pattern\Element\Metacharacter\Any; |
15
|
|
|
use Relax\Greppy\Pattern\Element\Metacharacter\Digit; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Pattern |
19
|
|
|
* |
20
|
|
|
* @author Daniel Ribeiro <[email protected]> |
21
|
|
|
* @package Greppy |
22
|
|
|
*/ |
23
|
|
|
final class FluentPattern implements Pattern |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
const DELIMITER_SLASH = "/"; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $subject; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $pattern; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function __toString() |
44
|
|
|
{ |
45
|
|
|
$pattern = sprintf("%s%s%s", self::DELIMITER_SLASH, $this->pattern, self::DELIMITER_SLASH); |
46
|
|
|
return $pattern; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return \Relax\Greppy\Pattern |
51
|
|
|
*/ |
52
|
|
|
public function any() |
53
|
|
|
{ |
54
|
|
|
$this->pattern .= new Any(); |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return \Relax\Greppy\Pattern |
60
|
|
|
*/ |
61
|
|
|
public function digit() |
62
|
|
|
{ |
63
|
|
|
$this->pattern .= new Digit(); |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return \Relax\Greppy\Pattern |
69
|
|
|
*/ |
70
|
|
|
public function literal() |
71
|
|
|
{ |
72
|
|
|
$characters = func_get_args(); |
73
|
|
|
$pattern = count($characters) > 1 ? "[%s]" : "%s"; |
74
|
|
|
|
75
|
|
|
$escapedCharacters = array(); |
76
|
|
|
|
77
|
|
|
foreach ($characters as $c) { |
78
|
|
|
$escapedCharacters[] = ctype_alnum($c) ? $c : sprintf("\%s", $c); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->pattern .= sprintf($pattern, implode("", $escapedCharacters)); |
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param int|string $from |
87
|
|
|
* @param int|string $to |
88
|
|
|
* @return \Relax\Greppy\Pattern |
89
|
|
|
*/ |
90
|
|
|
public function range($from, $to) |
91
|
|
|
{ |
92
|
|
|
$this->pattern .= sprintf("[%s-%s]", $from, $to); |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $character |
98
|
|
|
* @param int $min |
99
|
|
|
* @param int $max |
100
|
|
|
* @return \Relax\Greppy\Pattern |
101
|
|
|
*/ |
102
|
|
|
public function repetition($character, $min, $max = null) |
103
|
|
|
{ |
104
|
|
|
if (is_null($max)) { |
105
|
|
|
$this->pattern .= sprintf("%s{%s}", $character, $min); |
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->pattern .= sprintf("%s{%s,%s}", $character, $min, $max); |
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|