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 function to split a string using a given delimiter except |
49
|
|
|
* if said delimiter is enclosed between two different characters. |
50
|
|
|
* |
51
|
|
|
* This won't work if the opening and closing character for the |
52
|
|
|
* enclosure is the same (ie quotes), $open and $close need to |
53
|
|
|
* be different. |
54
|
|
|
* |
55
|
|
|
* The enclosing can have multiple depth. Each opening character needs |
56
|
|
|
* to be closed by exactly one closing character. No balancing is done. |
57
|
|
|
* |
58
|
|
|
* @param string $delimiter one character that will be the delimiter |
59
|
|
|
* @param string $open one character that starts the enclosing |
60
|
|
|
* @param string $close one character that stops the enclosing |
61
|
|
|
* @param string $string the string to split |
62
|
|
|
* @return array|string[] The split result. |
63
|
|
|
*/ |
64
|
|
|
function split_enclosed($delimiter, $open, $close, $string) |
65
|
|
|
{ |
66
|
|
|
$chars = str_split($string); |
67
|
|
|
|
68
|
|
|
$result = []; |
69
|
|
|
$buffer = ''; |
70
|
|
|
$level = 0; |
71
|
|
|
foreach($chars as $c) { |
72
|
|
|
if($c === $delimiter && $level === 0) { |
73
|
|
|
$result[] = $buffer; |
74
|
|
|
$buffer = ''; |
75
|
|
|
} else { |
76
|
|
|
$buffer .= $c; |
77
|
|
|
|
78
|
|
|
if($c === $open) { |
79
|
|
|
++$level; |
80
|
|
|
} else if($c === $close) { |
81
|
|
|
--$level; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if(strlen($buffer) > 0) { |
87
|
|
|
$result[] = $buffer; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $result; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
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.