Skip to content

Commit

Permalink
[Form] Added support for __get and __set in PropertyPath
Browse files Browse the repository at this point in the history
  • Loading branch information
julesbou authored and fabpot committed Nov 16, 2010
1 parent 5aeb358 commit 23ac47e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/PropertyPath.php
Expand Up @@ -324,7 +324,7 @@ protected function readProperty($object, $currentIndex)
}

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 {
Expand Down Expand Up @@ -367,7 +367,7 @@ protected function updateProperty(&$objectOrArray, $currentIndex, $value)
}

$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 {
Expand Down
18 changes: 18 additions & 0 deletions tests/Symfony/Tests/Component/Form/Fixtures/Magician.php
@@ -0,0 +1,18 @@
<?php

namespace Symfony\Tests\Component\Form\Fixtures;

class Magician
{
private $properties = array();

public function __set($name, $value)
{
$this->properties[$name] = $value;
}

public function __get($name)
{
return isset($this->properties[$name]) ? $this->properties[$name] : null;
}
}
22 changes: 22 additions & 0 deletions tests/Symfony/Tests/Component/Form/PropertyPathTest.php
Expand Up @@ -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
{
Expand Down Expand Up @@ -135,6 +137,16 @@ public function testGetValueReadsIssers()
$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');
Expand Down Expand Up @@ -205,6 +217,16 @@ public function testSetValueUpdatesArrayAccess()
$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]');
Expand Down

0 comments on commit 23ac47e

Please sign in to comment.