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