Skip to content

Commit

Permalink
Merge pull request nelmio#64 from thewilkybarkid/protected-setter
Browse files Browse the repository at this point in the history
Make sure a setter is public before invoking it
  • Loading branch information
Seldaek committed Oct 24, 2013
2 parents c829c75 + 8b3ab62 commit ebfba39
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Nelmio/Alice/Loader/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,13 @@ private function populateObject($instance, $class, $name, $data)
$variables[$key] = $generatedVal;
} elseif (method_exists($instance, 'set'.$key)) {
$generatedVal = $this->checkTypeHints($instance, 'set'.$key, $generatedVal);
$instance->{'set'.$key}($generatedVal);
if(!is_callable(array($instance, 'set'.$key))) {
$refl = new \ReflectionMethod($instance, 'set'.$key);
$refl->setAccessible(true);
$refl->invoke($instance, $generatedVal);
} else {
$instance->{'set'.$key}($generatedVal);
}
$variables[$key] = $generatedVal;
} elseif (property_exists($instance, $key)) {
$refl = new \ReflectionProperty($instance, $key);
Expand Down
14 changes: 14 additions & 0 deletions tests/Nelmio/Alice/Loader/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ public function testLoadAssignsDataToSetters()
$this->assertEquals('group', $group->getName());
}

public function testLoadAssignsDataToNonPublicSetters()
{
$res = $this->loadData(array(
self::GROUP => array(
'a' => array(
'sortName' => 'group'
),
),
));

$group = $res[0];
$this->assertEquals('group', $group->getSortName());
}

public function testLoadAddsReferencesToAdders()
{
$res = $this->loadData(array(
Expand Down
10 changes: 10 additions & 0 deletions tests/Nelmio/Alice/fixtures/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class Group
{
private $name;
private $sortName;
private $owner;
private $members = array();
private $creationDate;
Expand All @@ -23,6 +24,15 @@ public function setName($name)
$this->name = $name;
}

public function getSortName()
{
return $this->sortName;
}

protected function setSortName($sortName) {
$this->sortName = $sortName;
}

public function getOwner()
{
return $this->owner;
Expand Down

0 comments on commit ebfba39

Please sign in to comment.