Skip to content

Commit

Permalink
feat: WebResponseDataTransformer must always transform PersistableInt…
Browse files Browse the repository at this point in the history
…erface
  • Loading branch information
ambroisemaupate committed Feb 9, 2024
1 parent 8690594 commit de1226a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,18 @@

namespace RZ\Roadiz\CoreBundle\Api\DataTransformer;

use RZ\Roadiz\Core\AbstractEntities\PersistableInterface;
use RZ\Roadiz\CoreBundle\Api\Model\WebResponseInterface;

interface WebResponseDataTransformerInterface
{
/**
* Transforms the given object to something else, usually another object.
* This must return the original object if no transformations have been done.
*
* @param object $object
* @param PersistableInterface $object
* @param string $to
* @param array $context
* @return WebResponseInterface|null
*/
public function transform($object, string $to, array $context = []): ?WebResponseInterface;

/**
* Checks whether the transformation is supported for a given data and context.
*
* @param object|array $data object on normalize / array on denormalize
*/
public function supportsTransformation($data, string $to, array $context = []): bool;
public function transform(PersistableInterface $object, string $to, array $context = []): ?WebResponseInterface;

public function createWebResponse(): WebResponseInterface;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use RZ\Roadiz\Core\AbstractEntities\PersistableInterface;
use RZ\Roadiz\Core\AbstractEntities\TranslationInterface;
use RZ\Roadiz\CoreBundle\Api\Breadcrumbs\BreadcrumbsFactoryInterface;
use RZ\Roadiz\CoreBundle\Api\Model\BlocksAwareWebResponseInterface;
use RZ\Roadiz\CoreBundle\Api\Model\NodesSourcesHeadFactoryInterface;
use RZ\Roadiz\CoreBundle\Api\Model\RealmsAwareWebResponseInterface;
use RZ\Roadiz\CoreBundle\Api\Model\WebResponse;
use RZ\Roadiz\CoreBundle\Api\Model\WebResponseInterface;
use RZ\Roadiz\CoreBundle\Api\TreeWalker\AutoChildrenNodeSourceWalker;
Expand Down Expand Up @@ -69,45 +71,32 @@ protected function getRealmResolver(): RealmResolverInterface
return $this->realmResolver;
}

public function createWebResponse(): WebResponse
public function createWebResponse(): WebResponseInterface
{
return new WebResponse();
}

/**
* @inheritDoc
*/
public function transform($object, string $to, array $context = []): ?WebResponseInterface
public function transform(PersistableInterface $object, string $to, array $context = []): ?WebResponseInterface
{
if (!$object instanceof PersistableInterface) {
throw new \InvalidArgumentException(
'Data to transform must be instance of ' .
PersistableInterface::class
);
}
$output = $this->createWebResponse();
$output->item = $object;
$output->setItem($object);
if ($object instanceof NodesSources) {
$this->injectRealms($output, $object);
$this->injectBlocks($output, $object);
if ($output instanceof RealmsAwareWebResponseInterface) {
$this->injectRealms($output, $object);
}
if ($output instanceof BlocksAwareWebResponseInterface) {
$this->injectBlocks($output, $object);
}

$output->path = $this->urlGenerator->generate(RouteObjectInterface::OBJECT_BASED_ROUTE_NAME, [
$output->setPath($this->urlGenerator->generate(RouteObjectInterface::OBJECT_BASED_ROUTE_NAME, [
RouteObjectInterface::ROUTE_OBJECT => $object
], UrlGeneratorInterface::ABSOLUTE_PATH);
$output->head = $this->nodesSourcesHeadFactory->createForNodeSource($object);
$output->breadcrumbs = $this->breadcrumbsFactory->create($object);
], UrlGeneratorInterface::ABSOLUTE_PATH));
$output->setHead($this->nodesSourcesHeadFactory->createForNodeSource($object));
$output->setBreadcrumbs($this->breadcrumbsFactory->create($object));
}
if ($object instanceof TranslationInterface) {
$output->head = $this->nodesSourcesHeadFactory->createForTranslation($object);
$output->setHead($this->nodesSourcesHeadFactory->createForTranslation($object));
}
return $output;
}

/**
* @inheritDoc
*/
public function supportsTransformation($data, string $to, array $context = []): bool
{
return WebResponseInterface::class === $to && $data instanceof PersistableInterface;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function getBlocks(): ?Collection;

/**
* @param Collection<int, WalkerInterface>|null $blocks
* @return BlocksAwareWebResponseInterface
* @return $this
*/
public function setBlocks(?Collection $blocks): BlocksAwareWebResponseInterface;
public function setBlocks(?Collection $blocks): self;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public function getRealms(): ?array;

/**
* @param RealmInterface[]|null $realms
* @return RealmsAwareWebResponseInterface
* @return $this
*/
public function setRealms(?array $realms): RealmsAwareWebResponseInterface;
public function setRealms(?array $realms): self;

/**
* @return bool
Expand All @@ -26,7 +26,7 @@ public function isHidingBlocks(): bool;

/**
* @param bool $hidingBlocks
* @return RealmsAwareWebResponseInterface
* @return $this
*/
public function setHidingBlocks(bool $hidingBlocks): RealmsAwareWebResponseInterface;
public function setHidingBlocks(bool $hidingBlocks): self;
}
5 changes: 5 additions & 0 deletions lib/RoadizCoreBundle/src/Api/Model/WebResponseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
namespace RZ\Roadiz\CoreBundle\Api\Model;

use RZ\Roadiz\Core\AbstractEntities\PersistableInterface;
use RZ\Roadiz\CoreBundle\Api\Breadcrumbs\BreadcrumbsInterface;

interface WebResponseInterface
{
public function setHead(?NodesSourcesHeadInterface $head): self;
public function setBreadcrumbs(?BreadcrumbsInterface $breadcrumbs): self;
public function setItem(?PersistableInterface $item): self;
public function setPath(?string $path): self;
public function getItem(): ?PersistableInterface;
}
24 changes: 24 additions & 0 deletions lib/RoadizCoreBundle/src/Api/Model/WebResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ trait WebResponseTrait
#[Serializer\Groups(["web_response"])]
private bool $hidingBlocks = false;

public function setPath(?string $path): self
{
$this->path = $path;
return $this;
}

public function setItem(?PersistableInterface $item): self
{
$this->item = $item;
return $this;
}

/**
* @return PersistableInterface|null
*/
Expand Down Expand Up @@ -100,4 +112,16 @@ public function setHidingBlocks(bool $hidingBlocks): self
$this->hidingBlocks = $hidingBlocks;
return $this;
}

public function setBreadcrumbs(?BreadcrumbsInterface $breadcrumbs): self
{
$this->breadcrumbs = $breadcrumbs;
return $this;
}

public function setHead(?NodesSourcesHeadInterface $head): self
{
$this->head = $head;
return $this;
}
}

0 comments on commit de1226a

Please sign in to comment.