1 | <?php |
||
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) |
||
24 | |||
25 | protected function _parseStringConstant($value, $pattern) |
||
30 | |||
31 | protected function _parseIdentifier($value, $pattern) |
||
35 | |||
36 | protected function _parseWildcard() |
||
40 | |||
41 | protected function _parseArray($value, $pattern) |
||
72 | |||
73 | protected function _parseCons($value, $pattern) |
||
101 | |||
102 | protected function _parseAs($value, $pattern) |
||
109 | |||
110 | /** |
||
111 | * @param mixed $value |
||
112 | * @param string $pattern |
||
113 | * @return bool|array |
||
114 | */ |
||
115 | public function parse($value, $pattern) |
||
116 | { |
||
117 | $pattern = trim($pattern); |
||
118 | |||
119 | if(is_numeric($pattern) && is_numeric($value)) { |
||
120 | return $pattern == $value ? [] : false; |
||
121 | } |
||
122 | |||
123 | $matched = false; |
||
124 | foreach($this->rules as $regex => $method) { |
||
125 | if(preg_match($regex, $pattern)) { |
||
126 | $matched = true; |
||
127 | |||
128 | $arguments = call_user_func_array([$this, $method], [$value, $pattern]); |
||
129 | |||
130 | if($arguments !== false) { |
||
131 | return $arguments; |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | |||
136 | if(! $matched) { |
||
147 | } |