Optimized dom management

This commit is contained in:
Kevin Frantz 2019-02-25 16:38:54 +01:00
parent 0b50b1df9a
commit be9e22577c
13 changed files with 259 additions and 9 deletions

View File

@ -4,6 +4,8 @@ namespace Infinito\Attribut;
/**
* @author kevinfrantz
*
* @see GrantAttributInterface
*/
trait GrantAttribut
{
@ -12,11 +14,17 @@ trait GrantAttribut
*/
protected $grant;
/**
* @param bool $grant
*/
public function setGrant(bool $grant): void
{
$this->grant = $grant;
}
/**
* @return bool
*/
public function getGrant(): bool
{
return $this->grant;

View File

@ -7,7 +7,18 @@ namespace Infinito\Attribut;
*/
interface GrantAttributInterface
{
/**
* @var string
*/
const GRANT_ATTRIBUT_NAME = 'grant';
/**
* @param bool $grant
*/
public function setGrant(bool $grant): void;
/**
* @return bool
*/
public function getGrant(): bool;
}

View File

@ -3,6 +3,7 @@
namespace Infinito\Attribut;
use Doctrine\Common\Collections\Collection;
use Infinito\Entity\Meta\RightInterface;
/**
* @author kevinfrantz
@ -16,11 +17,17 @@ trait RightsAttribut
*/
protected $rights;
/**
* @param Collection|RightInterface[] $rights
*/
public function setRights(Collection $rights): void
{
$this->rights = $rights;
}
/**
* @return Collection|RightInterface[]
*/
public function getRights(): Collection
{
return $this->rights;

View File

@ -10,6 +10,9 @@ use Infinito\Entity\Meta\RightInterface;
*/
interface RightsAttributInterface
{
/**
* @var string
*/
const RIGHTS_ATTRIBUT_NAME = 'rights';
/**

View File

@ -6,6 +6,8 @@ use Infinito\Entity\Source\SourceInterface;
/**
* @author kevinfrantz
*
* @see SourceAttributInterface
*/
trait SourceAttribut
{
@ -14,11 +16,17 @@ trait SourceAttribut
*/
protected $source;
/**
* @return SourceInterface
*/
public function getSource(): SourceInterface
{
return $this->source;
}
/**
* @param SourceInterface $source
*/
public function setSource(SourceInterface $source): void
{
$this->source = $source;

View File

@ -9,7 +9,18 @@ use Infinito\Entity\Source\SourceInterface;
*/
interface SourceAttributInterface
{
/**
* @var string
*/
const SOURCE_ATTRIBUT_NAME = 'source';
/**
* @return SourceInterface
*/
public function getSource(): SourceInterface;
/**
* @param SourceInterface $source
*/
public function setSource(SourceInterface $source): void;
}

View File

@ -3,12 +3,12 @@
namespace Infinito\Domain\DomManagement;
use Infinito\Entity\EntityInterface;
use Infinito\Domain\LayerManagement\LayerClassMap;
use Infinito\Domain\RightManagement\RightTransformerService;
use Doctrine\Common\Collections\Collection;
use Infinito\Domain\RequestManagement\Entity\RequestedEntityServiceInterface;
use Infinito\Exception\NotCorrectInstanceException;
use Infinito\DBAL\Types\Meta\Right\LayerType;
use Infinito\Domain\LayerManagement\LayerInterfaceMap;
/**
* This class is not ready and not tested!
@ -112,13 +112,13 @@ final class EntityDomService implements EntityDomServiceInterface
}
/**
* @param mixed $value
* @param \DOMElement $domElement
* @param mixed|EntityInterface $value
* @param \DOMElement $domElement
*/
private function mappValue($value, \DOMElement $domElement): void
{
foreach (LayerClassMap::LAYER_CLASS_MAP as $layer => $class) {
if ($value instanceof $class) {
foreach (LayerInterfaceMap::getAllInterfaces() as $layer => $interface) {
if ($value instanceof $interface) {
$domElement->setAttribute('layer', $layer);
$domElement->setAttribute('id', $value->getId());
$domElement->setAttribute('name', LayerType::getReadableValue($layer));
@ -129,6 +129,7 @@ final class EntityDomService implements EntityDomServiceInterface
if (is_object($value)) {
throw new NotCorrectInstanceException('The instance '.get_class($value).' is not supported!');
}
$domElement->setAttribute('type', gettype($value));
$domElement->setAttribute('value', $value);
return;
@ -176,7 +177,7 @@ final class EntityDomService implements EntityDomServiceInterface
$value = $this->getPropertyValue($propertyName);
if ($value instanceof Collection) {
foreach ($value as $valueElement) {
$domSubElement = $domElement->createElement('list-element');
$domSubElement = $this->domDocument->createElement('list-element');
$domElement->setAttribute('name', $propertyName);
$this->mappValue($valueElement, $domSubElement);
$domElement->appendChild($domSubElement);

View File

@ -0,0 +1,77 @@
<?php
namespace Infinito\Domain\LayerManagement;
use Infinito\DBAL\Types\Meta\Right\LayerType;
/**
* @author kevinfrantz
*
* @todo for performance reasons it could be helpfull to implement lazy loading in the future
*/
final class LayerInterfaceMap implements LayerInterfaceMapInterface
{
/**
* @var string The abstract class prefix, which will be removed from the interface name
*/
const ABSTRACT_CLASS_PREFIX = 'Abstract';
/**
* @var string The suffix which will be added to the interface name
*/
const INTERFACE_SUFFIX = 'Interface';
/**
* @param string $shortClass
*
* @return string
*/
private static function filterAbstractClassName(string $shortClass): string
{
if (self::ABSTRACT_CLASS_PREFIX === substr($shortClass, 0, strlen(self::ABSTRACT_CLASS_PREFIX))) {
return substr($shortClass, strlen(self::ABSTRACT_CLASS_PREFIX));
}
return $shortClass;
}
/**
* @param string $class
*
* @return string
*/
private static function addInterfaceSuffix(string $class): string
{
return $class.self::INTERFACE_SUFFIX;
}
/**
* @param string $layer
*
* @return string
*/
public static function getInterface(string $layer): string
{
$className = LayerClassMap::getClass($layer);
$elements = explode('\\', $className);
$shortClass = $elements[count($elements) - 1];
$filteredAbstractClass = self::filterAbstractClassName($shortClass);
$elements[count($elements) - 1] = self::addInterfaceSuffix($filteredAbstractClass);
$interfaceName = implode('\\', $elements);
return $interfaceName;
}
/**
* @return array
*/
public static function getAllInterfaces(): array
{
$allInterfaces = [];
foreach (LayerType::getValues() as $layer) {
$allInterfaces[$layer] = self::getInterface($layer);
}
return $allInterfaces;
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace Infinito\Domain\LayerManagement;
/**
* @author kevinfrantz
*/
interface LayerInterfaceMapInterface
{
/**
* @return array|string[] All layer interfaces
*/
public static function getAllInterfaces(): array;
/**
* @param string $layer
*
* @return string The interface which belongs to an Layer
*/
public static function getInterface(string $layer): string;
}

View File

@ -5,7 +5,6 @@ namespace Infinito\Entity\Meta;
use Doctrine\ORM\Mapping as ORM;
use Infinito\Attribut\RightsAttribut;
use Doctrine\Common\Collections\ArrayCollection;
use Infinito\Attribut\RelationAttribut;
use Infinito\Entity\Source\SourceInterface;
use Infinito\Attribut\GrantAttribut;
@ -16,7 +15,7 @@ use Infinito\Attribut\GrantAttribut;
*/
class Law extends AbstractMeta implements LawInterface
{
use RightsAttribut, RelationAttribut, GrantAttribut;
use RightsAttribut, GrantAttribut;
/**
* @ORM\Column(type="boolean",name="`grant`")

View File

@ -14,7 +14,7 @@
{% if id and layer %}
<a href="{{ path('infinito_api_rest_layer_read',{'identity':id,'layer':layer,'_format':'html'}) }}">
<i class="fas fa-link"></i>
{% if id == content %}#{% endif %}{{ content }}
#{{ id }}
</a>
{% else %}
{{ content }}

View File

@ -11,6 +11,16 @@ use Infinito\Attribut\SlugAttributInterface;
use Infinito\Attribut\IdAttributInterface;
use Infinito\DBAL\Types\Meta\Right\LayerType;
use Infinito\Attribut\VersionAttributInterface;
use Infinito\Entity\Meta\Law;
use Infinito\Entity\Source\SourceInterface;
use Infinito\Entity\Meta\RightInterface;
use Infinito\Attribut\SourceAttributInterface;
use Infinito\Attribut\GrantAttributInterface;
use Infinito\Attribut\RightsAttributInterface;
use Infinito\Entity\EntityInterface;
use Infinito\Attribut\VersionAttribut;
use Infinito\Attribut\IdAttribut;
use Infinito\Exception\NotCorrectInstanceException;
/**
* @author kevinfrantz
@ -33,6 +43,29 @@ class EntityDomServiceTest extends TestCase
$this->entityDomService = new EntityDomService($this->requestedEntityService);
}
public function testNotCorrectInstanceException(): void
{
$entity = new class() implements EntityInterface {
use VersionAttribut,IdAttribut;
private $test;
public function setTest($test): void
{
$this->test = $test;
}
public function getTest()
{
return $this->test;
}
};
$entity->setTest(new class() {
});
$this->requestedEntityService->method('getEntity')->willReturn($entity);
$this->expectException(NotCorrectInstanceException::class);
$this->entityDomService->getDomDocument();
}
public function testAbstractSource(): void
{
$slug = 'test';
@ -66,4 +99,56 @@ class EntityDomServiceTest extends TestCase
}
}
}
public function testLaw(): void
{
$source = $this->createMock(SourceInterface::class);
$source->method('getId')->willReturn(123);
$source->method('hasId')->willReturn(true);
$right = $this->createMock(RightInterface::class);
$right->method('getId')->willReturn(124);
$right->method('hasId')->willReturn(true);
$id = 12345;
$law = new Law();
$law->setId($id);
$law->setSource($source);
$law->getRights()->add($right);
$law->getRights()->add(clone $right);
$law->getRights()->add(clone $right);
$this->requestedEntityService->method('getEntity')->willReturn($law);
$result = $this->entityDomService->getDomDocument();
foreach ($result->childNodes as $attribut) {
$name = $attribut->getAttribute('name');
$value = $attribut->getAttribute('value');
$id = $attribut->getAttribute('id');
$layer = $attribut->getAttribute('layer');
$type = $attribut->getAttribute('type');
switch ($name) {
case IdAttributInterface::ID_ATTRIBUT_NAME:
$this->assertGreaterThan(0, $value);
break;
case VersionAttributInterface::VERSION_ATTRIBUT_NAME:
$this->assertGreaterThan(-1, $value);
break;
case SourceAttributInterface::SOURCE_ATTRIBUT_NAME:
$this->assertGreaterThan(0, $id);
$this->assertEquals(LayerType::SOURCE, $layer);
break;
case GrantAttributInterface::GRANT_ATTRIBUT_NAME:
$this->assertEquals('', $value);
$this->assertEquals('boolean', $type);
break;
case RightsAttributInterface::RIGHTS_ATTRIBUT_NAME:
$rights = $attribut->childNodes;
$this->assertCount(3, $rights);
foreach ($rights as $rightAttribut) {
$this->assertEquals(LayerType::RIGHT, $rightAttribut->getAttribute('layer'));
$this->assertGreaterThan(0, $rightAttribut->getAttribute('id'));
}
break;
default:
throw new \Exception("No assert was defined for attribut with name <<$name>>!");
}
}
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace tests\Unit\Domain\LayerManagement;
use PHPUnit\Framework\TestCase;
use Infinito\Domain\LayerManagement\LayerInterfaceMap;
/**
* @author kevinfrantz
*/
class LayerInterfaceMapTest extends TestCase
{
public function testIfInterfaceForEachLayerExist(): void
{
foreach (LayerInterfaceMap::getAllInterfaces() as $interface) {
$this->assertTrue(interface_exists($interface));
}
}
}