1 | <?php |
||
13 | class Serializer |
||
14 | { |
||
15 | /** |
||
16 | * @var Escaper |
||
17 | */ |
||
18 | protected $escaper; |
||
19 | |||
20 | /** |
||
21 | * @var MetaCharacters |
||
22 | */ |
||
23 | protected $meta; |
||
24 | |||
25 | /** |
||
26 | * @var OutputInterface |
||
27 | */ |
||
28 | protected $output; |
||
29 | |||
30 | /** |
||
31 | * @param OutputInterface $output |
||
32 | * @parm MetaCharacters $meta |
||
33 | * @param Escaper $escaper |
||
34 | */ |
||
35 | 13 | public function __construct(OutputInterface $output, MetaCharacters $meta, Escaper $escaper) |
|
41 | |||
42 | /** |
||
43 | * Serialize given strings into a regular expression |
||
44 | * |
||
45 | * @param array[] $strings |
||
46 | * @return string |
||
47 | */ |
||
48 | 13 | public function serializeStrings(array $strings) |
|
66 | |||
67 | /** |
||
68 | * Analyze given strings to determine how to serialize them |
||
69 | * |
||
70 | * The returned array may contains any of the following elements: |
||
71 | * |
||
72 | * - (string) quantifier Either '' or '?' |
||
73 | * - (array) chars List of values from single-char strings |
||
74 | * - (array) strings List of multi-char strings |
||
75 | * |
||
76 | * @param array[] $strings |
||
77 | * @return array |
||
78 | */ |
||
79 | 13 | protected function analyzeStrings(array $strings) |
|
101 | |||
102 | /** |
||
103 | * Return the portion of strings that are composed of a single character |
||
104 | * |
||
105 | * @param array[] |
||
106 | * @return array String key => value |
||
107 | */ |
||
108 | 13 | protected function getChars(array $strings) |
|
121 | |||
122 | /** |
||
123 | * Get the list of ranges that cover all given values |
||
124 | * |
||
125 | * @param integer[] $values Ordered list of values |
||
126 | * @return array[] List of ranges in the form [start, end] |
||
127 | */ |
||
128 | 10 | protected function getRanges(array $values) |
|
151 | |||
152 | /** |
||
153 | * Test whether given string represents a single character |
||
154 | * |
||
155 | * @param array $string |
||
156 | * @return bool |
||
157 | */ |
||
158 | 13 | protected function isChar(array $string) |
|
162 | |||
163 | /** |
||
164 | * Test whether an expression is quantifiable based on the strings info |
||
165 | * |
||
166 | * @param array $info |
||
167 | * @return bool |
||
168 | */ |
||
169 | 3 | protected function isQuantifiable(array $info) |
|
175 | |||
176 | /** |
||
177 | * Test whether a list of strings contains only one single quantifiable string |
||
178 | * |
||
179 | * @param string[] $strings |
||
180 | * @return bool |
||
181 | */ |
||
182 | 1 | protected function isSingleQuantifiableString(array $strings) |
|
186 | |||
187 | /** |
||
188 | * Test whether an expression needs parentheses based on the strings info |
||
189 | * |
||
190 | * @param array $info |
||
191 | * @return bool |
||
192 | */ |
||
193 | 13 | protected function needsParentheses(array $info) |
|
197 | |||
198 | /** |
||
199 | * Serialize a given list of values into a character class |
||
200 | * |
||
201 | * @param integer[] $values |
||
202 | * @return string |
||
203 | */ |
||
204 | 10 | protected function serializeCharacterClass(array $values) |
|
223 | |||
224 | /** |
||
225 | * Serialize a given value to be used in a character class |
||
226 | * |
||
227 | * @param integer $value |
||
228 | * @return string |
||
229 | */ |
||
230 | 10 | protected function serializeCharacterClassUnit($value) |
|
234 | |||
235 | /** |
||
236 | * Serialize an element from a string |
||
237 | * |
||
238 | * @param array|integer $element |
||
239 | * @return string |
||
240 | */ |
||
241 | 5 | protected function serializeElement($element) |
|
245 | |||
246 | /** |
||
247 | * Serialize a given value to be used as a literal |
||
248 | * |
||
249 | * @param integer $value |
||
250 | * @return string |
||
251 | */ |
||
252 | 5 | protected function serializeLiteral($value) |
|
256 | |||
257 | /** |
||
258 | * Serialize a given string into a regular expression |
||
259 | * |
||
260 | * @param array $string |
||
261 | * @return string |
||
262 | */ |
||
263 | 5 | protected function serializeString(array $string) |
|
267 | |||
268 | /** |
||
269 | * Serialize a given value |
||
270 | * |
||
271 | * @param integer $value |
||
272 | * @param string $escapeMethod |
||
273 | * @return string |
||
274 | */ |
||
275 | 13 | protected function serializeValue($value, $escapeMethod) |
|
279 | } |