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