Implemented dimension helper

This commit is contained in:
Kevin Frantz 2018-11-01 21:48:33 +01:00
parent 61186470da
commit 0cd09258e7
3 changed files with 110 additions and 12 deletions

View File

@ -4,6 +4,7 @@ namespace App\Entity\Attribut;
use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\ArrayCollection;
use App\Helper\Dimension;
/** /**
* @author kevinfrantz * @author kevinfrantz
@ -33,18 +34,8 @@ trait MembersAttribut
*/ */
public function getMembersIncludingChildren(?int $dimension = null, Collection $members = null): Collection public function getMembersIncludingChildren(?int $dimension = null, Collection $members = null): Collection
{ {
$dimension = is_int($dimension) ? $dimension - 1 : null; $dimensionHelper = new Dimension(__FUNCTION__, MembersAttributInterface::class, $this, 'members');
$members = $members ?? new ArrayCollection(); return $dimensionHelper->getDimensions($dimension,$members);
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;
} }
private function continueIncludeMembersLoop(?int $dimension): bool private function continueIncludeMembersLoop(?int $dimension): bool

View File

@ -0,0 +1,97 @@
<?php
namespace App\Helper;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
/**
* Helps to get all Elements till a special dimension
* @author kevinfrantz
*
*/
final class Dimension implements DimensionInterface
{
/**
*
* @var string
*/
private $attribut;
/**
* @var string
*/
private $method;
/**
*
* @var string
*/
private $interface;
/**
*
* @var object
*/
private $object;
/**
* @var int|null the actual dimension to which the class points
*/
private $dimension = null;
/**
* @param string $method which uses the dimension helper
* @param string $interface which is implemented in all classes which have dimensions
* @param object $object which calls the dimension helper
* @param string $attribut which represents dimensions
*/
public function __construct(string $method,string $interface, object $object,string $attribut) {
$this->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();
}
}

View File

@ -0,0 +1,10 @@
<?php
namespace App\Helper;
use Doctrine\Common\Collections\Collection;
interface DimensionInterface
{
public function getDimensions(?int $dimension = null, Collection $elements = null): Collection;
}