mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2025-01-09 14:07:25 +01:00
Implemented dimension helper
This commit is contained in:
parent
61186470da
commit
0cd09258e7
@ -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
|
||||
|
97
application/src/Helper/Dimension.php
Normal file
97
application/src/Helper/Dimension.php
Normal 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();
|
||||
}
|
||||
}
|
||||
|
10
application/src/Helper/DimensionInterface.php
Normal file
10
application/src/Helper/DimensionInterface.php
Normal 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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user