added validation
This commit is contained in:
parent
4195e8b993
commit
ca8a7ae753
1 changed files with 35 additions and 1 deletions
|
@ -23,6 +23,8 @@
|
|||
|
||||
namespace Volkszaehler\Model;
|
||||
|
||||
use Doctrine\ORM\Mapping;
|
||||
|
||||
use Volkszaehler\Interpreter;
|
||||
use Doctrine\ORM;
|
||||
use Doctrine\Common\Collections;
|
||||
|
@ -65,9 +67,39 @@ class Aggregator extends Entity {
|
|||
* @todo add bidrectional association
|
||||
*/
|
||||
public function addChild(Entity $child) {
|
||||
if ($this->children->contains($child)) {
|
||||
throw new \Exception('Entity is already a child of the group');
|
||||
}
|
||||
|
||||
if ($child instanceof Aggregator && $child->contains($this)) {
|
||||
throw new \Exception('Recursion detected! Can\'t group to itself');
|
||||
}
|
||||
|
||||
$this->children->add($child);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if aggregator contains given entity
|
||||
*
|
||||
* @param Entity $entity
|
||||
* @param boolean $recursive should we search recursivly?
|
||||
*/
|
||||
protected function contains(Entity $entity, $recursive = FALSE) {
|
||||
if ($this->children->contains($entity)) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
if ($recursive) {
|
||||
foreach ($this->children as $child) {
|
||||
if ($child->contains($entity, $recursive)) {
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove child from group
|
||||
*
|
||||
|
@ -76,7 +108,9 @@ class Aggregator extends Entity {
|
|||
* @todo add bidrectional association
|
||||
*/
|
||||
public function removeChild(Entity $child) {
|
||||
return $this->children->removeElement($child);
|
||||
if (!$this->children->removeElement($child)) {
|
||||
throw new \Exception('This entity is not a child of this group');
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
Loading…
Add table
Reference in a new issue