diff --git a/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderExtension.php b/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderExtension.php index 9c8d4599e..8a9e70229 100644 --- a/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderExtension.php +++ b/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderExtension.php @@ -19,24 +19,7 @@ use Twig\TwigFilter; */ class ChillEntityRenderExtension extends AbstractExtension { - /** - * @var ChillEntityRender - */ - protected $defaultRender; - - /** - * @var iterable|ChillEntityRenderInterface[] - */ - protected $renders = []; - - /** - * ChillEntityRenderExtension constructor. - */ - public function __construct(iterable $renders) - { - $this->defaultRender = new ChillEntityRender(); - $this->renders = $renders; - } + public function __construct(private ChillEntityRenderManagerInterface $renderManager) {} /** * @return array|TwigFilter[] @@ -53,34 +36,13 @@ class ChillEntityRenderExtension extends AbstractExtension ]; } - public function renderBox($entity, array $options = []): string + public function renderBox(?object $entity, array $options = []): string { - if (null === $entity) { - return ''; - } - - return $this->getRender($entity, $options) - ->renderBox($entity, $options); + return $this->renderManager->renderBox($entity, $options); } - public function renderString($entity, array $options = []): string + public function renderString(?object $entity, array $options = []): string { - if (null === $entity) { - return ''; - } - - return $this->getRender($entity, $options) - ->renderString($entity, $options); - } - - protected function getRender($entity, $options): ?ChillEntityRenderInterface - { - foreach ($this->renders as $render) { - if ($render->supports($entity, $options)) { - return $render; - } - } - - return $this->defaultRender; + return $this->renderManager->renderString($entity, $options); } } diff --git a/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderInterface.php b/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderInterface.php index d628420c7..7a7a5e1fe 100644 --- a/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderInterface.php +++ b/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderInterface.php @@ -15,7 +15,7 @@ namespace Chill\MainBundle\Templating\Entity; * Interface to implement which will render an entity in template on a custom * manner. * - * @template T + * @template T of object */ interface ChillEntityRenderInterface { @@ -31,7 +31,7 @@ interface ChillEntityRenderInterface * * ``` * - * @param T $entity + * @param T|null $entity * * @phpstan-pure */ @@ -42,7 +42,7 @@ interface ChillEntityRenderInterface * * Example: returning the name of a person. * - * @param T $entity + * @param T|null $entity * * @phpstan-pure */ diff --git a/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderManager.php b/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderManager.php new file mode 100644 index 000000000..6437a9c24 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderManager.php @@ -0,0 +1,59 @@ + + */ + private iterable $renders; + + public function __construct(iterable $renders) + { + $this->defaultRender = new ChillEntityRender(); + $this->renders = $renders; + } + + public function renderBox($entity, array $options = []): string + { + if (null === $entity) { + return ''; + } + + return $this->getRender($entity, $options) + ->renderBox($entity, $options); + } + + public function renderString($entity, array $options = []): string + { + if (null === $entity) { + return ''; + } + + return $this->getRender($entity, $options) + ->renderString($entity, $options); + } + + private function getRender($entity, $options): ChillEntityRenderInterface + { + foreach ($this->renders as $render) { + if ($render->supports($entity, $options)) { + return $render; + } + } + + return $this->defaultRender; + } +} diff --git a/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderManagerInterface.php b/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderManagerInterface.php new file mode 100644 index 000000000..44b996d73 --- /dev/null +++ b/src/Bundle/ChillMainBundle/Templating/Entity/ChillEntityRenderManagerInterface.php @@ -0,0 +1,19 @@ +