1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PHPFunctional\PatternMatching; |
4
|
|
|
|
5
|
|
|
class Matcher |
6
|
|
|
{ |
7
|
|
|
private static $rules = [ |
8
|
|
|
"/^(true|false)$/i" => '_parseBooleanConstant', |
|
|
|
|
9
|
|
|
"/^(['\"])(?:(?!\\1).)*\\1$/" => '_parseStringConstant', |
10
|
|
|
"/^[a-zA-Z]+$/" => '_parseIdentifier', |
|
|
|
|
11
|
|
|
"/^_$/" => '_parseWildcard', |
|
|
|
|
12
|
|
|
"/^\\[.*\\]$/" => '_parseArray', |
|
|
|
|
13
|
|
|
"/^\\([^:]+:.+\\)$/" => '_parseCons', |
|
|
|
|
14
|
|
|
]; |
15
|
|
|
|
16
|
|
|
private static function _parseBooleanConstant($value, $pattern) |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
return is_bool($value) ? [] : false; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
private static function _parseStringConstant($value, $pattern) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$string_pattern = substr($pattern, 1, -1); |
24
|
|
|
return is_string($value) && $string_pattern == $value ? [] : false; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
private static function _parseIdentifier($value, $pattern) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
return [$value]; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
private static function _parseWildcard($value, $pattern) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
return []; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private static function _parseArray($value, $pattern) |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
if(! is_array($value)) { |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$patterns = array_filter(array_map('trim', split_enclosed(',', '[', ']', substr($pattern, 1, -1)))); |
44
|
|
|
|
45
|
|
|
if(count($patterns) === 0) { |
46
|
|
|
return count($value) === 0 ? [] : false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if(count($patterns) > count($value)) { |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$index = 0; |
54
|
|
|
$results = []; |
55
|
|
|
foreach($value as $v) { |
56
|
|
|
$new = self::parse($v, $patterns[$index]); |
57
|
|
|
|
58
|
|
|
if($new === false) { |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$results = array_merge($results, $new); |
63
|
|
|
++$index; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $results; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private static function _parseCons($value, $pattern) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
if(! is_array($value)) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$patterns = array_filter(array_map('trim', split_enclosed(':', '(', ')', substr($pattern, 1, -1)))); |
76
|
|
|
$last = array_pop($patterns); |
77
|
|
|
|
78
|
|
|
$results = []; |
79
|
|
|
foreach($patterns as $p) { |
80
|
|
|
if(count($value) == 0) { |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$new = self::parse(array_shift($value), $p); |
85
|
|
|
|
86
|
|
|
if($new === false) { |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$results = array_merge($results, $new); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$new = self::parse($value, $last); |
94
|
|
|
|
95
|
|
|
return $new === false ? false : array_merge($results, $new); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param mixed $value |
100
|
|
|
* @param string $pattern |
101
|
|
|
* @return bool|array |
102
|
|
|
*/ |
103
|
|
|
private static function parse($value, $pattern) |
104
|
|
|
{ |
105
|
|
|
$pattern = trim($pattern); |
|
|
|
|
106
|
|
|
|
107
|
|
|
if(is_numeric($pattern) && is_numeric($value)) { |
108
|
|
|
return []; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
foreach(self::$rules as $regex => $method) { |
112
|
|
|
if(preg_match($regex, $pattern)) { |
113
|
|
|
$arguments = call_user_func_array(['static', $method], [$value, $pattern]); |
114
|
|
|
|
115
|
|
|
if($arguments !== false) { |
116
|
|
|
return $arguments; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return false; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param mixed $value |
126
|
|
|
* @param array $patterns |
127
|
|
|
* @return mixed |
128
|
|
|
*/ |
129
|
|
|
public static function match($value, array $patterns) |
130
|
|
|
{ |
131
|
|
|
foreach($patterns as $pattern => $callback) { |
132
|
|
|
$match = self::parse($value, $pattern); |
133
|
|
|
|
134
|
|
|
if($match !== false) { |
135
|
|
|
return call_user_func_array($callback, $match); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
throw new \RuntimeException("Non-exhaustive patterns."); |
|
|
|
|
140
|
|
|
} |
141
|
|
|
} |
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.