@@ -15,7 +15,7 @@ discard block |
||
15 | 15 | */ |
16 | 16 | function extract($pattern, $value = null) |
17 | 17 | { |
18 | - $function = function ($value) use ($pattern) { |
|
18 | + $function = function($value) use ($pattern) { |
|
19 | 19 | return (new Parser())->parse($pattern, $value); |
20 | 20 | }; |
21 | 21 | |
@@ -33,18 +33,17 @@ discard block |
||
33 | 33 | */ |
34 | 34 | function pmatch(array $patterns, $value = null) |
35 | 35 | { |
36 | - $function = function ($value) use ($patterns) { |
|
36 | + $function = function($value) use ($patterns) { |
|
37 | 37 | $parser = new Parser(); |
38 | 38 | |
39 | - foreach ($patterns as $pattern => $callback) { |
|
39 | + foreach($patterns as $pattern => $callback) { |
|
40 | 40 | $match = $parser->parse($pattern, $value); |
41 | 41 | |
42 | - if ($match !== false) { |
|
42 | + if($match !== false) { |
|
43 | 43 | try { |
44 | 44 | return is_callable($callback) ? |
45 | - call_user_func_array($callback, array_values($match)) : |
|
46 | - $callback; |
|
47 | - } catch (\Throwable $exp) { |
|
45 | + call_user_func_array($callback, array_values($match)) : $callback; |
|
46 | + } catch(\Throwable $exp) { |
|
48 | 47 | return $callback; |
49 | 48 | } |
50 | 49 | } |
@@ -70,11 +69,11 @@ discard block |
||
70 | 69 | */ |
71 | 70 | function func(array $patterns) |
72 | 71 | { |
73 | - $array_patterns = array_combine(array_map(function ($k) { |
|
74 | - return '[' . implode(', ', explode(' ', $k)) . ']'; |
|
72 | + $array_patterns = array_combine(array_map(function($k) { |
|
73 | + return '['.implode(', ', explode(' ', $k)).']'; |
|
75 | 74 | }, array_keys($patterns)), array_values($patterns)); |
76 | 75 | |
77 | - return function () use ($array_patterns) { |
|
76 | + return function() use ($array_patterns) { |
|
78 | 77 | return pmatch($array_patterns, func_get_args()); |
79 | 78 | }; |
80 | 79 | } |
@@ -105,20 +104,20 @@ discard block |
||
105 | 104 | { |
106 | 105 | $string = trim($string); |
107 | 106 | |
108 | - if (strlen($string) === 0) { |
|
107 | + if(strlen($string) === 0) { |
|
109 | 108 | return []; |
110 | 109 | } |
111 | 110 | |
112 | 111 | $results = []; |
113 | 112 | $buffer = ''; |
114 | 113 | $depth = 0; |
115 | - foreach (str_split($string) as $c) { |
|
116 | - if ($c === ' ') { |
|
114 | + foreach(str_split($string) as $c) { |
|
115 | + if($c === ' ') { |
|
117 | 116 | continue; |
118 | 117 | } |
119 | 118 | |
120 | - if ($c === $delimiter && $depth === 0) { |
|
121 | - if (strlen($buffer) === 0) { |
|
119 | + if($c === $delimiter && $depth === 0) { |
|
120 | + if(strlen($buffer) === 0) { |
|
122 | 121 | return false; |
123 | 122 | } |
124 | 123 | |
@@ -127,9 +126,9 @@ discard block |
||
127 | 126 | continue; |
128 | 127 | } |
129 | 128 | |
130 | - if ($c === $open) { |
|
129 | + if($c === $open) { |
|
131 | 130 | ++$depth; |
132 | - } elseif ($c === $close) { |
|
131 | + } elseif($c === $close) { |
|
133 | 132 | --$depth; |
134 | 133 | } |
135 | 134 |
@@ -47,8 +47,8 @@ discard block |
||
47 | 47 | { |
48 | 48 | $patterns = $this->_split(',', '[', ']', substr($pattern, 1, -1)); |
49 | 49 | |
50 | - if ($value instanceof \Countable || is_array($value)) { |
|
51 | - if (count($patterns) === 0) { |
|
50 | + if($value instanceof \Countable || is_array($value)) { |
|
51 | + if(count($patterns) === 0) { |
|
52 | 52 | return count($value) === 0 ? [] : false; |
53 | 53 | } |
54 | 54 | } |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | $patterns = $this->_split(':', '(', ')', substr($pattern, 1, -1)); |
62 | 62 | $last_pattern = array_pop($patterns); |
63 | 63 | |
64 | - if (! is_array($value)) { |
|
64 | + if(!is_array($value)) { |
|
65 | 65 | return false; |
66 | 66 | } |
67 | 67 | |
@@ -88,18 +88,18 @@ discard block |
||
88 | 88 | { |
89 | 89 | $pattern = trim($pattern); |
90 | 90 | |
91 | - if (is_numeric($pattern)) { |
|
91 | + if(is_numeric($pattern)) { |
|
92 | 92 | return $this->_parseNumericConstant($value, $pattern); |
93 | 93 | } |
94 | 94 | |
95 | 95 | // a true value will mean that no regex matched |
96 | 96 | // a false value will mean that at least one regex matched but the pattern didn't |
97 | 97 | // anything else is the result of the pattern matching |
98 | - $result = array_reduce(array_keys($this->rules), function ($current, $regex) use ($value, $pattern) { |
|
98 | + $result = array_reduce(array_keys($this->rules), function($current, $regex) use ($value, $pattern) { |
|
99 | 99 | return $this->_updateParsingResult($value, $pattern, $regex, $current); |
100 | 100 | }, true); |
101 | 101 | |
102 | - if ($result === true) { |
|
102 | + if($result === true) { |
|
103 | 103 | $this->_invalidPattern($pattern); |
104 | 104 | } |
105 | 105 | |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | |
109 | 109 | protected function _updateParsingResult($value, $pattern, $regex, $current) |
110 | 110 | { |
111 | - if (is_bool($current) && preg_match($regex, $pattern)) { |
|
111 | + if(is_bool($current) && preg_match($regex, $pattern)) { |
|
112 | 112 | $current = call_user_func_array([$this, $this->rules[$regex]], [$value, $pattern]); |
113 | 113 | } |
114 | 114 | |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | { |
120 | 120 | $result = split_enclosed($delimiter, $start, $stop, $pattern); |
121 | 121 | |
122 | - if ($result === false) { |
|
122 | + if($result === false) { |
|
123 | 123 | $this->_invalidPattern($pattern); |
124 | 124 | } |
125 | 125 | |
@@ -128,23 +128,23 @@ discard block |
||
128 | 128 | |
129 | 129 | protected function _recurse($value, $patterns) |
130 | 130 | { |
131 | - if (! is_array($value) || count($patterns) > count($value)) { |
|
131 | + if(!is_array($value) || count($patterns) > count($value)) { |
|
132 | 132 | return false; |
133 | 133 | } |
134 | 134 | |
135 | - return array_reduce($patterns, function ($results, $p) use (&$value) { |
|
135 | + return array_reduce($patterns, function($results, $p) use (&$value) { |
|
136 | 136 | return $this->_mergeResults($this->parse($p, array_shift($value)), $results); |
137 | 137 | }, []); |
138 | 138 | } |
139 | 139 | |
140 | 140 | protected function _mergeResults($new, $current) |
141 | 141 | { |
142 | - if ($new === false || $current === false) { |
|
142 | + if($new === false || $current === false) { |
|
143 | 143 | return false; |
144 | 144 | } |
145 | 145 | |
146 | 146 | $common = array_intersect_key($current, $new); |
147 | - if (count($common) > 0) { |
|
147 | + if(count($common) > 0) { |
|
148 | 148 | throw new \RuntimeException(sprintf('Non unique identifiers: "%s".', implode(', ', array_keys($common)))); |
149 | 149 | } |
150 | 150 |