@@ -33,7 +33,7 @@ class PropertyAccessor implements PropertyAccessorInterface
33
33
/**
34
34
* @var Boolean
35
35
*/
36
- private $ throwExceptionOnInvalidIndex ;
36
+ private $ ignoreInvalidIndices ;
37
37
38
38
/**
39
39
* Should not be used by application code. Use
@@ -42,7 +42,7 @@ class PropertyAccessor implements PropertyAccessorInterface
42
42
public function __construct ($ magicCall = false , $ throwExceptionOnInvalidIndex = false )
43
43
{
44
44
$ this ->magicCall = $ magicCall ;
45
- $ this ->throwExceptionOnInvalidIndex = $ throwExceptionOnInvalidIndex ;
45
+ $ this ->ignoreInvalidIndices = ! $ throwExceptionOnInvalidIndex ;
46
46
}
47
47
48
48
/**
@@ -56,7 +56,7 @@ public function getValue($objectOrArray, $propertyPath)
56
56
throw new UnexpectedTypeException ($ propertyPath , 'string or Symfony\Component\PropertyAccess\PropertyPathInterface ' );
57
57
}
58
58
59
- $ propertyValues =& $ this ->readPropertiesUntil ($ objectOrArray , $ propertyPath , $ propertyPath ->getLength (), $ this ->throwExceptionOnInvalidIndex );
59
+ $ propertyValues =& $ this ->readPropertiesUntil ($ objectOrArray , $ propertyPath , $ propertyPath ->getLength (), $ this ->ignoreInvalidIndices );
60
60
61
61
return $ propertyValues [count ($ propertyValues ) - 1 ][self ::VALUE ];
62
62
}
@@ -117,7 +117,7 @@ public function isReadable($objectOrArray, $propertyPath)
117
117
}
118
118
119
119
try {
120
- $ this ->readPropertiesUntil ($ objectOrArray , $ propertyPath , $ propertyPath ->getLength (), $ this ->throwExceptionOnInvalidIndex );
120
+ $ this ->readPropertiesUntil ($ objectOrArray , $ propertyPath , $ propertyPath ->getLength (), $ this ->ignoreInvalidIndices );
121
121
122
122
return true ;
123
123
} catch (NoSuchIndexException $ e ) {
@@ -186,15 +186,18 @@ public function isWritable($objectOrArray, $propertyPath, $value)
186
186
/**
187
187
* Reads the path from an object up to a given path index.
188
188
*
189
- * @param object|array $objectOrArray The object or array to read from
190
- * @param PropertyPathInterface $propertyPath The property path to read
191
- * @param integer $lastIndex The index up to which should be read
189
+ * @param object|array $objectOrArray The object or array to read from
190
+ * @param PropertyPathInterface $propertyPath The property path to read
191
+ * @param integer $lastIndex The index up to which should be read
192
+ * @param Boolean $ignoreInvalidIndices Whether to ignore invalid indices
193
+ * or throw an exception
192
194
*
193
195
* @return array The values read in the path.
194
196
*
195
197
* @throws UnexpectedTypeException If a value within the path is neither object nor array.
198
+ * @throws NoSuchIndexException If a non-existing index is accessed
196
199
*/
197
- private function &readPropertiesUntil (&$ objectOrArray , PropertyPathInterface $ propertyPath , $ lastIndex , $ throwExceptionOnInvalidIndex = false )
200
+ private function &readPropertiesUntil (&$ objectOrArray , PropertyPathInterface $ propertyPath , $ lastIndex , $ ignoreInvalidIndices = true )
198
201
{
199
202
$ propertyValues = array ();
200
203
@@ -209,7 +212,7 @@ private function &readPropertiesUntil(&$objectOrArray, PropertyPathInterface $pr
209
212
210
213
// Create missing nested arrays on demand
211
214
if ($ isIndex && $ isArrayAccess && !isset ($ objectOrArray [$ property ])) {
212
- if ($ throwExceptionOnInvalidIndex ) {
215
+ if (! $ ignoreInvalidIndices ) {
213
216
throw new NoSuchIndexException (sprintf ('Cannot read property "%s". Available properties are "%s" ' , $ property , print_r (array_keys ($ objectOrArray ), true )));
214
217
}
215
218
0 commit comments