diff --git a/application/src/Entity/Attribut/MembersAttribut.php b/application/src/Entity/Attribut/MembersAttribut.php index 3a35cf8..3c3529f 100644 --- a/application/src/Entity/Attribut/MembersAttribut.php +++ b/application/src/Entity/Attribut/MembersAttribut.php @@ -4,6 +4,7 @@ namespace App\Entity\Attribut; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ArrayCollection; +use App\Helper\Dimension; /** * @author kevinfrantz @@ -33,18 +34,8 @@ trait MembersAttribut */ public function getMembersIncludingChildren(?int $dimension = null, Collection $members = null): Collection { - $dimension = is_int($dimension) ? $dimension - 1 : null; - $members = $members ?? new ArrayCollection(); - foreach ($this->members->toArray() as $member) { - if (!$members->contains($member)) { - $members->add($member); - if ($this->continueIncludeMembersLoop($dimension) && $member instanceof MembersAttributInterface) { - $member->getMembersIncludingChildren($dimension, $members); - } - } - } - - return $members; + $dimensionHelper = new Dimension(__FUNCTION__, MembersAttributInterface::class, $this, 'members'); + return $dimensionHelper->getDimensions($dimension,$members); } private function continueIncludeMembersLoop(?int $dimension): bool diff --git a/application/src/Helper/Dimension.php b/application/src/Helper/Dimension.php new file mode 100644 index 0000000..e76d3fc --- /dev/null +++ b/application/src/Helper/Dimension.php @@ -0,0 +1,97 @@ +method = $method; + $this->interface = $interface; + $this->object = $object; + $this->attribut = $attribut; + } + + /** + * @param int $dimension The dimensions start with 1 for the elements of the actuall dimension and NULL for all elements + * @param Collection $elements A elements collection, to which new elements should be add + * + * @return Collection Returns all elements till the defined dimension + */ + public function getDimensions(?int $dimension = null, Collection $elements = null): Collection + { + $this->setDimension($dimension); + $elements = $elements ?? new ArrayCollection(); + foreach ($this->object->{$this->attributGetterName()}()->toArray() as $element) { + if (!$elements->contains($element)) { + $elements->add($element); + if ($this->continueLoop() && $element instanceof $this->interface) { + $element->{$this->method}($this->dimension, $elements); + } + } + } + return $elements; + } + + private function setDimension(?int $dimension):void{ + $this->dimension = is_int($dimension) ? $dimension - 1 : null; + } + + private function attributGetterName():string{ + return 'get'.ucfirst($this->attribut); + } + + private function includeInfiniteDimensions():bool{ + return is_null($this->dimension); + } + + private function isNotLastDimension():bool{ + return $this->dimension > 0; + } + + private function continueLoop(): bool + { + return $this->includeInfiniteDimensions() || $this->isNotLastDimension(); + } +} + diff --git a/application/src/Helper/DimensionInterface.php b/application/src/Helper/DimensionInterface.php new file mode 100644 index 0000000..cefa3eb --- /dev/null +++ b/application/src/Helper/DimensionInterface.php @@ -0,0 +1,10 @@ +