92 lines
2.3 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);
/**
* 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;
use Chill\MainBundle\Entity\Address;
use Symfony\Component\Templating\EngineInterface;
use function array_merge;
use function strtr;
class AddressRender implements ChillEntityRenderInterface
{
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,
];
private EngineInterface $templating;
public function __construct(EngineInterface $templating)
{
$this->templating = $templating;
}
/**
* @param Address addr
* @param mixed $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,
]);
}
/**
* @param Address addr
* @param mixed $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);
}
public function supports($entity, array $options): bool
{
return $entity instanceof Address;
}
}