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

@@ -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;
}