From 32d524547eb452718f85aa76b5cee4f7460c8fd0 Mon Sep 17 00:00:00 2001 From: Yannick de Lange Date: Mon, 2 Jun 2014 14:42:10 +0200 Subject: [PATCH 1/6] Added support for non-camel-case variable code conventions in ClosureExpressionVisitor --- .../Collections/Expr/ClosureExpressionVisitor.php | 13 +++++++++++++ .../Collections/ClosureExpressionVisitorTest.php | 14 ++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php b/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php index 994085f91..0cbda6d07 100644 --- a/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php +++ b/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php @@ -58,6 +58,19 @@ public static function getObjectFieldValue($object, $field) return $object->$accessor(); } + // camelcase field name to support different variable naming conventions + $ccField = preg_replace_callback('/_(.?)/', function($matches) { return strtoupper($matches[1]); }, $field); + + foreach ($accessors as $accessor) { + $accessor .= $ccField; + + if ( ! method_exists($object, $accessor)) { + continue; + } + + return $object->$accessor(); + } + // __call should be triggered for get. $accessor = $accessors[0] . $field; diff --git a/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php b/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php index 21c2e3dfc..dea46f88f 100644 --- a/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php +++ b/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php @@ -50,6 +50,15 @@ public function testGetObjectFieldValueIsAccessor() $this->assertTrue($this->visitor->getObjectFieldValue($object, 'baz')); } + public function testGetObjectFieldValueIsAccessorCamelCase() + { + $object = new TestObject(1, 2); + + $this->assertEquals([1, 2], $this->visitor->getObjectFieldValue($object, 'foo_bar')); + $this->assertEquals([1, 2], $this->visitor->getObjectFieldValue($object, 'foobar')); + $this->assertEquals([1, 2], $this->visitor->getObjectFieldValue($object, 'fooBar')); + } + public function testGetObjectFieldValueMagicCallMethod() { $object = new TestObject(1, 2, true, 3); @@ -246,5 +255,10 @@ public function isBaz() { return $this->baz; } + + public function getFooBar() + { + return array($this->foo, $this->bar); + } } From 4710b0a4a8abc30c534489246e464873d8f53262 Mon Sep 17 00:00:00 2001 From: Yannick de Lange Date: Mon, 2 Jun 2014 15:23:57 +0200 Subject: [PATCH 2/6] Fixed PHP5.4 array thing --- .../Common/Collections/ClosureExpressionVisitorTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php b/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php index dea46f88f..636bf57c0 100644 --- a/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php +++ b/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php @@ -54,9 +54,9 @@ public function testGetObjectFieldValueIsAccessorCamelCase() { $object = new TestObject(1, 2); - $this->assertEquals([1, 2], $this->visitor->getObjectFieldValue($object, 'foo_bar')); - $this->assertEquals([1, 2], $this->visitor->getObjectFieldValue($object, 'foobar')); - $this->assertEquals([1, 2], $this->visitor->getObjectFieldValue($object, 'fooBar')); + $this->assertEquals(array(1, 2), $this->visitor->getObjectFieldValue($object, 'foo_bar')); + $this->assertEquals(array(1, 2), $this->visitor->getObjectFieldValue($object, 'foobar')); + $this->assertEquals(array(1, 2), $this->visitor->getObjectFieldValue($object, 'fooBar')); } public function testGetObjectFieldValueMagicCallMethod() From e16bfec1c456777049be2f26e5cc7f293259d35b Mon Sep 17 00:00:00 2001 From: Yannick de Lange Date: Fri, 6 Jun 2014 12:24:50 +0200 Subject: [PATCH 3/6] Made Camel Case last callback --- .../Expr/ClosureExpressionVisitor.php | 30 +++++++++++-------- .../ClosureExpressionVisitorTest.php | 25 ++++++++++++---- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php b/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php index 0cbda6d07..76400248d 100644 --- a/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php +++ b/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php @@ -58,18 +58,6 @@ public static function getObjectFieldValue($object, $field) return $object->$accessor(); } - // camelcase field name to support different variable naming conventions - $ccField = preg_replace_callback('/_(.?)/', function($matches) { return strtoupper($matches[1]); }, $field); - - foreach ($accessors as $accessor) { - $accessor .= $ccField; - - if ( ! method_exists($object, $accessor)) { - continue; - } - - return $object->$accessor(); - } // __call should be triggered for get. $accessor = $accessors[0] . $field; @@ -82,6 +70,24 @@ public static function getObjectFieldValue($object, $field) return $object[$field]; } + if (isset($object->$field)) { + return $object->$field; + } + + // camelcase field name to support different variable naming conventions + $ccField = preg_replace_callback('/_(.?)/', function($matches) { return strtoupper($matches[1]); }, $field); + + foreach ($accessors as $accessor) { + $accessor .= $ccField; + + + if ( ! method_exists($object, $accessor)) { + continue; + } + + 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 636bf57c0..ede9d3cae 100644 --- a/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php +++ b/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php @@ -52,11 +52,11 @@ public function testGetObjectFieldValueIsAccessor() public function testGetObjectFieldValueIsAccessorCamelCase() { - $object = new TestObject(1, 2); + $object = new TestObjectNotCamelCase(1); - $this->assertEquals(array(1, 2), $this->visitor->getObjectFieldValue($object, 'foo_bar')); - $this->assertEquals(array(1, 2), $this->visitor->getObjectFieldValue($object, 'foobar')); - $this->assertEquals(array(1, 2), $this->visitor->getObjectFieldValue($object, 'fooBar')); + $this->assertEquals(1, $this->visitor->getObjectFieldValue($object, 'foo_bar')); + $this->assertEquals(1, $this->visitor->getObjectFieldValue($object, 'foobar')); + $this->assertEquals(1, $this->visitor->getObjectFieldValue($object, 'fooBar')); } public function testGetObjectFieldValueMagicCallMethod() @@ -255,10 +255,25 @@ public function isBaz() { return $this->baz; } - + public function getFooBar() { return array($this->foo, $this->bar); } } +class TestObjectNotCamelCase +{ + private $foo_bar; + + public function __construct($foo_bar = null) + { + $this->foo_bar = $foo_bar; + } + + public function getFooBar() + { + return $this->foo_bar; + } +} + From 91256bb756eff73a837c93a70e6a66e17a174a22 Mon Sep 17 00:00:00 2001 From: Yannick de Lange Date: Fri, 6 Jun 2014 13:02:51 +0200 Subject: [PATCH 4/6] Removed unused method from test class --- .../Common/Collections/ClosureExpressionVisitorTest.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php b/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php index ede9d3cae..998568bf3 100644 --- a/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php +++ b/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php @@ -255,11 +255,6 @@ public function isBaz() { return $this->baz; } - - public function getFooBar() - { - return array($this->foo, $this->bar); - } } class TestObjectNotCamelCase From b69ac56b5ec36a6ac19ad45282a63ef4b841b424 Mon Sep 17 00:00:00 2001 From: Yannick de Lange Date: Fri, 6 Jun 2014 13:12:04 +0200 Subject: [PATCH 5/6] removed extra whiteline --- .../Common/Collections/Expr/ClosureExpressionVisitor.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php b/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php index 76400248d..5a4cbeae8 100644 --- a/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php +++ b/lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php @@ -58,7 +58,6 @@ public static function getObjectFieldValue($object, $field) return $object->$accessor(); } - // __call should be triggered for get. $accessor = $accessors[0] . $field; From 99f52ec3281927d1e5a42003819f9b0c74db66bc Mon Sep 17 00:00:00 2001 From: Iltar van der Berg Date: Mon, 21 Mar 2016 08:19:01 +0100 Subject: [PATCH 6/6] Added extra tests to prove backwards compatibility --- .../ClosureExpressionVisitorTest.php | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php b/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php index 998568bf3..d25e1b108 100644 --- a/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php +++ b/tests/Doctrine/Tests/Common/Collections/ClosureExpressionVisitorTest.php @@ -59,6 +59,33 @@ public function testGetObjectFieldValueIsAccessorCamelCase() $this->assertEquals(1, $this->visitor->getObjectFieldValue($object, 'fooBar')); } + public function testGetObjectFieldValueIsAccessorBoth() + { + $object = new TestObjectBothCamelCaseAndUnderscore(1, 2); + + $this->assertEquals(2, $this->visitor->getObjectFieldValue($object, 'foo_bar')); + $this->assertEquals(2, $this->visitor->getObjectFieldValue($object, 'foobar')); + $this->assertEquals(2, $this->visitor->getObjectFieldValue($object, 'fooBar')); + } + + public function testGetObjectFieldValueIsAccessorOnePublic() + { + $object = new TestObjectPublicCamelCaseAndPrivateUnderscore(1, 2); + + $this->assertEquals(2, $this->visitor->getObjectFieldValue($object, 'foo_bar')); + $this->assertEquals(2, $this->visitor->getObjectFieldValue($object, 'foobar')); + $this->assertEquals(2, $this->visitor->getObjectFieldValue($object, 'fooBar')); + } + + public function testGetObjectFieldValueIsAccessorBothPublic() + { + $object = new TestObjectPublicCamelCaseAndPrivateUnderscore(1, 2); + + $this->assertEquals(2, $this->visitor->getObjectFieldValue($object, 'foo_bar')); + $this->assertEquals(2, $this->visitor->getObjectFieldValue($object, 'foobar')); + $this->assertEquals(2, $this->visitor->getObjectFieldValue($object, 'fooBar')); + } + public function testGetObjectFieldValueMagicCallMethod() { $object = new TestObject(1, 2, true, 3); @@ -272,3 +299,53 @@ public function getFooBar() } } +class TestObjectBothCamelCaseAndUnderscore +{ + private $foo_bar; + private $fooBar; + + public function __construct($foo_bar = null, $fooBar = null) + { + $this->foo_bar = $foo_bar; + $this->fooBar = $fooBar; + } + + public function getFooBar() + { + return $this->fooBar; + } +} + +class TestObjectPublicCamelCaseAndPrivateUnderscore +{ + private $foo_bar; + public $fooBar; + + public function __construct($foo_bar = null, $fooBar = null) + { + $this->foo_bar = $foo_bar; + $this->fooBar = $fooBar; + } + + public function getFooBar() + { + return $this->fooBar; + } +} + +class TestObjectBothPublic +{ + public $foo_bar; + public $fooBar; + + public function __construct($foo_bar = null, $fooBar = null) + { + $this->foo_bar = $foo_bar; + $this->fooBar = $fooBar; + } + + public function getFooBar() + { + return $this->foo_bar; + } +}