Mathieu Jaumotte 2021-02-03 18:50:34 +01:00
parent 2fee88bfce
commit 1cf0e51e9a

View File

@ -24,28 +24,33 @@ use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
/**
*
* Class ChillEntityRenderExtension
*
* @package Chill\MainBundle\Templating\Entity
*/
class ChillEntityRenderExtension extends AbstractExtension
{
/**
*
* @var ChillEntityRenderInterface
*/
protected $renders = [];
/**
*
* @var ChillEntityRender
*/
protected $defaultRender;
/**
* ChillEntityRenderExtension constructor.
*/
public function __construct()
{
$this->defaultRender = new ChillEntityRender();
}
/**
* @return array|TwigFilter[]
*/
public function getFilters()
{
return [
@ -58,31 +63,47 @@ class ChillEntityRenderExtension extends AbstractExtension
];
}
/**
* @param $entity
* @param array $options
* @return string
*/
public function renderString($entity, array $options = []): string
{
if (NULL === $entity) {
return '';
}
return $this->getRender($entity, $options)
->renderString($entity, $options);
}
/**
* @param $entity
* @param array $options
* @return string
*/
public function renderBox($entity, array $options = []): string
{
if (NULL === $entity) {
return '';
}
return $this->getRender($entity, $options)
->renderBox($entity, $options);
}
/**
* @param ChillEntityRenderInterface $render
*/
public function addRender(ChillEntityRenderInterface $render)
{
$this->renders[] = $render;
}
/**
* @param $entity
* @param $options
* @return ChillEntityRenderInterface|null
*/
protected function getRender($entity, $options): ?ChillEntityRenderInterface
{
foreach ($this->renders as $render) {
@ -90,7 +111,6 @@ class ChillEntityRenderExtension extends AbstractExtension
return $render;
}
}
return $this->defaultRender;
}
}