@@ -70,18 +70,18 @@ discard block |
||
70 | 70 | protected function buildMap(ReflectionEntity $filter): array |
71 | 71 | { |
72 | 72 | $schema = $filter->getProperty('schema', true); |
73 | - if (empty($schema)) { |
|
73 | + if (empty($schema)){ |
|
74 | 74 | throw new SchemaException("Filter `{$filter->getName()}` does not define any schema"); |
75 | 75 | } |
76 | 76 | |
77 | 77 | $result = []; |
78 | - foreach ($schema as $field => $definition) { |
|
78 | + foreach ($schema as $field => $definition){ |
|
79 | 79 | $optional = false; |
80 | 80 | |
81 | 81 | // short definition |
82 | - if (is_string($definition)) { |
|
82 | + if (is_string($definition)){ |
|
83 | 83 | // simple scalar field definition |
84 | - if (!class_exists($definition)) { |
|
84 | + if (!class_exists($definition)){ |
|
85 | 85 | [$source, $origin] = $this->parseDefinition($field, $definition); |
86 | 86 | $result[$field] = [ |
87 | 87 | FilterProvider::SOURCE => $source, |
@@ -102,25 +102,25 @@ discard block |
||
102 | 102 | continue; |
103 | 103 | } |
104 | 104 | |
105 | - if (!is_array($definition) || count($definition) === 0) { |
|
105 | + if (!is_array($definition) || count($definition) === 0){ |
|
106 | 106 | throw new SchemaException( |
107 | 107 | "Invalid schema definition at `{$filter->getName()}`->`{$field}`" |
108 | 108 | ); |
109 | 109 | } |
110 | 110 | |
111 | 111 | // complex definition |
112 | - if (!empty($definition[self::ORIGIN])) { |
|
112 | + if (!empty($definition[self::ORIGIN])){ |
|
113 | 113 | $origin = $definition[self::ORIGIN]; |
114 | 114 | |
115 | 115 | // [class, 'data:something.*'] vs [class, 'data:something'] |
116 | 116 | $iterate = strpos($origin, '.*') !== false || !empty($definition[self::ITERATE]); |
117 | 117 | $origin = rtrim($origin, '.*'); |
118 | - } else { |
|
118 | + }else{ |
|
119 | 119 | $origin = $field; |
120 | 120 | $iterate = true; |
121 | 121 | } |
122 | 122 | |
123 | - if (!empty($definition[self::OPTIONAL]) && $definition[self::OPTIONAL]) { |
|
123 | + if (!empty($definition[self::OPTIONAL]) && $definition[self::OPTIONAL]){ |
|
124 | 124 | $optional = true; |
125 | 125 | } |
126 | 126 | |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | FilterProvider::OPTIONAL => $optional, |
134 | 134 | ]; |
135 | 135 | |
136 | - if ($iterate) { |
|
136 | + if ($iterate){ |
|
137 | 137 | [$source, $origin] = $this->parseDefinition($field, $definition[self::ITERATE] ?? $origin); |
138 | 138 | |
139 | 139 | $map[FilterProvider::ITERATE_SOURCE] = $source; |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | */ |
160 | 160 | private function parseDefinition(string $field, string $definition): array |
161 | 161 | { |
162 | - if (strpos($definition, ':') === false) { |
|
162 | + if (strpos($definition, ':') === false){ |
|
163 | 163 | return ['data', $definition ?? $field]; |
164 | 164 | } |
165 | 165 |
@@ -70,18 +70,22 @@ discard block |
||
70 | 70 | protected function buildMap(ReflectionEntity $filter): array |
71 | 71 | { |
72 | 72 | $schema = $filter->getProperty('schema', true); |
73 | - if (empty($schema)) { |
|
73 | + if (empty($schema)) |
|
74 | + { |
|
74 | 75 | throw new SchemaException("Filter `{$filter->getName()}` does not define any schema"); |
75 | 76 | } |
76 | 77 | |
77 | 78 | $result = []; |
78 | - foreach ($schema as $field => $definition) { |
|
79 | + foreach ($schema as $field => $definition) |
|
80 | + { |
|
79 | 81 | $optional = false; |
80 | 82 | |
81 | 83 | // short definition |
82 | - if (is_string($definition)) { |
|
84 | + if (is_string($definition)) |
|
85 | + { |
|
83 | 86 | // simple scalar field definition |
84 | - if (!class_exists($definition)) { |
|
87 | + if (!class_exists($definition)) |
|
88 | + { |
|
85 | 89 | [$source, $origin] = $this->parseDefinition($field, $definition); |
86 | 90 | $result[$field] = [ |
87 | 91 | FilterProvider::SOURCE => $source, |
@@ -102,25 +106,30 @@ discard block |
||
102 | 106 | continue; |
103 | 107 | } |
104 | 108 | |
105 | - if (!is_array($definition) || count($definition) === 0) { |
|
109 | + if (!is_array($definition) || count($definition) === 0) |
|
110 | + { |
|
106 | 111 | throw new SchemaException( |
107 | 112 | "Invalid schema definition at `{$filter->getName()}`->`{$field}`" |
108 | 113 | ); |
109 | 114 | } |
110 | 115 | |
111 | 116 | // complex definition |
112 | - if (!empty($definition[self::ORIGIN])) { |
|
117 | + if (!empty($definition[self::ORIGIN])) |
|
118 | + { |
|
113 | 119 | $origin = $definition[self::ORIGIN]; |
114 | 120 | |
115 | 121 | // [class, 'data:something.*'] vs [class, 'data:something'] |
116 | 122 | $iterate = strpos($origin, '.*') !== false || !empty($definition[self::ITERATE]); |
117 | 123 | $origin = rtrim($origin, '.*'); |
118 | - } else { |
|
124 | + } |
|
125 | + else |
|
126 | + { |
|
119 | 127 | $origin = $field; |
120 | 128 | $iterate = true; |
121 | 129 | } |
122 | 130 | |
123 | - if (!empty($definition[self::OPTIONAL]) && $definition[self::OPTIONAL]) { |
|
131 | + if (!empty($definition[self::OPTIONAL]) && $definition[self::OPTIONAL]) |
|
132 | + { |
|
124 | 133 | $optional = true; |
125 | 134 | } |
126 | 135 | |
@@ -133,7 +142,8 @@ discard block |
||
133 | 142 | FilterProvider::OPTIONAL => $optional, |
134 | 143 | ]; |
135 | 144 | |
136 | - if ($iterate) { |
|
145 | + if ($iterate) |
|
146 | + { |
|
137 | 147 | [$source, $origin] = $this->parseDefinition($field, $definition[self::ITERATE] ?? $origin); |
138 | 148 | |
139 | 149 | $map[FilterProvider::ITERATE_SOURCE] = $source; |
@@ -159,7 +169,8 @@ discard block |
||
159 | 169 | */ |
160 | 170 | private function parseDefinition(string $field, string $definition): array |
161 | 171 | { |
162 | - if (strpos($definition, ':') === false) { |
|
172 | + if (strpos($definition, ':') === false) |
|
173 | + { |
|
163 | 174 | return ['data', $definition ?? $field]; |
164 | 175 | } |
165 | 176 |
@@ -95,7 +95,7 @@ discard block |
||
95 | 95 | FilterProvider::SOURCE => null, |
96 | 96 | FilterProvider::ORIGIN => $field, |
97 | 97 | FilterProvider::FILTER => $definition, |
98 | - FilterProvider::ARRAY => false, |
|
98 | + FilterProvider::array => false, |
|
99 | 99 | FilterProvider::OPTIONAL => $optional, |
100 | 100 | ]; |
101 | 101 | |
@@ -129,7 +129,7 @@ discard block |
||
129 | 129 | FilterProvider::FILTER => $definition[self::NESTED], |
130 | 130 | FilterProvider::SOURCE => null, |
131 | 131 | FilterProvider::ORIGIN => $origin, |
132 | - FilterProvider::ARRAY => $iterate, |
|
132 | + FilterProvider::array => $iterate, |
|
133 | 133 | FilterProvider::OPTIONAL => $optional, |
134 | 134 | ]; |
135 | 135 |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | public const SOURCE = 'source'; |
34 | 34 | public const ORIGIN = 'origin'; |
35 | 35 | public const FILTER = 'filter'; |
36 | - public const ARRAY = 'array'; |
|
36 | + public const array = 'array'; |
|
37 | 37 | public const OPTIONAL = 'optional'; |
38 | 38 | public const ITERATE_SOURCE = 'iterate_source'; |
39 | 39 | public const ITERATE_ORIGIN = 'iterate_origin'; |
@@ -103,7 +103,7 @@ discard block |
||
103 | 103 | } |
104 | 104 | |
105 | 105 | $nested = $map[self::FILTER]; |
106 | - if (empty($map[self::ARRAY])) { |
|
106 | + if (empty($map[self::array])) { |
|
107 | 107 | // slicing down |
108 | 108 | $result[$field] = $this->createFilter($nested, $input->withPrefix($map[self::ORIGIN])); |
109 | 109 | continue; |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | array $schema, |
90 | 90 | ValidatorInterface $validator, |
91 | 91 | ErrorMapper $errorMapper |
92 | - ) { |
|
92 | + ){ |
|
93 | 93 | parent::__construct($data, $schema); |
94 | 94 | |
95 | 95 | $this->mappings = $schema[FilterProvider::MAPPING] ?? []; |
@@ -160,10 +160,10 @@ discard block |
||
160 | 160 | */ |
161 | 161 | public function getErrors(): array |
162 | 162 | { |
163 | - if ($this->errors === null) { |
|
163 | + if ($this->errors === null){ |
|
164 | 164 | $this->errors = []; |
165 | - foreach ($this->validator->withData($this)->getErrors() as $field => $error) { |
|
166 | - if (is_string($error) && Translator::isMessage($error)) { |
|
165 | + foreach ($this->validator->withData($this)->getErrors() as $field => $error){ |
|
166 | + if (is_string($error) && Translator::isMessage($error)){ |
|
167 | 167 | // translate error message |
168 | 168 | $error = $this->say($error); |
169 | 169 | } |
@@ -195,36 +195,36 @@ discard block |
||
195 | 195 | */ |
196 | 196 | protected function validateNested(array $errors): array |
197 | 197 | { |
198 | - foreach ($this->getFields(false) as $index => $value) { |
|
199 | - if (isset($errors[$index])) { |
|
198 | + foreach ($this->getFields(false) as $index => $value){ |
|
199 | + if (isset($errors[$index])){ |
|
200 | 200 | //Invalid on parent level |
201 | 201 | continue; |
202 | 202 | } |
203 | 203 | |
204 | - if ($value instanceof FilterInterface) { |
|
204 | + if ($value instanceof FilterInterface){ |
|
205 | 205 | $isOptional = $this->mappings[$index][FilterProvider::OPTIONAL] ?? false; |
206 | 206 | |
207 | - if ($isOptional && $this->getField($index) !== []) { |
|
207 | + if ($isOptional && $this->getField($index) !== []){ |
|
208 | 208 | continue; |
209 | 209 | } |
210 | 210 | |
211 | - if (!$value->isValid()) { |
|
211 | + if (!$value->isValid()){ |
|
212 | 212 | $errors[$index] = $value->getErrors(); |
213 | 213 | continue; |
214 | 214 | } |
215 | 215 | } |
216 | 216 | |
217 | 217 | //Array of nested entities for validation |
218 | - if (is_iterable($value)) { |
|
219 | - foreach ($value as $nIndex => $nValue) { |
|
220 | - if ($nValue instanceof FilterInterface) { |
|
218 | + if (is_iterable($value)){ |
|
219 | + foreach ($value as $nIndex => $nValue){ |
|
220 | + if ($nValue instanceof FilterInterface){ |
|
221 | 221 | $isOptional = $this->mappings[$index][FilterProvider::OPTIONAL] ?? false; |
222 | 222 | |
223 | - if ($isOptional && $this->getField($nIndex) !== []) { |
|
223 | + if ($isOptional && $this->getField($nIndex) !== []){ |
|
224 | 224 | continue; |
225 | 225 | } |
226 | 226 | |
227 | - if (!$nValue->isValid()) { |
|
227 | + if (!$nValue->isValid()){ |
|
228 | 228 | $errors[$index][$nIndex] = $nValue->getErrors(); |
229 | 229 | } |
230 | 230 | } |
@@ -160,10 +160,13 @@ discard block |
||
160 | 160 | */ |
161 | 161 | public function getErrors(): array |
162 | 162 | { |
163 | - if ($this->errors === null) { |
|
163 | + if ($this->errors === null) |
|
164 | + { |
|
164 | 165 | $this->errors = []; |
165 | - foreach ($this->validator->withData($this)->getErrors() as $field => $error) { |
|
166 | - if (is_string($error) && Translator::isMessage($error)) { |
|
166 | + foreach ($this->validator->withData($this)->getErrors() as $field => $error) |
|
167 | + { |
|
168 | + if (is_string($error) && Translator::isMessage($error)) |
|
169 | + { |
|
167 | 170 | // translate error message |
168 | 171 | $error = $this->say($error); |
169 | 172 | } |
@@ -195,36 +198,46 @@ discard block |
||
195 | 198 | */ |
196 | 199 | protected function validateNested(array $errors): array |
197 | 200 | { |
198 | - foreach ($this->getFields(false) as $index => $value) { |
|
199 | - if (isset($errors[$index])) { |
|
201 | + foreach ($this->getFields(false) as $index => $value) |
|
202 | + { |
|
203 | + if (isset($errors[$index])) |
|
204 | + { |
|
200 | 205 | //Invalid on parent level |
201 | 206 | continue; |
202 | 207 | } |
203 | 208 | |
204 | - if ($value instanceof FilterInterface) { |
|
209 | + if ($value instanceof FilterInterface) |
|
210 | + { |
|
205 | 211 | $isOptional = $this->mappings[$index][FilterProvider::OPTIONAL] ?? false; |
206 | 212 | |
207 | - if ($isOptional && $this->getField($index) !== []) { |
|
213 | + if ($isOptional && $this->getField($index) !== []) |
|
214 | + { |
|
208 | 215 | continue; |
209 | 216 | } |
210 | 217 | |
211 | - if (!$value->isValid()) { |
|
218 | + if (!$value->isValid()) |
|
219 | + { |
|
212 | 220 | $errors[$index] = $value->getErrors(); |
213 | 221 | continue; |
214 | 222 | } |
215 | 223 | } |
216 | 224 | |
217 | 225 | //Array of nested entities for validation |
218 | - if (is_iterable($value)) { |
|
219 | - foreach ($value as $nIndex => $nValue) { |
|
220 | - if ($nValue instanceof FilterInterface) { |
|
226 | + if (is_iterable($value)) |
|
227 | + { |
|
228 | + foreach ($value as $nIndex => $nValue) |
|
229 | + { |
|
230 | + if ($nValue instanceof FilterInterface) |
|
231 | + { |
|
221 | 232 | $isOptional = $this->mappings[$index][FilterProvider::OPTIONAL] ?? false; |
222 | 233 | |
223 | - if ($isOptional && $this->getField($nIndex) !== []) { |
|
234 | + if ($isOptional && $this->getField($nIndex) !== []) |
|
235 | + { |
|
224 | 236 | continue; |
225 | 237 | } |
226 | 238 | |
227 | - if (!$nValue->isValid()) { |
|
239 | + if (!$nValue->isValid()) |
|
240 | + { |
|
228 | 241 | $errors[$index][$nIndex] = $nValue->getErrors(); |
229 | 242 | } |
230 | 243 | } |
@@ -63,8 +63,8 @@ |
||
63 | 63 | #[\ReturnTypeWillChange] |
64 | 64 | public function gc($maxlifetime) |
65 | 65 | { |
66 | - foreach ($this->files->getFiles($this->directory) as $filename) { |
|
67 | - if ($this->files->time($filename) < time() - $maxlifetime) { |
|
66 | + foreach ($this->files->getFiles($this->directory) as $filename){ |
|
67 | + if ($this->files->time($filename) < time() - $maxlifetime){ |
|
68 | 68 | $this->files->delete($filename); |
69 | 69 | } |
70 | 70 | } |
@@ -63,8 +63,10 @@ |
||
63 | 63 | #[\ReturnTypeWillChange] |
64 | 64 | public function gc($maxlifetime) |
65 | 65 | { |
66 | - foreach ($this->files->getFiles($this->directory) as $filename) { |
|
67 | - if ($this->files->time($filename) < time() - $maxlifetime) { |
|
66 | + foreach ($this->files->getFiles($this->directory) as $filename) |
|
67 | + { |
|
68 | + if ($this->files->time($filename) < time() - $maxlifetime) |
|
69 | + { |
|
68 | 70 | $this->files->delete($filename); |
69 | 71 | } |
70 | 72 | } |
@@ -72,7 +72,7 @@ |
||
72 | 72 | #[\ReturnTypeWillChange] |
73 | 73 | public function offsetGet($offset) |
74 | 74 | { |
75 | - if (!$this->offsetExists($offset)) { |
|
75 | + if (!$this->offsetExists($offset)){ |
|
76 | 76 | throw new ConfigException("Undefined configuration key '{$offset}'"); |
77 | 77 | } |
78 | 78 |
@@ -72,7 +72,8 @@ |
||
72 | 72 | #[\ReturnTypeWillChange] |
73 | 73 | public function offsetGet($offset) |
74 | 74 | { |
75 | - if (!$this->offsetExists($offset)) { |
|
75 | + if (!$this->offsetExists($offset)) |
|
76 | + { |
|
76 | 77 | throw new ConfigException("Undefined configuration key '{$offset}'"); |
77 | 78 | } |
78 | 79 |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | */ |
47 | 47 | public function getIterator(): \Traversable |
48 | 48 | { |
49 | - while ($n = $this->next()) { |
|
49 | + while ($n = $this->next()){ |
|
50 | 50 | yield $n; |
51 | 51 | } |
52 | 52 | } |
@@ -64,18 +64,18 @@ discard block |
||
64 | 64 | */ |
65 | 65 | public function next() |
66 | 66 | { |
67 | - if ($this->replay !== []) { |
|
67 | + if ($this->replay !== []){ |
|
68 | 68 | $n = array_shift($this->replay); |
69 | - } else { |
|
69 | + }else{ |
|
70 | 70 | $n = $this->generator->current(); |
71 | - if ($n === null) { |
|
71 | + if ($n === null){ |
|
72 | 72 | return null; |
73 | 73 | } |
74 | 74 | $this->generator->next(); |
75 | 75 | $this->buffer[] = $n; |
76 | 76 | } |
77 | 77 | |
78 | - if ($n !== null) { |
|
78 | + if ($n !== null){ |
|
79 | 79 | $this->offset = $n->offset; |
80 | 80 | } |
81 | 81 | |
@@ -90,10 +90,10 @@ discard block |
||
90 | 90 | public function nextBytes(): string |
91 | 91 | { |
92 | 92 | $result = ''; |
93 | - while ($n = $this->next()) { |
|
94 | - if ($n instanceof Byte) { |
|
93 | + while ($n = $this->next()){ |
|
94 | + if ($n instanceof Byte){ |
|
95 | 95 | $result .= $n->char; |
96 | - } else { |
|
96 | + }else{ |
|
97 | 97 | break; |
98 | 98 | } |
99 | 99 | } |
@@ -106,12 +106,12 @@ discard block |
||
106 | 106 | */ |
107 | 107 | public function lookahead() |
108 | 108 | { |
109 | - if ($this->replay !== []) { |
|
109 | + if ($this->replay !== []){ |
|
110 | 110 | return $this->replay[0]; |
111 | 111 | } |
112 | 112 | |
113 | 113 | $n = $this->next(); |
114 | - if ($n !== null) { |
|
114 | + if ($n !== null){ |
|
115 | 115 | array_unshift($this->replay, $n); |
116 | 116 | } |
117 | 117 | |
@@ -128,20 +128,20 @@ discard block |
||
128 | 128 | { |
129 | 129 | $result = ''; |
130 | 130 | $replay = []; |
131 | - for ($i = 0; $i < $size; $i++) { |
|
131 | + for ($i = 0; $i < $size; $i++){ |
|
132 | 132 | $n = $this->next(); |
133 | - if ($n !== null) { |
|
133 | + if ($n !== null){ |
|
134 | 134 | $replay[] = $n; |
135 | 135 | } |
136 | 136 | |
137 | - if (!$n instanceof Byte) { |
|
137 | + if (!$n instanceof Byte){ |
|
138 | 138 | break; |
139 | 139 | } |
140 | 140 | |
141 | 141 | $result .= $n->char; |
142 | 142 | } |
143 | 143 | |
144 | - foreach (array_reverse($replay) as $n) { |
|
144 | + foreach (array_reverse($replay) as $n){ |
|
145 | 145 | array_unshift($this->replay, $n); |
146 | 146 | } |
147 | 147 | |
@@ -155,8 +155,8 @@ discard block |
||
155 | 155 | */ |
156 | 156 | public function replay(int $offset): void |
157 | 157 | { |
158 | - foreach ($this->buffer as $n) { |
|
159 | - if ($n->offset > $offset) { |
|
158 | + foreach ($this->buffer as $n){ |
|
159 | + if ($n->offset > $offset){ |
|
160 | 160 | $this->replay[] = $n; |
161 | 161 | } |
162 | 162 | } |
@@ -46,7 +46,8 @@ discard block |
||
46 | 46 | */ |
47 | 47 | public function getIterator(): \Traversable |
48 | 48 | { |
49 | - while ($n = $this->next()) { |
|
49 | + while ($n = $this->next()) |
|
50 | + { |
|
50 | 51 | yield $n; |
51 | 52 | } |
52 | 53 | } |
@@ -64,18 +65,23 @@ discard block |
||
64 | 65 | */ |
65 | 66 | public function next() |
66 | 67 | { |
67 | - if ($this->replay !== []) { |
|
68 | + if ($this->replay !== []) |
|
69 | + { |
|
68 | 70 | $n = array_shift($this->replay); |
69 | - } else { |
|
71 | + } |
|
72 | + else |
|
73 | + { |
|
70 | 74 | $n = $this->generator->current(); |
71 | - if ($n === null) { |
|
75 | + if ($n === null) |
|
76 | + { |
|
72 | 77 | return null; |
73 | 78 | } |
74 | 79 | $this->generator->next(); |
75 | 80 | $this->buffer[] = $n; |
76 | 81 | } |
77 | 82 | |
78 | - if ($n !== null) { |
|
83 | + if ($n !== null) |
|
84 | + { |
|
79 | 85 | $this->offset = $n->offset; |
80 | 86 | } |
81 | 87 | |
@@ -90,10 +96,14 @@ discard block |
||
90 | 96 | public function nextBytes(): string |
91 | 97 | { |
92 | 98 | $result = ''; |
93 | - while ($n = $this->next()) { |
|
94 | - if ($n instanceof Byte) { |
|
99 | + while ($n = $this->next()) |
|
100 | + { |
|
101 | + if ($n instanceof Byte) |
|
102 | + { |
|
95 | 103 | $result .= $n->char; |
96 | - } else { |
|
104 | + } |
|
105 | + else |
|
106 | + { |
|
97 | 107 | break; |
98 | 108 | } |
99 | 109 | } |
@@ -106,12 +116,14 @@ discard block |
||
106 | 116 | */ |
107 | 117 | public function lookahead() |
108 | 118 | { |
109 | - if ($this->replay !== []) { |
|
119 | + if ($this->replay !== []) |
|
120 | + { |
|
110 | 121 | return $this->replay[0]; |
111 | 122 | } |
112 | 123 | |
113 | 124 | $n = $this->next(); |
114 | - if ($n !== null) { |
|
125 | + if ($n !== null) |
|
126 | + { |
|
115 | 127 | array_unshift($this->replay, $n); |
116 | 128 | } |
117 | 129 | |
@@ -128,20 +140,24 @@ discard block |
||
128 | 140 | { |
129 | 141 | $result = ''; |
130 | 142 | $replay = []; |
131 | - for ($i = 0; $i < $size; $i++) { |
|
143 | + for ($i = 0; $i < $size; $i++) |
|
144 | + { |
|
132 | 145 | $n = $this->next(); |
133 | - if ($n !== null) { |
|
146 | + if ($n !== null) |
|
147 | + { |
|
134 | 148 | $replay[] = $n; |
135 | 149 | } |
136 | 150 | |
137 | - if (!$n instanceof Byte) { |
|
151 | + if (!$n instanceof Byte) |
|
152 | + { |
|
138 | 153 | break; |
139 | 154 | } |
140 | 155 | |
141 | 156 | $result .= $n->char; |
142 | 157 | } |
143 | 158 | |
144 | - foreach (array_reverse($replay) as $n) { |
|
159 | + foreach (array_reverse($replay) as $n) |
|
160 | + { |
|
145 | 161 | array_unshift($this->replay, $n); |
146 | 162 | } |
147 | 163 | |
@@ -155,8 +171,10 @@ discard block |
||
155 | 171 | */ |
156 | 172 | public function replay(int $offset): void |
157 | 173 | { |
158 | - foreach ($this->buffer as $n) { |
|
159 | - if ($n->offset > $offset) { |
|
174 | + foreach ($this->buffer as $n) |
|
175 | + { |
|
176 | + if ($n->offset > $offset) |
|
177 | + { |
|
160 | 178 | $this->replay[] = $n; |
161 | 179 | } |
162 | 180 | } |
@@ -51,39 +51,39 @@ discard block |
||
51 | 51 | $this->body = null; |
52 | 52 | $hasWhitespace = false; |
53 | 53 | |
54 | - while ($n = $src->next()) { |
|
55 | - if (!$n instanceof Byte) { |
|
54 | + while ($n = $src->next()){ |
|
55 | + if (!$n instanceof Byte){ |
|
56 | 56 | // no other grammars are allowed |
57 | 57 | break; |
58 | 58 | } |
59 | 59 | |
60 | - switch ($n->char) { |
|
60 | + switch ($n->char){ |
|
61 | 61 | case '(': |
62 | 62 | $this->flushName(); |
63 | 63 | $this->tokens[] = new Token(DynamicGrammar::TYPE_BODY_OPEN, $n->offset, $n->char); |
64 | 64 | |
65 | 65 | return $this->parseBody($src); |
66 | 66 | default: |
67 | - if (preg_match(self::REGEXP_WHITESPACE, $n->char)) { |
|
67 | + if (preg_match(self::REGEXP_WHITESPACE, $n->char)){ |
|
68 | 68 | $hasWhitespace = true; |
69 | - if ($this->name !== []) { |
|
69 | + if ($this->name !== []){ |
|
70 | 70 | $this->flushName(); |
71 | 71 | $this->tokens[] = new Token(DynamicGrammar::TYPE_WHITESPACE, $n->offset, $n->char); |
72 | 72 | break; |
73 | 73 | } |
74 | 74 | |
75 | - if ($this->getLastToken()->type === DynamicGrammar::TYPE_WHITESPACE) { |
|
75 | + if ($this->getLastToken()->type === DynamicGrammar::TYPE_WHITESPACE){ |
|
76 | 76 | $this->getLastToken()->content .= $n->char; |
77 | 77 | break; |
78 | 78 | } |
79 | 79 | |
80 | 80 | // invalid directive |
81 | 81 | return false; |
82 | - } elseif ($hasWhitespace) { |
|
82 | + } elseif ($hasWhitespace){ |
|
83 | 83 | return $this->finalize(); |
84 | 84 | } |
85 | 85 | |
86 | - if (!preg_match(self::REGEXP_KEYWORD, $n->char)) { |
|
86 | + if (!preg_match(self::REGEXP_KEYWORD, $n->char)){ |
|
87 | 87 | $this->flushName(); |
88 | 88 | |
89 | 89 | return $this->finalize(); |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | */ |
106 | 106 | public function getIterator(): \Traversable |
107 | 107 | { |
108 | - if ($this->tokens === []) { |
|
108 | + if ($this->tokens === []){ |
|
109 | 109 | throw new \LogicException('Directive not parsed'); |
110 | 110 | } |
111 | 111 | |
@@ -129,8 +129,8 @@ discard block |
||
129 | 129 | */ |
130 | 130 | public function getKeyword(): string |
131 | 131 | { |
132 | - foreach ($this->tokens as $token) { |
|
133 | - if ($token->type === DynamicGrammar::TYPE_KEYWORD) { |
|
132 | + foreach ($this->tokens as $token){ |
|
133 | + if ($token->type === DynamicGrammar::TYPE_KEYWORD){ |
|
134 | 134 | return $token->content; |
135 | 135 | } |
136 | 136 | } |
@@ -145,8 +145,8 @@ discard block |
||
145 | 145 | */ |
146 | 146 | public function getBody(): ?string |
147 | 147 | { |
148 | - foreach ($this->tokens as $token) { |
|
149 | - if ($token->type === DynamicGrammar::TYPE_BODY) { |
|
148 | + foreach ($this->tokens as $token){ |
|
149 | + if ($token->type === DynamicGrammar::TYPE_BODY){ |
|
150 | 150 | return $token->content; |
151 | 151 | } |
152 | 152 | } |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | */ |
160 | 160 | private function flushName(): void |
161 | 161 | { |
162 | - if ($this->name === []) { |
|
162 | + if ($this->name === []){ |
|
163 | 163 | return; |
164 | 164 | } |
165 | 165 | |
@@ -176,17 +176,17 @@ discard block |
||
176 | 176 | $this->body = []; |
177 | 177 | $level = 1; |
178 | 178 | |
179 | - while ($nn = $src->next()) { |
|
180 | - if (!$nn instanceof Byte) { |
|
179 | + while ($nn = $src->next()){ |
|
180 | + if (!$nn instanceof Byte){ |
|
181 | 181 | $this->flushBody(); |
182 | 182 | return $this->finalize(); |
183 | 183 | } |
184 | 184 | |
185 | - if (in_array($nn->char, ['"', '"'])) { |
|
185 | + if (in_array($nn->char, ['"', '"'])){ |
|
186 | 186 | $this->body[] = $nn; |
187 | - while ($nnn = $src->next()) { |
|
187 | + while ($nnn = $src->next()){ |
|
188 | 188 | $this->body[] = $nnn; |
189 | - if ($nnn instanceof Byte && $nnn->char === $nn->char) { |
|
189 | + if ($nnn instanceof Byte && $nnn->char === $nn->char){ |
|
190 | 190 | break; |
191 | 191 | } |
192 | 192 | } |
@@ -195,15 +195,15 @@ discard block |
||
195 | 195 | |
196 | 196 | $this->body[] = $nn; |
197 | 197 | |
198 | - if ($nn->char === '(') { |
|
198 | + if ($nn->char === '('){ |
|
199 | 199 | $level++; |
200 | 200 | continue; |
201 | 201 | } |
202 | 202 | |
203 | - if ($nn->char === ')') { |
|
203 | + if ($nn->char === ')'){ |
|
204 | 204 | $level--; |
205 | 205 | |
206 | - if ($level === 0) { |
|
206 | + if ($level === 0){ |
|
207 | 207 | $n = array_pop($this->body); |
208 | 208 | |
209 | 209 | $this->flushBody(); |
@@ -224,7 +224,7 @@ discard block |
||
224 | 224 | */ |
225 | 225 | private function flushBody(): void |
226 | 226 | { |
227 | - if ($this->body === []) { |
|
227 | + if ($this->body === []){ |
|
228 | 228 | return; |
229 | 229 | } |
230 | 230 | |
@@ -237,7 +237,7 @@ discard block |
||
237 | 237 | */ |
238 | 238 | private function getLastToken(): Token |
239 | 239 | { |
240 | - if ($this->tokens === []) { |
|
240 | + if ($this->tokens === []){ |
|
241 | 241 | throw new \LogicException('Directive not parsed'); |
242 | 242 | } |
243 | 243 | |
@@ -253,8 +253,8 @@ discard block |
||
253 | 253 | { |
254 | 254 | $tokens = $this->tokens; |
255 | 255 | |
256 | - foreach (array_reverse($tokens, true) as $i => $t) { |
|
257 | - if ($t->type !== DynamicGrammar::TYPE_WHITESPACE) { |
|
256 | + foreach (array_reverse($tokens, true) as $i => $t){ |
|
257 | + if ($t->type !== DynamicGrammar::TYPE_WHITESPACE){ |
|
258 | 258 | break; |
259 | 259 | } |
260 | 260 | |
@@ -262,19 +262,19 @@ discard block |
||
262 | 262 | } |
263 | 263 | |
264 | 264 | $body = null; |
265 | - foreach ($tokens as $t) { |
|
266 | - if ($t->type === DynamicGrammar::TYPE_BODY_OPEN) { |
|
265 | + foreach ($tokens as $t){ |
|
266 | + if ($t->type === DynamicGrammar::TYPE_BODY_OPEN){ |
|
267 | 267 | $body = false; |
268 | 268 | continue; |
269 | 269 | } |
270 | 270 | |
271 | - if ($t->type === DynamicGrammar::TYPE_BODY_CLOSE) { |
|
271 | + if ($t->type === DynamicGrammar::TYPE_BODY_CLOSE){ |
|
272 | 272 | $body = null; |
273 | 273 | continue; |
274 | 274 | } |
275 | 275 | } |
276 | 276 | |
277 | - if ($body !== null) { |
|
277 | + if ($body !== null){ |
|
278 | 278 | return false; |
279 | 279 | } |
280 | 280 |
@@ -51,39 +51,48 @@ discard block |
||
51 | 51 | $this->body = null; |
52 | 52 | $hasWhitespace = false; |
53 | 53 | |
54 | - while ($n = $src->next()) { |
|
55 | - if (!$n instanceof Byte) { |
|
54 | + while ($n = $src->next()) |
|
55 | + { |
|
56 | + if (!$n instanceof Byte) |
|
57 | + { |
|
56 | 58 | // no other grammars are allowed |
57 | 59 | break; |
58 | 60 | } |
59 | 61 | |
60 | - switch ($n->char) { |
|
62 | + switch ($n->char) |
|
63 | + { |
|
61 | 64 | case '(': |
62 | 65 | $this->flushName(); |
63 | 66 | $this->tokens[] = new Token(DynamicGrammar::TYPE_BODY_OPEN, $n->offset, $n->char); |
64 | 67 | |
65 | 68 | return $this->parseBody($src); |
66 | 69 | default: |
67 | - if (preg_match(self::REGEXP_WHITESPACE, $n->char)) { |
|
70 | + if (preg_match(self::REGEXP_WHITESPACE, $n->char)) |
|
71 | + { |
|
68 | 72 | $hasWhitespace = true; |
69 | - if ($this->name !== []) { |
|
73 | + if ($this->name !== []) |
|
74 | + { |
|
70 | 75 | $this->flushName(); |
71 | 76 | $this->tokens[] = new Token(DynamicGrammar::TYPE_WHITESPACE, $n->offset, $n->char); |
72 | 77 | break; |
73 | 78 | } |
74 | 79 | |
75 | - if ($this->getLastToken()->type === DynamicGrammar::TYPE_WHITESPACE) { |
|
80 | + if ($this->getLastToken()->type === DynamicGrammar::TYPE_WHITESPACE) |
|
81 | + { |
|
76 | 82 | $this->getLastToken()->content .= $n->char; |
77 | 83 | break; |
78 | 84 | } |
79 | 85 | |
80 | 86 | // invalid directive |
81 | 87 | return false; |
82 | - } elseif ($hasWhitespace) { |
|
88 | + } |
|
89 | + elseif ($hasWhitespace) |
|
90 | + { |
|
83 | 91 | return $this->finalize(); |
84 | 92 | } |
85 | 93 | |
86 | - if (!preg_match(self::REGEXP_KEYWORD, $n->char)) { |
|
94 | + if (!preg_match(self::REGEXP_KEYWORD, $n->char)) |
|
95 | + { |
|
87 | 96 | $this->flushName(); |
88 | 97 | |
89 | 98 | return $this->finalize(); |
@@ -105,7 +114,8 @@ discard block |
||
105 | 114 | */ |
106 | 115 | public function getIterator(): \Traversable |
107 | 116 | { |
108 | - if ($this->tokens === []) { |
|
117 | + if ($this->tokens === []) |
|
118 | + { |
|
109 | 119 | throw new \LogicException('Directive not parsed'); |
110 | 120 | } |
111 | 121 | |
@@ -129,8 +139,10 @@ discard block |
||
129 | 139 | */ |
130 | 140 | public function getKeyword(): string |
131 | 141 | { |
132 | - foreach ($this->tokens as $token) { |
|
133 | - if ($token->type === DynamicGrammar::TYPE_KEYWORD) { |
|
142 | + foreach ($this->tokens as $token) |
|
143 | + { |
|
144 | + if ($token->type === DynamicGrammar::TYPE_KEYWORD) |
|
145 | + { |
|
134 | 146 | return $token->content; |
135 | 147 | } |
136 | 148 | } |
@@ -145,8 +157,10 @@ discard block |
||
145 | 157 | */ |
146 | 158 | public function getBody(): ?string |
147 | 159 | { |
148 | - foreach ($this->tokens as $token) { |
|
149 | - if ($token->type === DynamicGrammar::TYPE_BODY) { |
|
160 | + foreach ($this->tokens as $token) |
|
161 | + { |
|
162 | + if ($token->type === DynamicGrammar::TYPE_BODY) |
|
163 | + { |
|
150 | 164 | return $token->content; |
151 | 165 | } |
152 | 166 | } |
@@ -159,7 +173,8 @@ discard block |
||
159 | 173 | */ |
160 | 174 | private function flushName(): void |
161 | 175 | { |
162 | - if ($this->name === []) { |
|
176 | + if ($this->name === []) |
|
177 | + { |
|
163 | 178 | return; |
164 | 179 | } |
165 | 180 | |
@@ -176,17 +191,22 @@ discard block |
||
176 | 191 | $this->body = []; |
177 | 192 | $level = 1; |
178 | 193 | |
179 | - while ($nn = $src->next()) { |
|
180 | - if (!$nn instanceof Byte) { |
|
194 | + while ($nn = $src->next()) |
|
195 | + { |
|
196 | + if (!$nn instanceof Byte) |
|
197 | + { |
|
181 | 198 | $this->flushBody(); |
182 | 199 | return $this->finalize(); |
183 | 200 | } |
184 | 201 | |
185 | - if (in_array($nn->char, ['"', '"'])) { |
|
202 | + if (in_array($nn->char, ['"', '"'])) |
|
203 | + { |
|
186 | 204 | $this->body[] = $nn; |
187 | - while ($nnn = $src->next()) { |
|
205 | + while ($nnn = $src->next()) |
|
206 | + { |
|
188 | 207 | $this->body[] = $nnn; |
189 | - if ($nnn instanceof Byte && $nnn->char === $nn->char) { |
|
208 | + if ($nnn instanceof Byte && $nnn->char === $nn->char) |
|
209 | + { |
|
190 | 210 | break; |
191 | 211 | } |
192 | 212 | } |
@@ -195,15 +215,18 @@ discard block |
||
195 | 215 | |
196 | 216 | $this->body[] = $nn; |
197 | 217 | |
198 | - if ($nn->char === '(') { |
|
218 | + if ($nn->char === '(') |
|
219 | + { |
|
199 | 220 | $level++; |
200 | 221 | continue; |
201 | 222 | } |
202 | 223 | |
203 | - if ($nn->char === ')') { |
|
224 | + if ($nn->char === ')') |
|
225 | + { |
|
204 | 226 | $level--; |
205 | 227 | |
206 | - if ($level === 0) { |
|
228 | + if ($level === 0) |
|
229 | + { |
|
207 | 230 | $n = array_pop($this->body); |
208 | 231 | |
209 | 232 | $this->flushBody(); |
@@ -224,7 +247,8 @@ discard block |
||
224 | 247 | */ |
225 | 248 | private function flushBody(): void |
226 | 249 | { |
227 | - if ($this->body === []) { |
|
250 | + if ($this->body === []) |
|
251 | + { |
|
228 | 252 | return; |
229 | 253 | } |
230 | 254 | |
@@ -237,7 +261,8 @@ discard block |
||
237 | 261 | */ |
238 | 262 | private function getLastToken(): Token |
239 | 263 | { |
240 | - if ($this->tokens === []) { |
|
264 | + if ($this->tokens === []) |
|
265 | + { |
|
241 | 266 | throw new \LogicException('Directive not parsed'); |
242 | 267 | } |
243 | 268 | |
@@ -253,8 +278,10 @@ discard block |
||
253 | 278 | { |
254 | 279 | $tokens = $this->tokens; |
255 | 280 | |
256 | - foreach (array_reverse($tokens, true) as $i => $t) { |
|
257 | - if ($t->type !== DynamicGrammar::TYPE_WHITESPACE) { |
|
281 | + foreach (array_reverse($tokens, true) as $i => $t) |
|
282 | + { |
|
283 | + if ($t->type !== DynamicGrammar::TYPE_WHITESPACE) |
|
284 | + { |
|
258 | 285 | break; |
259 | 286 | } |
260 | 287 | |
@@ -262,19 +289,23 @@ discard block |
||
262 | 289 | } |
263 | 290 | |
264 | 291 | $body = null; |
265 | - foreach ($tokens as $t) { |
|
266 | - if ($t->type === DynamicGrammar::TYPE_BODY_OPEN) { |
|
292 | + foreach ($tokens as $t) |
|
293 | + { |
|
294 | + if ($t->type === DynamicGrammar::TYPE_BODY_OPEN) |
|
295 | + { |
|
267 | 296 | $body = false; |
268 | 297 | continue; |
269 | 298 | } |
270 | 299 | |
271 | - if ($t->type === DynamicGrammar::TYPE_BODY_CLOSE) { |
|
300 | + if ($t->type === DynamicGrammar::TYPE_BODY_CLOSE) |
|
301 | + { |
|
272 | 302 | $body = null; |
273 | 303 | continue; |
274 | 304 | } |
275 | 305 | } |
276 | 306 | |
277 | - if ($body !== null) { |
|
307 | + if ($body !== null) |
|
308 | + { |
|
278 | 309 | return false; |
279 | 310 | } |
280 | 311 |