From f43cf44aaa0be949b410a1786cd35d1d97be8b36 Mon Sep 17 00:00:00 2001 From: Ruud Kamphuis Date: Wed, 30 Aug 2017 13:54:30 +0200 Subject: [PATCH] Add support for `is*` accessors in ClosureExpressionVisitor When your property is called `private $isActive = true` with `function isActive()` as the getter, it should be possible to query it with `isActive = true` without getting errors. --- .../Collections/Expr/ClosureExpressionVisitor.php | 15 ++++++++------- .../Collections/ClosureExpressionVisitorTest.php | 7 +++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php b/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php index 231348692..2d6542f56 100644 --- a/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php +++ b/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php @@ -9,6 +9,7 @@ use function is_array; use function iterator_to_array; use function method_exists; +use function preg_match; use function preg_replace_callback; use function strlen; use function strpos; @@ -44,11 +45,13 @@ public static function getObjectFieldValue($object, $field) foreach ($accessors as $accessor) { $accessor .= $field; - if (! method_exists($object, $accessor)) { - continue; + if (method_exists($object, $accessor)) { + return $object->$accessor(); } + } - return $object->$accessor(); + if (preg_match('/^is[A-Z]+/', $field) === 1 && method_exists($object, $field)) { + return $object->$field(); } // __call should be triggered for get. @@ -74,11 +77,9 @@ public static function getObjectFieldValue($object, $field) foreach ($accessors as $accessor) { $accessor .= $ccField; - if (! method_exists($object, $accessor)) { - continue; + if (method_exists($object, $accessor)) { + return $object->$accessor(); } - - return $object->$accessor(); } return $object->$field; diff --git a/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php b/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php index eff227d4d..ab4e2d2be 100644 --- a/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php +++ b/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php @@ -31,6 +31,13 @@ public function testGetObjectFieldValueIsAccessor() : void self::assertTrue($this->visitor->getObjectFieldValue($object, 'baz')); } + public function testGetObjectFieldValueIsAccessorWithIsPrefix() : void + { + $object = new TestObject(1, 2, true); + + self::assertTrue($this->visitor->getObjectFieldValue($object, 'isBaz')); + } + public function testGetObjectFieldValueIsAccessorCamelCase() : void { $object = new TestObjectNotCamelCase(1);