73 lines
1.8 KiB
PHP

<?php
namespace Chill\MainBundle\Templating\Entity;
use Symfony\Component\Templating\EngineInterface;
use Chill\MainBundle\Entity\Address;
class AddressRender implements ChillEntityRenderInterface
{
private EngineInterface $templating;
public const DEFAULT_OPTIONS = [
'with_valid_from' => false,
'with_valid_to' => false,
'with_picto' => false,
'with_delimiter' => false,
'has_no_address' => false,
'multiline' => true,
'extended_infos' => false
];
public function __construct(EngineInterface $templating)
{
$this->templating = $templating;
}
/**
* {@inheritDoc}
*/
public function supports($entity, array $options): bool
{
return $entity instanceof Address;
}
/**
* @param Address addr
*/
public function renderString($addr, array $options): string
{
$lines = [];
if (!empty($addr->getStreet())) {
$lines[0] = $addr->getStreet();
}
if (!empty($addr->getStreetNumber())) {
$lines[0] .= ", ".$addr->getStreetNumber();
}
if (!empty($addr->getPostcode())) {
$lines[1] = \strtr("{postcode} {label}", [
'{postcode}' => $addr->getPostcode()->getCode(),
'{label}' => $addr->getPostcode()->getName()
]);
}
return implode(" - ", $lines);
}
/**
* {@inheritDoc}
* @param Address addr
*/
public function renderBox($addr, array $options): string
{
$options = \array_merge(self::DEFAULT_OPTIONS, $options);
return $this->templating
->render('@ChillMain/Entity/address.html.twig', [
'address' => $addr,
'render' => $options['render'] ?? 'bloc',
'options' => $options
]);
}
}