From 253eebad881573cf119a01b5f71bc032457ab522 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dariusz=20G=C3=B3recki?= Date: Tue, 10 Jan 2012 11:06:00 +0100 Subject: [PATCH] [BugFix][Validator] Fix for PHP incosistent behaviour of ArrayAccess Bug fix: yes Feature addition: no Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: #2779 Todo: - Because PHP function `array_key_exists` is buggy, it works great with native PHP `ArrayObject` instances, but hand written implementations of `ArrayAccess` and `Traversable` objects will fail to work with `CollectionValidator` --- .../Component/Validator/Constraints/CollectionValidator.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Validator/Constraints/CollectionValidator.php b/src/Symfony/Component/Validator/Constraints/CollectionValidator.php index 67ede37b19..e703b538e0 100644 --- a/src/Symfony/Component/Validator/Constraints/CollectionValidator.php +++ b/src/Symfony/Component/Validator/Constraints/CollectionValidator.php @@ -52,7 +52,11 @@ class CollectionValidator extends ConstraintValidator } foreach ($constraint->fields as $field => $constraints) { - if (array_key_exists($field, $value)) { + if ( + // bug fix issue #2779 + (is_array($value) && array_key_exists($field, $value)) || + ($value instanceof \ArrayAccess && $value->offsetExists($field)) + ) { // cannot simply cast to array, because then the object is converted to an // array instead of wrapped inside $constraints = is_array($constraints) ? $constraints : array($constraints);