-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPattern.php
More file actions
150 lines (124 loc) Β· 4.17 KB
/
Pattern.php
File metadata and controls
150 lines (124 loc) Β· 4.17 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
<?php
namespace Vector\Control;
use ReflectionClass;
use ReflectionFunction;
use ReflectionParameter;
use Vector\Core\Exception\IncompletePatternMatchException;
use Vector\Core\Module;
use Vector\Data\Maybe\Nothing;
use Vector\Lib\Arrays;
use Vector\Core\Curry;
abstract class Pattern
{
use Module;
/**
* Get type OR class for params, as well as re-mapping inconsistencies
* @param $param
* @return string
*/
private static function getType($param)
{
$type = is_object($param)
? get_class($param)
: gettype($param);
return $type === 'integer' ? 'int' : $type;
}
/**
* Pattern Matching. Use switch-case for explicit values, this for everything else.
* @param array $patterns
* @return mixed
*/
#[Curry]
protected static function match(array $patterns)
{
return function (...$args) use ($patterns) {
$parameterTypes = array_map(fn($arg) => self::getType($arg), $args);
$keysToValues = Arrays::zip(array_keys($patterns), array_values($patterns));
$val = Arrays::first(
fn ($pattern) => self::patternApplies($parameterTypes, $args, $pattern),
$keysToValues
);
// Can't use Pattern::match here because we lack tail call optimization.
if (get_class($val) === Nothing::class) {
throw new IncompletePatternMatchException(
'Incomplete pattern match expression. (missing ' . implode(', ', $parameterTypes) . ')'
);
}
$matchingPattern = $val->extract()[1];
[$hasExtractable, $unwrappedArgs] = self::unwrapArgs($args);
$value = $matchingPattern(...$args);
$isCallable = is_callable($value);
/**
* Extractable requires a callback to feed args into.
*/
if ($hasExtractable && $isCallable) {
return $matchingPattern(...$args)(...$unwrappedArgs);
}
/**
* No extractable or callable so we can just return the value directly.
*/
return $value;
};
}
/**
* Extracts args from Extractable values
* @param array $args
* @return array
*/
protected static function unwrapArgs(array $args) : array
{
$hasExtractable = false;
$unwrappedArgs = self::flatten(array_map(function ($arg) use (&$hasExtractable) {
if (is_object($arg) && method_exists($arg, 'extract')) {
$hasExtractable = true;
return $arg->extract();
}
return $arg;
}, $args));
return [$hasExtractable, $unwrappedArgs];
}
/**
* @param array $parameterTypes
* @param array $args
* @param array $pattern
* @return bool
* @throws \ReflectionException
*/
private static function patternApplies(array $parameterTypes, array $args, array $pattern) : bool
{
[$_, $pattern] = $pattern;
$reflected = new ReflectionFunction($pattern);
$patternParameterTypes = array_map(function (ReflectionParameter $parameter) {
$class = $parameter->getType() && ! $parameter->getType()->isBuiltin()
? new ReflectionClass($parameter->getType()->getName())
: null;
if ($class) {
return $class->getName();
}
return (string) ($parameter->getType() ? $parameter->getType()->getName() : null);
}, $reflected->getParameters());
/**
* Check count/type of params
*/
return count($patternParameterTypes) === 0
|| (
count($parameterTypes) === count($patternParameterTypes)
&& $parameterTypes === $patternParameterTypes
);
}
/**
* @param array $array
* @return array
*/
private static function flatten(array $array) : array
{
$values = [];
array_walk_recursive(
$array,
function ($value) use (&$values) {
$values[] = $value;
}
);
return $values;
}
}