Optimized EntityDomService

This commit is contained in:
Kevin Frantz
2019-02-25 10:05:42 +01:00
parent 4385e84cc7
commit d0adc9b42a
18 changed files with 109 additions and 17 deletions

View File

@@ -6,6 +6,8 @@ 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;
/**
* @author kevinfrantz
@@ -22,6 +24,11 @@ final class EntityDomService implements EntityDomServiceInterface
*/
const HAS_METHOD = RightTransformerService::HAS_PREFIX;
/**
* @var RequestedEntityServiceInterface
*/
private $requestedEntity;
/**
* @var EntityInterface
*/
@@ -42,6 +49,14 @@ final class EntityDomService implements EntityDomServiceInterface
*/
private $domDocument;
/**
* @param RequestedEntityServiceInterface $requestedEntity
*/
public function __construct(RequestedEntityServiceInterface $requestedEntity)
{
$this->requestedEntity = $requestedEntity;
}
private function createProperties(): void
{
$this->properties = $this->entityReflectionClass->getProperties();
@@ -84,8 +99,9 @@ final class EntityDomService implements EntityDomServiceInterface
return null;
}
$getMethod = $this->getMethodName(self::GET_METHOD, $propertyName);
$result = $this->getMethodResult($getMethod);
return $this->getMethodResult($getMethod);
return $result;
}
/**
@@ -97,11 +113,15 @@ final class EntityDomService implements EntityDomServiceInterface
foreach (LayerClassMap::LAYER_CLASS_MAP as $layer => $class) {
if ($value instanceof $class) {
$domElement->setAttribute('layer', $layer);
$domElement->setAttribute('id', $value->getId());
$domElement->setAttribute('value', $value->getId());
return;
}
}
if (is_object($value)) {
throw new NotCorrectInstanceException('The instance '.get_class($value).' is not supported!');
}
$domElement->setAttribute('value', $value);
return;
@@ -135,25 +155,32 @@ final class EntityDomService implements EntityDomServiceInterface
*
* @see \Infinito\Domain\DomManagement\EntityDomServiceInterface::getDomDocument()
*/
public function getDomDocument(EntityInterface $entity): \DOMDocument
public function getDomDocument(): \DOMDocument
{
$this->entity = $this->requestedEntity->getEntity();
$this->entityReflectionClass = new \ReflectionClass($this->entity);
$this->createProperties();
$this->createDomDocument();
foreach ($this->properties as $property) {
$propertyName = $property->getName();
if ($this->isPropertyAccessible($propertyName)) {
$domElement = $this->domDocument->createElement($propertyName);
$domElement = $this->domDocument->createElement('attribut');
$domElement->setAttribute('name', $propertyName);
$value = $this->getPropertyValue($propertyName);
if ($value instanceof Collection) {
foreach ($value as $valueElement) {
$domSubElement = $domElement->createElement('list-element');
$domElement->setAttribute('name', $propertyName);
$this->mappValue($valueElement, $domSubElement);
$domElement->appendChild($domSubElement);
}
} else {
$this->mappValue($value, $domElement);
}
$this->domDocument->appendChild($domElement);
}
}
$this->domDocument->saveXML();
return $this->domDocument;
}

View File

@@ -2,8 +2,6 @@
namespace Infinito\Domain\DomManagement;
use Infinito\Entity\EntityInterface;
/**
* Allows to build a DOM for an entity.
*
@@ -14,9 +12,7 @@ use Infinito\Entity\EntityInterface;
interface EntityDomServiceInterface
{
/**
* @param EntityInterface $entity
*
* @return \DOMDocument
*/
public function getDomDocument(EntityInterface $entity): \DOMDocument;
public function getDomDocument(): \DOMDocument;
}