-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMapMatcher.php
More file actions
350 lines (299 loc) · 8.26 KB
/
MapMatcher.php
File metadata and controls
350 lines (299 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/*
* This file is part of UniversalMatcher.
*
* (c) 2014 Nicolò Martini
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace UniversalMatcher;
/**
* Class MapMatcher
*/
class MapMatcher implements Matcher
{
/**
* @var array[PrioritizedMap]
*/
private $maps;
/**
* The set of maps that have at least one rule attached
* @var array[PrioritizedMap]
*/
private $activeMaps;
/**
* Tells if the $activeMaps array is sorted by priority
* @var bool
*/
private $areMapsSorted = true;
/**
* @var array[array]
*/
private $rules;
/**
* @var mixed
*/
private $default = null;
/**
* @var static
*/
private $parent = null;
/**
* @param null $defaultValue
*/
public function __construct($defaultValue = null)
{
$this->setDefault($defaultValue);
}
/**
* {@inheritdoc}
*/
public function getDefault()
{
return $this->default;
}
/**
* @param callable|mixed $value
* @return $this
*/
public function setDefault($value)
{
$this->default = is_callable($value) ? $value : $this->func($value);
return $this;
}
/**
* Register a map
*
* @param string $name
* @param callable $map
* @param int $priority
* @throws InvalidMatcherException
* @return $this
*/
public function defineMap($name, callable $map, $priority = 0)
{
$this->areMapsSorted = false;
$this->maps[$name] = new PrioritizedMap($map, $priority, count($this->maps));
if (!isset($this->rules[$name]))
$this->rules[$name] = [];
return $this;
}
/**
* @param string $mapName
*
* @return mixed
*
* @throws \OutOfBoundsException
*/
public function getMap($mapName)
{
if (isset($this->maps[$mapName]))
return $this->maps[$mapName];
throw new \OutOfBoundsException("There is no map registered with the name \"$mapName\"");
}
/**
* @param string $mapName
*
* @return mixed
*
* @throws \OutOfBoundsException
*/
public function priority($mapName)
{
if (isset($this->maps[$mapName]))
return $this->maps[$mapName]->priority;
throw new \OutOfBoundsException("There is no map registered with the name \"$mapName\"");
}
/**
* @param string|callable $map The name of a registered map or a callable
* @param mixed $expected The expected matching value
* @param mixed $value The value that will be returned on match, or a callable that will be called
* on match, passing the matching value as parameter
* @return $this The current instance
*/
public function rule($map, $expected, $value = null)
{
if (is_callable($map)) {
if (is_string($map)) {
if (!isset($this->maps[$map]))
return $this->callbackRule($map, $expected, $value);
} else {
return $this->callbackRule($map, $expected, $value);
}
}
if (isset($value)) {
$valueCallback = is_callable($value) ? $value : $this->func($value);
} else {
$valueCallback = $this->linkedMatcher();
$valueCallback->parent = $this;
}
$this->rules[$map][$this->serializeExpected($expected)] = $valueCallback;
$this->activateMap($map);
return isset($value) ? $this : $valueCallback;
}
/**
* Create a rule providing directly a callable
*
* @param callable $callback
* @param mixed $expected
* @param mixed $value
* @param int $priority
*
* @return $this
*/
public function callbackRule(callable $callback, $expected, $value, $priority = 0)
{
$key = is_string($callback) ? $callback : $this->getFreeKey();
$this
->defineMap($key, $callback, $priority)
->rule($key, $expected, $value)
;
return $this;
}
/**
* {@inheritdoc}
*/
public function match($value)
{
// Sort maps by descending priority if necessary
if (!$this->areMapsSorted) {
$this->sortMaps();
$this->areMapsSorted = true;
}
foreach ($this->activeMaps as $name => $prioritizedMap) {
$map = $prioritizedMap->map;
$matchingValue = $this->serializeExpected(call_user_func($map, $value));
if (isset($this->rules[$name][$matchingValue])) {
$return = $this->rules[$name][$matchingValue]($value);
if (!$return instanceof None)
return $return;
}
}
$def = $this->getDefault();
return $def($value);
}
/**
* {@inheritdoc}
*/
public function matchAll($value)
{
$matches = [];
// Sort maps by descending priority if necessary
if (!$this->areMapsSorted) {
$this->sortMaps();
$this->areMapsSorted = true;
}
foreach ($this->activeMaps as $name => $prioritizedMap) {
$map = $prioritizedMap->map;
$matchingValue = $this->serializeExpected(call_user_func($map, $value));
if (isset($this->rules[$name][$matchingValue])) {
$return = $this->rules[$name][$matchingValue]($value);
if (!$return instanceof None)
$matches[] = $return;
}
}
return $matches;
}
/**
* Retrieve a the return value linked to a map value
*
* @param string $mapName
* @param mixed $matchingValue
* @param mixed $fakeValue
* @return mixed
*/
public function matchByMapValue($mapName, $matchingValue, $fakeValue = null)
{
if (isset($this->rules[$mapName][$matchingValue]))
return $this->rules[$mapName][$matchingValue]($fakeValue);
$defaultCallback = $this->getDefault();
return $defaultCallback($fakeValue);
}
/**
* @return static
*
* @throws \UnderflowException
*/
public function end()
{
if (isset($this->parent))
return $this->parent;
throw new \UnderflowException("We are already at the end of the stack");
}
/**
* Render an MapMatcher a callable => Can be nested in other engines!
* @param mixed $value
* @return mixed
*/
public function __invoke($value)
{
return $this->match($value);
}
/**
* Wrap a value with a callable that returns that value
*
* @param mixed $value
*
* @return callable
*/
public function func($value)
{
return function() use ($value) { return $value; };
}
/**
* Returns a matcher that has the exactly same set of maps,
* but different set of rules.
*
* @return MapMatcher
*/
public function linkedMatcher()
{
/** @var MapMatcher $matcher */
$matcher = new static(new None);
$matcher->maps =& $this->maps;
return $matcher;
}
/**
* Generate a defineMap key not already taken
* @return int
*/
private function getFreeKey()
{
$index = count($this->maps);
while (isset($this->maps[$index]))
$index++;
return $index;
}
/**
* @param $value
* @return string
*/
private function serializeExpected($value)
{
if (is_scalar($value))
return $value;
return serialize($value);
}
/**
* Sort maps by priority. On equal prioriy, first inserted wins
*/
private function sortMaps()
{
uasort($this->activeMaps, function(PrioritizedMap $m1, PrioritizedMap $m2) {
if ($p = $m2->priority - $m1->priority)
return $p;
return $m1->priority2 - $m2->priority2;
});
}
/**
* Put a map to the activate map array
*
* @param string $mapName
*/
private function activateMap($mapName)
{
if (!isset($this->activeMaps[$mapName]))
$this->activeMaps[$mapName] = $this->maps[$mapName];
}
}