Skip to content

[Form] Add magic getter method support to Entities #5309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Symfony/Component/Form/Tests/Fixtures/Magician.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ public function __get($property)
{
return isset($this->$property) ? $this->$property : null;
}

public function __call($method, $arguments) {
if (preg_match('/^(get|is|has)([A-Z].*)$/', $method, $matches)) {
return $this->{lcfirst($matches[2])};
}
}
}
24 changes: 24 additions & 0 deletions src/Symfony/Component/Form/Tests/Util/PropertyPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,30 @@ public function testSetValueUpdatesPropertiesWithCustomPropertyPath()
$this->assertEquals('Bernhard', $object->child['index']->firstName);
}

public function testSetValueUpdateMagicGetMethod()
{
$object = new Magician();
$path = new PropertyPath('magicProperty');
$path->setValue($object, 'foobar');
$this->assertEquals($path->getValue($object), $object->getMagicProperty());
}

public function testSetValueUpdateMagicHasMethod()
{
$object = new Magician();
$path = new PropertyPath('magicProperty');
$path->setValue($object, 'foobar');
$this->assertEquals($path->getValue($object), $object->hasMagicProperty());
}

public function testSetValueUpdateMagicIsMethod()
{
$object = new Magician();
$path = new PropertyPath('magicProperty');
$path->setValue($object, 'foobar');
$this->assertEquals($path->getValue($object), $object->isMagicProperty());
}

public function testSetValueUpdateMagicSet()
{
$object = new Magician();
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Form/Util/PropertyPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,12 @@ private function &readProperty(&$objectOrArray, $property, $isIndex)
// needed to support \stdClass instances
$result[self::VALUE] =& $objectOrArray->$property;
$result[self::IS_REF] = true;
} elseif (is_callable(array($objectOrArray, $getter))) {
$result[self::VALUE] = $objectOrArray->$getter();
} elseif (is_callable(array($objectOrArray, $isser))) {
$result[self::VALUE] = $objectOrArray->$isser();
} elseif (is_callable(array($objectOrArray, $hasser))) {
$result[self::VALUE] = $objectOrArray->$hasser();
} else {
throw new InvalidPropertyException(sprintf('Neither property "%s" nor method "%s()" nor method "%s()" exists in class "%s"', $property, $getter, $isser, $reflClass->name));
}
Expand Down