2021-12-21 10:59:23 +01:00

94 lines
2.7 KiB
PHP

<?php
/**
* 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.
*/
declare(strict_types=1);
namespace Chill\PersonBundle\Templating\Entity;
use Chill\MainBundle\Templating\Entity\ChillEntityRenderInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
use Symfony\Component\Templating\EngineInterface;
use function array_merge;
use function array_reverse;
use function implode;
class SocialActionRender implements ChillEntityRenderInterface
{
public const DEFAULT_ARGS = [
self::SEPARATOR_KEY => ' > ',
self::NO_BADGE => false,
];
/**
* if true, the action will not be encapsulated into a "badge".
*/
public const NO_BADGE = 'no-badge';
public const SEPARATOR_KEY = 'default.separator';
private EngineInterface $engine;
private TranslatableStringHelper $translatableStringHelper;
public function __construct(TranslatableStringHelper $translatableStringHelper, EngineInterface $engine)
{
$this->translatableStringHelper = $translatableStringHelper;
$this->engine = $engine;
}
public function renderBox($socialAction, array $options): string
{
$options = array_merge(self::DEFAULT_ARGS, $options);
// give some help to twig: an array of parents
$parents = $this->buildParents($socialAction);
return $this->engine->render('@ChillPerson/Entity/social_action.html.twig', [
'socialAction' => $socialAction,
'parents' => $parents,
'options' => $options,
]);
}
public function renderString($socialAction, array $options): string
{
/** @var SocialAction $socialAction */
$options = array_merge(self::DEFAULT_ARGS, $options);
$titles = [$this->translatableStringHelper->localize($socialAction->getTitle())];
while ($socialAction->hasParent()) {
$socialAction = $socialAction->getParent();
$titles[] = $this->translatableStringHelper->localize(
$socialAction->getTitle()
);
}
$titles = array_reverse($titles);
return implode($options[self::SEPARATOR_KEY], $titles);
}
public function supports($entity, array $options): bool
{
return $entity instanceof SocialAction;
}
protected function buildParents($socialAction): array
{
$parents = [];
while ($socialAction->hasParent()) {
$socialAction = $parents[] = $socialAction->getParent();
}
return $parents;
}
}