Skip to content

Commit

Permalink
MagicAccessors: fix incorrect handling of method names [Fixes #227] [C…
Browse files Browse the repository at this point in the history
…loses #228]
  • Loading branch information
lookyman authored and fprochazka committed Mar 28, 2016
1 parent cc4a3e8 commit ae0f00d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
25 changes: 25 additions & 0 deletions src/Kdyby/Doctrine/Entities/MagicAccessors.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ public function __call($name, $args)

return $this;

} elseif (substr($prop, -1) === 's' && isset($properties[$prop = substr($prop, 0, -1) . 'ses'])) {
if (!$this->$prop instanceof Collection) {
throw UnexpectedValueException::notACollection($this, $prop);
}

$this->$prop->add($args[0]);

return $this;

} elseif (isset($properties[$prop])) {
throw UnexpectedValueException::notACollection($this, $prop);
}
Expand All @@ -152,6 +161,13 @@ public function __call($name, $args)

return $this->$prop->contains($args[0]);

} elseif (substr($prop, -1) === 's' && isset($properties[$prop = substr($prop, 0, -1) . 'ses'])) {
if (!$this->$prop instanceof Collection) {
throw UnexpectedValueException::notACollection($this, $prop);
}

return $this->$prop->contains($args[0]);

} elseif (isset($properties[$prop])) {
throw UnexpectedValueException::notACollection($this, $prop);
}
Expand All @@ -177,6 +193,15 @@ public function __call($name, $args)

return $this;

} elseif (substr($prop, -1) === 's' && isset($properties[$prop = substr($prop, 0, -1) . 'ses'])) {
if (!$this->$prop instanceof Collection) {
throw UnexpectedValueException::notACollection($this, $prop);
}

$this->$prop->removeElement($args[0]);

return $this;

} elseif (isset($properties[$prop])) {
throw UnexpectedValueException::notACollection($this, $prop);
}
Expand Down
26 changes: 24 additions & 2 deletions tests/KdybyTests/Doctrine/MagicAccesors.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,23 @@ class MagicAccessorsTest extends Tester\TestCase
Assert::same(2, $entity->getRealSomething());
}



public function testPluralAccessor()
{
$entity = new BadlyNamedEntity();
Assert::false($entity->hasBus(1));

$entity->addBus(1);
$entity->addBus(2);
Assert::true($entity->hasBus(1));

$entity->removeBus(1);
Assert::false($entity->hasBus(1));

Assert::true($entity->hasBus(2));
}

}


Expand Down Expand Up @@ -425,6 +442,11 @@ class BadlyNamedEntity
*/
protected $four;

/**
* @var \Doctrine\Common\Collections\ArrayCollection
*/
protected $buses;

/**
* @var object
*/
Expand Down Expand Up @@ -457,8 +479,6 @@ class BadlyNamedEntity



/**
*/
public function __construct()
{
$this->one = (object) ['id' => 1];
Expand All @@ -469,6 +489,8 @@ class BadlyNamedEntity
$this->twos = new ArrayCollection([(object) ['id' => 2]]);
$this->proxies = new ArrayCollection([(object) ['id' => 3]]);
$this->threes = new ArrayCollection([(object) ['id' => 4]]);

$this->buses = new ArrayCollection();
}


Expand Down

0 comments on commit ae0f00d

Please sign in to comment.