1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FunctionalPHP\PatternMatching; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Destructure the given value using the given pattern, then returns |
7
|
|
|
* the resulting values as an array indexed using the identifiers of |
8
|
|
|
* the pattern. |
9
|
|
|
* |
10
|
|
|
* If the extraction failed, will return False. |
11
|
|
|
* |
12
|
|
|
* @param string $pattern |
13
|
|
|
* @param mixed $value |
14
|
|
|
* @return array|bool|callable |
|
|
|
|
15
|
|
|
*/ |
16
|
|
|
function extract($pattern, $value = null) |
17
|
|
|
{ |
18
|
|
|
$function = function($value) use($pattern) { |
19
|
|
|
return (new Parser())->parse($pattern, $value); |
20
|
|
|
}; |
21
|
|
|
|
22
|
|
|
return func_num_args() > 1 ? $function($value) : $function; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Given a value and an array with the format <pattern> => <callback>, |
27
|
|
|
* matches the value to the first pattern possible and execute the |
28
|
|
|
* callback by passing the arguments destructured from the value. |
29
|
|
|
* |
30
|
|
|
* @param array $patterns <pattern> => <callback> |
31
|
|
|
* @param mixed $value |
32
|
|
|
* @return array|mixed|callable |
33
|
|
|
*/ |
34
|
|
|
function match(array $patterns, $value = null) |
35
|
|
|
{ |
36
|
|
|
$function = function($value) use($patterns) { |
37
|
|
|
$parser = new Parser(); |
38
|
|
|
|
39
|
|
|
foreach($patterns as $pattern => $callback) { |
40
|
|
|
$match = $parser->parse($pattern, $value); |
41
|
|
|
|
42
|
|
|
if($match !== false) { |
43
|
|
|
return is_callable($callback) ? |
44
|
|
|
call_user_func_array($callback, $match) : |
45
|
|
|
$callback; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
throw new \RuntimeException('Non-exhaustive patterns.'); |
50
|
|
|
}; |
51
|
|
|
|
52
|
|
|
return func_num_args() > 1 ? $function($value) : $function; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Helper to create a function for which the value depends on pattern matching. |
57
|
|
|
* |
58
|
|
|
* You pass the various parameters your function except separated |
59
|
|
|
* by spaces, the helper will automatically transform this to accept |
60
|
|
|
* an array as parameter by replacing all spaces with commas. |
61
|
|
|
* |
62
|
|
|
* This means you cannot use any space in your patterns. |
63
|
|
|
* |
64
|
|
|
* @param array $patterns |
65
|
|
|
* @return \Closure|callable |
66
|
|
|
*/ |
67
|
|
|
function func(array $patterns) |
68
|
|
|
{ |
69
|
|
|
$array_patterns = array_combine(array_map(function($k) { |
70
|
|
|
return '['.implode(', ', explode(' ', $k)).']'; |
71
|
|
|
}, array_keys($patterns)), array_values($patterns)); |
72
|
|
|
|
73
|
|
|
return function() use($array_patterns) { |
74
|
|
|
return match($array_patterns, func_get_args()); |
75
|
|
|
}; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Helper function to split a string using a given delimiter except |
80
|
|
|
* if said delimiter is enclosed between two different characters. |
81
|
|
|
* |
82
|
|
|
* The given string will be trimmed as will all values in the |
83
|
|
|
* resulting array. An empty string will result in an empty |
84
|
|
|
* array. If at any time a string that should be added to the |
85
|
|
|
* result is empty, the function will return false instead. |
86
|
|
|
* |
87
|
|
|
* This won't work if the opening and closing character for the |
88
|
|
|
* enclosure is the same (ie quotes), $open and $close need to |
89
|
|
|
* be different. |
90
|
|
|
* |
91
|
|
|
* The enclosing can have multiple depth. Each opening character needs |
92
|
|
|
* to be closed by exactly one closing character. No balancing is done. |
93
|
|
|
* |
94
|
|
|
* @param string $delimiter one character that will be the delimiter |
95
|
|
|
* @param string $open one character that starts the enclosing |
96
|
|
|
* @param string $close one character that stops the enclosing |
97
|
|
|
* @param string $string the string to split |
98
|
|
|
* @return array|string[]|bool The split result, false if any of the value was empty |
99
|
|
|
*/ |
100
|
|
|
function split_enclosed($delimiter, $open, $close, $string) |
101
|
|
|
{ |
102
|
|
|
$string = trim($string); |
103
|
|
|
|
104
|
|
|
if(strlen($string) === 0) { |
105
|
|
|
return []; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$results = []; |
109
|
|
|
$buffer = ''; |
110
|
|
|
$depth = 0; |
111
|
|
|
foreach(str_split($string) as $c) { |
112
|
|
|
if($c === ' ') { |
113
|
|
|
continue; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if($c === $delimiter && $depth === 0) { |
117
|
|
|
if(strlen($buffer) === 0) { |
118
|
|
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$results[] = $buffer; |
122
|
|
|
$buffer = ''; |
123
|
|
|
continue; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if($c === $open) { |
127
|
|
|
++$depth; |
128
|
|
|
} else if($c === $close) { |
129
|
|
|
--$depth; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$buffer .= $c; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return strlen($buffer) === 0 ? false : array_merge($results, [$buffer]); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.