From 2a75a8e426e77aba2ba86733ebf043a769f607e7 Mon Sep 17 00:00:00 2001 From: Piotr Pasich Date: Wed, 19 Dec 2012 08:59:48 +0100 Subject: [PATCH 1/3] [FEATURE] Added __call method to binding forms --- .../Component/Form/Tests/Fixtures/Magician.php | 17 +++++++++++++++++ .../Form/Tests/Util/PropertyPathTest.php | 9 +++++++++ .../Component/Form/Util/PropertyPath.php | 8 ++++++++ 3 files changed, 34 insertions(+) diff --git a/src/Symfony/Component/Form/Tests/Fixtures/Magician.php b/src/Symfony/Component/Form/Tests/Fixtures/Magician.php index cd66f29d079af..21b024d0adacd 100644 --- a/src/Symfony/Component/Form/Tests/Fixtures/Magician.php +++ b/src/Symfony/Component/Form/Tests/Fixtures/Magician.php @@ -24,4 +24,21 @@ public function __get($property) { return isset($this->$property) ? $this->$property : null; } + + public function __call($methodName, $args) + { + $returnValue = null; + $property = lcfirst(substr($methodName, 3)); + + switch (substr($methodName, 0, 3)) { + case 'get': + $returnValue = $this->__get($property); + break; + case 'set': + $returnValue = $this->__set($property, $args); + break; + } + + return $returnValue; + } } diff --git a/src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php b/src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php index 7642def026464..f503c2dc0de88 100644 --- a/src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php +++ b/src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php @@ -320,6 +320,15 @@ public function testSetValueUpdateMagicSet() $this->assertEquals('foobar', $object->__get('magicProperty')); } + public function testSetValueUpdateMagicCall() + { + $object = new Magician(); + + $object->setMagicProperty('foobar'); + + $this->assertEquals('foobar', $object->getMagicProperty()); + } + public function testSetValueUpdatesSetters() { $object = new Author(); diff --git a/src/Symfony/Component/Form/Util/PropertyPath.php b/src/Symfony/Component/Form/Util/PropertyPath.php index a5cf13befe70b..902b8f323c5bd 100644 --- a/src/Symfony/Component/Form/Util/PropertyPath.php +++ b/src/Symfony/Component/Form/Util/PropertyPath.php @@ -420,6 +420,10 @@ private function &readProperty(&$objectOrArray, $property, $isIndex) } $result[self::VALUE] = $objectOrArray->$hasser(); + } elseif ($reflClass->hasMethod('__call')) { + // needed to support magic method __call + $callMethod = 'get' . ucfirst($property); + $result[self::VALUE] = $objectOrArray->$callMethod(); } elseif ($reflClass->hasMethod('__get')) { // needed to support magic method __get $result[self::VALUE] = $objectOrArray->$property; @@ -536,6 +540,10 @@ private function writeProperty(&$objectOrArray, $property, $singular, $isIndex, } $objectOrArray->$setter($value); + } elseif ($reflClass->hasMethod('__call')) { + // needed to support magic method __call + $callMethod = 'set' . ucfirst($property); + $objectOrArray->$callMethod($value); } elseif ($reflClass->hasMethod('__set')) { // needed to support magic method __set $objectOrArray->$property = $value; From ad215c503954b50c886f37e2b6743bbbc2e7e54b Mon Sep 17 00:00:00 2001 From: Piotr Pasich Date: Wed, 19 Dec 2012 09:06:12 +0100 Subject: [PATCH 2/3] [FEATURE] Added tests to new __call method in property path --- .../Form/Tests/Fixtures/Magician.php | 16 ------- .../Form/Tests/Fixtures/MagicianCall.php | 45 +++++++++++++++++++ .../Form/Tests/Util/PropertyPathTest.php | 6 ++- 3 files changed, 49 insertions(+), 18 deletions(-) create mode 100644 src/Symfony/Component/Form/Tests/Fixtures/MagicianCall.php diff --git a/src/Symfony/Component/Form/Tests/Fixtures/Magician.php b/src/Symfony/Component/Form/Tests/Fixtures/Magician.php index 21b024d0adacd..d035ec4a954d3 100644 --- a/src/Symfony/Component/Form/Tests/Fixtures/Magician.php +++ b/src/Symfony/Component/Form/Tests/Fixtures/Magician.php @@ -25,20 +25,4 @@ public function __get($property) return isset($this->$property) ? $this->$property : null; } - public function __call($methodName, $args) - { - $returnValue = null; - $property = lcfirst(substr($methodName, 3)); - - switch (substr($methodName, 0, 3)) { - case 'get': - $returnValue = $this->__get($property); - break; - case 'set': - $returnValue = $this->__set($property, $args); - break; - } - - return $returnValue; - } } diff --git a/src/Symfony/Component/Form/Tests/Fixtures/MagicianCall.php b/src/Symfony/Component/Form/Tests/Fixtures/MagicianCall.php new file mode 100644 index 0000000000000..a21edd8d62e91 --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Fixtures/MagicianCall.php @@ -0,0 +1,45 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Form\Tests\Fixtures; + +class MagicianCall +{ + private $foobar; + + public function __call($methodName, $args) + { + $returnValue = null; + $property = lcfirst(substr($methodName, 3)); + + switch (substr($methodName, 0, 3)) { + case 'get': + $returnValue = $this->get($property); + break; + case 'set': + $returnValue = $this->set($property, $args); + break; + } + + return $returnValue; + } + + private function set($property, $value) + { + $this->$property = $value; + } + + private function get($property) + { + return isset($this->$property) ? $this->$property : null; + } + +} diff --git a/src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php b/src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php index f503c2dc0de88..c7d35cb7086bc 100644 --- a/src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php +++ b/src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php @@ -14,6 +14,7 @@ use Symfony\Component\Form\Util\PropertyPath; use Symfony\Component\Form\Tests\Fixtures\Author; use Symfony\Component\Form\Tests\Fixtures\Magician; +use Symfony\Component\Form\Tests\Fixtures\MagicianCall; class PropertyPathTest extends \PHPUnit_Framework_TestCase { @@ -322,9 +323,10 @@ public function testSetValueUpdateMagicSet() public function testSetValueUpdateMagicCall() { - $object = new Magician(); + $object = new MagicianCall(); - $object->setMagicProperty('foobar'); + $path = new PropertyPath('magicProperty'); + $path->setValue($object, 'foobar'); $this->assertEquals('foobar', $object->getMagicProperty()); } From e58d67b1c16386ac646beed44e61e508434b4b94 Mon Sep 17 00:00:00 2001 From: Piotr Pasich Date: Wed, 19 Dec 2012 09:47:48 +0100 Subject: [PATCH 3/3] Fixed tests for __call method --- src/Symfony/Component/Form/Tests/Fixtures/MagicianCall.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Symfony/Component/Form/Tests/Fixtures/MagicianCall.php b/src/Symfony/Component/Form/Tests/Fixtures/MagicianCall.php index a21edd8d62e91..80256cf279f97 100644 --- a/src/Symfony/Component/Form/Tests/Fixtures/MagicianCall.php +++ b/src/Symfony/Component/Form/Tests/Fixtures/MagicianCall.php @@ -17,8 +17,10 @@ class MagicianCall public function __call($methodName, $args) { + $returnValue = null; $property = lcfirst(substr($methodName, 3)); + $args = $this->getSimpleValue($args); switch (substr($methodName, 0, 3)) { case 'get': @@ -41,5 +43,10 @@ private function get($property) { return isset($this->$property) ? $this->$property : null; } + + private function getSimpleValue($args) + { + return count($args)==1 ? current($args) : $args; + } }