From 23ac47e0119f783b4396ebeaf682aaa465916caa Mon Sep 17 00:00:00 2001 From: GordonsLondon Date: Tue, 9 Nov 2010 12:18:19 +0100 Subject: [PATCH] [Form] Added support for __get and __set in PropertyPath --- src/Symfony/Component/Form/PropertyPath.php | 4 ++-- .../Component/Form/Fixtures/Magician.php | 18 +++++++++++++++ .../Tests/Component/Form/PropertyPathTest.php | 22 +++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 tests/Symfony/Tests/Component/Form/Fixtures/Magician.php diff --git a/src/Symfony/Component/Form/PropertyPath.php b/src/Symfony/Component/Form/PropertyPath.php index 99a9ab7ccd..a23288efd8 100644 --- a/src/Symfony/Component/Form/PropertyPath.php +++ b/src/Symfony/Component/Form/PropertyPath.php @@ -324,7 +324,7 @@ class PropertyPath implements \IteratorAggregate } return $object->$property; - } else if (property_exists($object, $property)) { + } else if (property_exists($object, $property) || $reflClass->hasMethod('__get')) { // needed to support \stdClass instances return $object->$property; } else { @@ -367,7 +367,7 @@ class PropertyPath implements \IteratorAggregate } $objectOrArray->$property = $value; - } else if (property_exists($objectOrArray, $property)) { + } else if (property_exists($objectOrArray, $property) || $reflClass->hasMethod('__get')) { // needed to support \stdClass instances $objectOrArray->$property = $value; } else { diff --git a/tests/Symfony/Tests/Component/Form/Fixtures/Magician.php b/tests/Symfony/Tests/Component/Form/Fixtures/Magician.php new file mode 100644 index 0000000000..e924150cae --- /dev/null +++ b/tests/Symfony/Tests/Component/Form/Fixtures/Magician.php @@ -0,0 +1,18 @@ +properties[$name] = $value; + } + + public function __get($name) + { + return isset($this->properties[$name]) ? $this->properties[$name] : null; + } +} \ No newline at end of file diff --git a/tests/Symfony/Tests/Component/Form/PropertyPathTest.php b/tests/Symfony/Tests/Component/Form/PropertyPathTest.php index 282d327aa3..bb03b247ad 100644 --- a/tests/Symfony/Tests/Component/Form/PropertyPathTest.php +++ b/tests/Symfony/Tests/Component/Form/PropertyPathTest.php @@ -3,9 +3,11 @@ namespace Symfony\Tests\Component\Form; require_once __DIR__ . '/Fixtures/Author.php'; +require_once __DIR__ . '/Fixtures/Magician.php'; use Symfony\Component\Form\PropertyPath; use Symfony\Tests\Component\Form\Fixtures\Author; +use Symfony\Tests\Component\Form\Fixtures\Magician; class PropertyPathTest extends \PHPUnit_Framework_TestCase { @@ -135,6 +137,16 @@ class PropertyPathTest extends \PHPUnit_Framework_TestCase $this->assertSame(false, $path->getValue($object)); } + public function testGetValueReadsMagicGet() + { + $path = new PropertyPath('magicProperty'); + + $object = new Magician(); + $object->__set('magicProperty', 'foobar'); + + $this->assertSame('foobar', $path->getValue($object)); + } + public function testGetValueThrowsExceptionIfIsserIsNotPublic() { $path = new PropertyPath('privateIsser'); @@ -205,6 +217,16 @@ class PropertyPathTest extends \PHPUnit_Framework_TestCase $this->assertEquals('Bernhard', $object['firstName']); } + public function testSetValueUpdateMagicSet() + { + $object = new Magician(); + + $path = new PropertyPath('magicProperty'); + $path->setValue($object, 'foobar'); + + $this->assertEquals('foobar', $object->__get('magicProperty')); + } + public function testSetValueThrowsExceptionIfArrayAccessExpected() { $path = new PropertyPath('[firstName]');