add criteria when fetching node children

This commit is contained in:
Simon Vieille 2021-07-12 14:21:20 +02:00
parent e7baa477bb
commit e0d77dc17e

View file

@ -225,12 +225,30 @@ class Node implements EntityInterface
/** /**
* @return Collection|Node[] * @return Collection|Node[]
*/ */
public function getChildren(): Collection public function getChildren(array $criteria = []): Collection
{ {
if (null === $this->children) { if (null === $this->children) {
$this->children = new ArrayCollection(); $this->children = new ArrayCollection();
} }
if (!empty($criteria)) {
$children = new ArrayCollection();
foreach ($this->children as $child) {
$add = true;
if (isset($criteria['visible']) && $child->getIsVisible() !== $criteria['visible']) {
$add = false;
}
if ($add) {
$children->add($child);
}
}
return $children;
}
return $this->children; return $this->children;
} }
@ -256,7 +274,7 @@ class Node implements EntityInterface
return $this; return $this;
} }
public function getAllChildren(): ArrayCollection public function getAllChildren(array $criteria = []): ArrayCollection
{ {
$children = []; $children = [];
@ -274,6 +292,14 @@ class Node implements EntityInterface
return $a->getTreeLeft() < $b->getTreeLeft() ? -1 : 1; return $a->getTreeLeft() < $b->getTreeLeft() ? -1 : 1;
}); });
if (!empty($criteria)) {
foreach ($children as $key => $child) {
if (isset($criteria['visible']) && $child->getIsVisible() !== $criteria['visible']) {
unset($children[$key]);
}
}
}
return new ArrayCollection($children); return new ArrayCollection($children);
} }