1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FunctionalPHP\PatternMatching; |
4
|
|
|
|
5
|
|
|
class Parser |
6
|
|
|
{ |
7
|
|
|
protected $reserved = ['true', 'false']; |
8
|
|
|
|
9
|
|
|
protected $rules = [ |
10
|
|
|
'/^(true|false)$/i' => '_parseBooleanConstant', |
11
|
|
|
'/^([\'"])(?:(?!\\1).)*\\1$/' => '_parseStringConstant', |
12
|
|
|
'/^[a-zA-Z]+$/' => '_parseIdentifier', |
13
|
|
|
'/^_$/' => '_parseWildcard', |
14
|
|
|
'/^\\[.*\\]$/' => '_parseArray', |
15
|
|
|
'/^\\(.+:.+\\)$/' => '_parseCons', |
16
|
|
|
'/^[a-zA-Z]+@.+$/' => '_parseAs', |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
protected function _parseBooleanConstant($value, $pattern) |
20
|
|
|
{ |
21
|
1 |
|
$pattern_value = strtoupper($pattern) === 'TRUE' ? true : false; |
22
|
1 |
|
return is_bool($value) && $value === $pattern_value ? [] : false; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function _parseStringConstant($value, $pattern) |
26
|
|
|
{ |
27
|
1 |
|
$string_pattern = substr($pattern, 1, -1); |
28
|
1 |
|
return is_string($value) && $string_pattern == $value ? [] : false; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
protected function _parseIdentifier($value, $pattern) |
32
|
|
|
{ |
33
|
1 |
|
return in_array(strtolower($pattern), $this->reserved) ? false : [$pattern => $value]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function _parseWildcard() |
37
|
|
|
{ |
38
|
1 |
|
return []; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function _parseArray($value, $pattern) |
42
|
|
|
{ |
43
|
1 |
|
$patterns = $this->_split(',', '[', ']', substr($pattern, 1, -1)); |
44
|
|
|
|
45
|
1 |
|
if(count($patterns) === 0) { |
46
|
1 |
|
return count($value) === 0 ? [] : false; |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
return $this->_recurse($value, $patterns); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function _parseCons($value, $pattern) |
53
|
|
|
{ |
54
|
1 |
|
$patterns = $this->_split(':', '(', ')', substr($pattern, 1, -1)); |
55
|
1 |
|
$last_pattern = array_pop($patterns); |
56
|
|
|
|
57
|
1 |
|
if(! is_array($value)) { |
58
|
1 |
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
return $this->_mergeResults( |
62
|
1 |
|
$this->parse(array_splice($value, count($patterns)), $last_pattern), |
63
|
1 |
|
$this->_recurse($value, $patterns) |
64
|
1 |
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function _parseAs($value, $pattern) |
68
|
|
|
{ |
69
|
1 |
|
$patterns = explode('@', $pattern, 2); |
70
|
|
|
|
71
|
1 |
|
$rest = $this->parse($value, $patterns[1]); |
72
|
1 |
|
return $this->_mergeResults([$patterns[0] => $value], $rest); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param mixed $value |
77
|
|
|
* @param string $pattern |
78
|
|
|
* @return bool|array |
79
|
|
|
*/ |
80
|
|
|
public function parse($value, $pattern) |
81
|
|
|
{ |
82
|
1 |
|
$pattern = trim($pattern); |
83
|
|
|
|
84
|
1 |
|
if(is_numeric($pattern)) { |
85
|
1 |
|
return is_numeric($value) && $pattern == $value ? [] : false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// a true value will mean that no regex matched |
89
|
|
|
// a false value will mean that at least one regex matched but the pattern didn't |
90
|
|
|
// anything else is the result of the pattern matching |
91
|
1 |
|
$result = array_reduce(array_keys($this->rules), function($result, $regex) use($value, $pattern) { |
92
|
1 |
|
if(is_bool($result) && preg_match($regex, $pattern)) { |
93
|
1 |
|
$result = call_user_func_array([$this, $this->rules[$regex]], [$value, $pattern]); |
|
|
|
|
94
|
1 |
|
} |
95
|
|
|
|
96
|
1 |
|
return $result; |
97
|
1 |
|
}, true); |
98
|
|
|
|
99
|
1 |
|
if($result === true) { |
100
|
1 |
|
$this->_invalidPattern($pattern); |
101
|
|
|
} |
102
|
|
|
|
103
|
1 |
|
return $result; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function _split($delimiter, $start, $stop, $pattern) |
|
|
|
|
107
|
|
|
{ |
108
|
1 |
|
$result = split_enclosed($delimiter, $start, $stop, $pattern); |
109
|
|
|
|
110
|
1 |
|
if($result === false) { |
111
|
1 |
|
$this->_invalidPattern($pattern); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
return $result; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function _recurse($value, $patterns) |
118
|
|
|
{ |
119
|
1 |
|
if(! is_array($value) || count($patterns) > count($value)) { |
120
|
1 |
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
return array_reduce($patterns, function($results, $p) use(&$value) { |
124
|
1 |
|
return $this->_mergeResults($results, $this->parse(array_shift($value), $p)); |
125
|
1 |
|
}, []); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
protected function _mergeResults($new, $current) |
129
|
|
|
{ |
130
|
1 |
|
return $new === false || $current === false ? |
131
|
1 |
|
false : |
132
|
1 |
|
array_merge($current, $new); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
protected function _invalidPattern($pattern) |
136
|
|
|
{ |
137
|
1 |
|
throw new \RuntimeException(sprintf('Invalid pattern "%s".', $pattern)); |
138
|
|
|
} |
139
|
|
|
} |