1 | <?php |
||
13 | class MetaCharacters |
||
14 | { |
||
15 | /** |
||
16 | * @const Bit value that indicates whether a meta-character represents a single character |
||
17 | */ |
||
18 | const IS_CHAR = 1; |
||
19 | |||
20 | /** |
||
21 | * @const Bit value that indicates whether a meta-character represents a quantifiable expression |
||
22 | */ |
||
23 | const IS_QUANTIFIABLE = 2; |
||
24 | |||
25 | /** |
||
26 | * @var array Map of meta values and the expression they represent |
||
27 | */ |
||
28 | protected $exprs = []; |
||
29 | |||
30 | /** |
||
31 | * @var InputInterface |
||
32 | */ |
||
33 | protected $input; |
||
34 | |||
35 | /** |
||
36 | * @var array Map of meta-characters' codepoints and their value |
||
37 | */ |
||
38 | protected $meta = []; |
||
39 | |||
40 | /** |
||
41 | * @param InputInterface $input |
||
42 | */ |
||
43 | 13 | public function __construct(InputInterface $input) |
|
47 | |||
48 | /** |
||
49 | * Add a meta-character to the list |
||
50 | * |
||
51 | * @param string $char Meta-character |
||
52 | * @param string $expr Regular expression |
||
53 | * @return void |
||
54 | */ |
||
55 | 12 | public function add($char, $expr) |
|
73 | |||
74 | /** |
||
75 | * Get the expression associated with a meta value |
||
76 | * |
||
77 | * @param integer $metaValue |
||
78 | * @return string |
||
79 | */ |
||
80 | 2 | public function getExpression($metaValue) |
|
89 | |||
90 | /** |
||
91 | * Return whether a given value represents a single character |
||
92 | * |
||
93 | * @param integer $value |
||
94 | * @return bool |
||
95 | */ |
||
96 | 9 | public static function isChar($value) |
|
100 | |||
101 | /** |
||
102 | * Return whether a given value represents a quantifiable expression |
||
103 | * |
||
104 | * @param integer $value |
||
105 | * @return bool |
||
106 | */ |
||
107 | 9 | public static function isQuantifiable($value) |
|
111 | |||
112 | /** |
||
113 | * Replace values from meta-characters in a list of strings with their meta value |
||
114 | * |
||
115 | * @param array[] $strings |
||
116 | * @return array[] |
||
117 | */ |
||
118 | 10 | public function replaceMeta(array $strings) |
|
133 | |||
134 | /** |
||
135 | * Compute and return a value for given expression |
||
136 | * |
||
137 | * Values are meant to be a unique negative integer. The last 2 bits indicate whether the |
||
138 | * expression is quantifiable and/or represents a single character. |
||
139 | * |
||
140 | * @param string $expr Regular expression |
||
141 | * @return integer |
||
142 | */ |
||
143 | 10 | protected function computeValue($expr) |
|
157 | |||
158 | /** |
||
159 | * Test whether given expression represents a single character usable in a character class |
||
160 | * |
||
161 | * @param string $expr |
||
162 | * @return bool |
||
163 | */ |
||
164 | 10 | protected function exprIsChar($expr) |
|
192 | |||
193 | /** |
||
194 | * Test whether given expression is quantifiable |
||
195 | * |
||
196 | * @param string $expr |
||
197 | * @return bool |
||
198 | */ |
||
199 | 10 | protected function exprIsQuantifiable($expr) |
|
203 | } |