Refactor entity rendering with manager pattern

Introduce ChillEntityRenderManager to centralize entity rendering logic, reducing redundancy and improving code organization. Update dependencies and service configuration to support the new manager pattern, enhancing maintainability and flexibility of entity rendering in templates.
This commit is contained in:
2024-09-12 11:17:54 +02:00
parent 0ee91800ab
commit 09563979a2
5 changed files with 93 additions and 48 deletions

View File

@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Templating\Entity;
final readonly class ChillEntityRenderManager implements ChillEntityRenderManagerInterface
{
private ChillEntityRender $defaultRender;
/**
* @var iterable<ChillEntityRenderInterface>
*/
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;
}
}