6
6
use Coduo \ToString \StringConverter ;
7
7
use Symfony \Component \PropertyAccess \PropertyAccess ;
8
8
use Symfony \Component \PropertyAccess \PropertyAccessor ;
9
+ use Symfony \Component \PropertyAccess \PropertyPath ;
9
10
10
11
final class ArrayMatcher extends Matcher
11
12
{
@@ -147,7 +148,7 @@ private function isPatternValid(array $pattern, array $values, $parentPath)
147
148
148
149
if (count ($ notExistingKeys ) > 0 ) {
149
150
$ keyNames = array_keys ($ notExistingKeys );
150
- $ path = $ this ->formatFullPath ($ parentPath , $ this ->formatAccessPath ($ keyNames [0 ]));
151
+ $ path = $ this ->formatFullPath ($ parentPath , $ this ->formatAccessPath ($ keyNames [0 ]));
151
152
$ this ->setMissingElementInError ('value ' , $ path );
152
153
return false ;
153
154
}
@@ -180,7 +181,33 @@ private function valueMatchPattern($value, $pattern)
180
181
*/
181
182
private function valueExist ($ path , array $ haystack )
182
183
{
183
- return $ this ->getPropertyAccessor ()->isReadable ($ haystack , $ path );
184
+ $ propertyPath = new PropertyPath ($ path );
185
+ $ length = $ propertyPath ->getLength ();
186
+ $ valueExist = true ;
187
+ for ($ i = 0 ; $ i < $ length ; ++$ i ) {
188
+ $ property = $ propertyPath ->getElement ($ i );
189
+ $ isIndex = $ propertyPath ->isIndex ($ i );
190
+ $ propertyExist = $ this ->arrayPropertyExists ($ property , $ haystack );
191
+
192
+ if ($ isIndex && !$ propertyExist ) {
193
+ $ valueExist = false ;
194
+ break ;
195
+ }
196
+ }
197
+
198
+ unset($ propertyPath );
199
+ return $ valueExist ;
200
+ }
201
+
202
+ /**
203
+ * @param string $property
204
+ * @param array $objectOrArray
205
+ * @return bool
206
+ */
207
+ private function arrayPropertyExists ($ property , array $ objectOrArray )
208
+ {
209
+ return ($ objectOrArray instanceof \ArrayAccess && isset ($ objectOrArray [$ property ])) ||
210
+ (is_array ($ objectOrArray ) && array_key_exists ($ property , $ objectOrArray ));
184
211
}
185
212
186
213
/**
@@ -203,7 +230,6 @@ private function getPropertyAccessor()
203
230
}
204
231
205
232
$ accessorBuilder = PropertyAccess::createPropertyAccessorBuilder ();
206
- $ accessorBuilder ->enableExceptionOnInvalidIndex ();
207
233
$ this ->accessor = $ accessorBuilder ->getPropertyAccessor ();
208
234
209
235
return $ this ->accessor ;
0 commit comments