diff --git a/src/Symfony/Component/Form/Tests/Fixtures/Magician.php b/src/Symfony/Component/Form/Tests/Fixtures/Magician.php index cd66f29d079af..d035ec4a954d3 100644 --- a/src/Symfony/Component/Form/Tests/Fixtures/Magician.php +++ b/src/Symfony/Component/Form/Tests/Fixtures/Magician.php @@ -24,4 +24,5 @@ public function __get($property) { return isset($this->$property) ? $this->$property : null; } + } 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..80256cf279f97 --- /dev/null +++ b/src/Symfony/Component/Form/Tests/Fixtures/MagicianCall.php @@ -0,0 +1,52 @@ + + * + * 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)); + $args = $this->getSimpleValue($args); + + 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; + } + + private function getSimpleValue($args) + { + return count($args)==1 ? current($args) : $args; + } + +} diff --git a/src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php b/src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php index 7642def026464..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 { @@ -320,6 +321,16 @@ public function testSetValueUpdateMagicSet() $this->assertEquals('foobar', $object->__get('magicProperty')); } + public function testSetValueUpdateMagicCall() + { + $object = new MagicianCall(); + + $path = new PropertyPath('magicProperty'); + $path->setValue($object, '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;