|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package s9e\RegexpBuilder |
|
5
|
|
|
* @copyright Copyright (c) 2016 The s9e Authors |
|
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace s9e\RegexpBuilder\Passes; |
|
9
|
|
|
|
|
10
|
|
|
abstract class AbstractPass implements PassInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var bool Whether the current set of strings is optional |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $isOptional; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* {@inheritdoc} |
|
19
|
|
|
*/ |
|
20
|
34 |
|
public function run(array $strings) |
|
21
|
|
|
{ |
|
22
|
34 |
|
$strings = $this->beforeRun($strings); |
|
23
|
34 |
|
if ($this->canRun($strings)) |
|
24
|
|
|
{ |
|
25
|
31 |
|
$strings = $this->runPass($strings); |
|
26
|
|
|
} |
|
27
|
34 |
|
$strings = $this->afterRun($strings); |
|
28
|
|
|
|
|
29
|
34 |
|
return $strings; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Process the list of strings after the pass is run |
|
34
|
|
|
* |
|
35
|
|
|
* @param array[] $strings |
|
36
|
|
|
* @return array[] |
|
37
|
|
|
*/ |
|
38
|
34 |
|
protected function afterRun(array $strings) |
|
39
|
|
|
{ |
|
40
|
34 |
|
if ($this->isOptional && $strings[0] !== []) |
|
41
|
|
|
{ |
|
42
|
5 |
|
array_unshift($strings, []); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
34 |
|
return $strings; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Prepare the list of strings before the pass is run |
|
50
|
|
|
* |
|
51
|
|
|
* @param array[] $strings |
|
52
|
|
|
* @return array[] |
|
53
|
|
|
*/ |
|
54
|
34 |
|
protected function beforeRun(array $strings) |
|
55
|
|
|
{ |
|
56
|
34 |
|
$this->isOptional = (isset($strings[0]) && $strings[0] === []); |
|
57
|
34 |
|
if ($this->isOptional) |
|
58
|
|
|
{ |
|
59
|
9 |
|
array_shift($strings); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
34 |
|
return $strings; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Test whether this pass can be run on a given list of strings |
|
67
|
|
|
* |
|
68
|
|
|
* @param array[] $strings |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
19 |
|
protected function canRun(array $strings) |
|
72
|
|
|
{ |
|
73
|
19 |
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Run this pass on a list of strings |
|
78
|
|
|
* |
|
79
|
|
|
* @param array[] $strings |
|
80
|
|
|
* @return array[] |
|
81
|
|
|
*/ |
|
82
|
|
|
abstract protected function runPass(array $strings); |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Test whether given string has an optional suffix |
|
86
|
|
|
* |
|
87
|
|
|
* @param array $string |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
7 |
|
protected function hasOptionalSuffix(array $string) |
|
91
|
|
|
{ |
|
92
|
7 |
|
$suffix = end($string); |
|
93
|
|
|
|
|
94
|
7 |
|
return (is_array($suffix) && $suffix[0] === []); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Test whether given string contains a single alternations made of single values |
|
99
|
|
|
* |
|
100
|
|
|
* @param array $string |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
7 |
|
protected function isCharacterClassString(array $string) |
|
104
|
|
|
{ |
|
105
|
7 |
|
return ($this->isSingleAlternationString($string) && $this->isSingleCharStringList($string[0])); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Test whether given string contains one single element that is an alternation |
|
110
|
|
|
* |
|
111
|
|
|
* @param array |
|
112
|
|
|
* @return bool |
|
113
|
|
|
*/ |
|
114
|
7 |
|
protected function isSingleAlternationString(array $string) |
|
115
|
|
|
{ |
|
116
|
7 |
|
return (count($string) === 1 && is_array($string[0])); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Test whether given string contains a single character value |
|
121
|
|
|
* |
|
122
|
|
|
* @param array $string |
|
123
|
|
|
* @return bool |
|
124
|
|
|
*/ |
|
125
|
5 |
|
protected function isSingleCharString(array $string) |
|
126
|
|
|
{ |
|
127
|
5 |
|
return (count($string) === 1 && !is_array($string[0])); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Test whether given list of strings contains nothing but single-char strings |
|
132
|
|
|
* |
|
133
|
|
|
* @param array[] $strings |
|
134
|
|
|
* @return bool |
|
135
|
|
|
*/ |
|
136
|
1 |
|
protected function isSingleCharStringList(array $strings) |
|
137
|
|
|
{ |
|
138
|
1 |
|
foreach ($strings as $string) |
|
139
|
|
|
{ |
|
140
|
1 |
|
if (!$this->isSingleCharString($string)) |
|
141
|
|
|
{ |
|
142
|
1 |
|
return false; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
1 |
|
return true; |
|
147
|
|
|
} |
|
148
|
|
|
} |