|
2 | 2 |
|
3 | 3 | namespace Coduo\PHPMatcher\Matcher;
|
4 | 4 |
|
| 5 | +use Coduo\PHPMatcher\Exception\Exception; |
5 | 6 | use Coduo\PHPMatcher\Parser;
|
6 | 7 | use Coduo\ToString\StringConverter;
|
7 | 8 | use Symfony\Component\PropertyAccess\PropertyAccess;
|
@@ -153,19 +154,46 @@ function ($item) use ($skipPattern) {
|
153 | 154 | }
|
154 | 155 | );
|
155 | 156 |
|
156 |
| - $notExistingKeys = array_diff_key($pattern, $values); |
157 |
| - |
| 157 | + $notExistingKeys = $this->findNotExistingKeys($pattern, $values); |
158 | 158 | if (count($notExistingKeys) > 0) {
|
159 | 159 | $keyNames = array_keys($notExistingKeys);
|
160 | 160 | $path = $this->formatFullPath($parentPath, $this->formatAccessPath($keyNames[0]));
|
161 | 161 | $this->setMissingElementInError('value', $path);
|
| 162 | + |
162 | 163 | return false;
|
163 | 164 | }
|
164 | 165 | }
|
165 | 166 |
|
166 | 167 | return true;
|
167 | 168 | }
|
168 | 169 |
|
| 170 | + /** |
| 171 | + * Finds not existing keys |
| 172 | + * Excludes keys with pattern which includes Optional Expander |
| 173 | + * |
| 174 | + * @param $pattern |
| 175 | + * @param $values |
| 176 | + * @return array |
| 177 | + */ |
| 178 | + private function findNotExistingKeys($pattern, $values) |
| 179 | + { |
| 180 | + $notExistingKeys = array_diff_key($pattern, $values); |
| 181 | + |
| 182 | + return array_filter($notExistingKeys, function ($pattern) use ($values) { |
| 183 | + if (is_array($pattern)) { |
| 184 | + return !$this->match($values, $pattern); |
| 185 | + } |
| 186 | + |
| 187 | + try { |
| 188 | + $typePattern = $this->parser->parse($pattern); |
| 189 | + } catch (Exception $e) { |
| 190 | + return true; |
| 191 | + } |
| 192 | + |
| 193 | + return !$typePattern->hasExpander('optional'); |
| 194 | + }); |
| 195 | + } |
| 196 | + |
169 | 197 | /**
|
170 | 198 | * @param $value
|
171 | 199 | * @param $pattern
|
|
0 commit comments