address: render Address as lines

This commit is contained in:
juminet
2022-01-10 11:49:38 +00:00
committed by Julien Fastré
parent a3ea28d307
commit edeaaf0218
8 changed files with 451 additions and 105 deletions

View File

@@ -12,6 +12,7 @@ declare(strict_types=1);
namespace Chill\MainBundle\Templating\Entity;
use Chill\MainBundle\Entity\Address;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Symfony\Component\Templating\EngineInterface;
use function array_merge;
@@ -26,14 +27,18 @@ class AddressRender implements ChillEntityRenderInterface
'with_delimiter' => false,
'has_no_address' => false,
'multiline' => true,
/* deprecated */
'extended_infos' => false,
];
private EngineInterface $templating;
public function __construct(EngineInterface $templating)
private TranslatableStringHelper $translatableStringHelper;
public function __construct(EngineInterface $templating, TranslatableStringHelper $translatableStringHelper)
{
$this->templating = $templating;
$this->translatableStringHelper = $translatableStringHelper;
}
/**
@@ -50,43 +55,49 @@ class AddressRender implements ChillEntityRenderInterface
'streetLine' => $this->renderStreetLine($addr),
'render' => $options['render'] ?? 'bloc',
'options' => $options,
'lines' => $this->renderLines($addr),
]);
}
/**
* @param Address addr
* @param mixed $addr
*
* @return string[]
*/
public function renderString($addr, array $options): string
public function renderLines($addr): array
{
$lines = [];
$lines[0] = $this->renderStreetLine($addr);
if (!empty($addr->getPostcode())) {
$lines[1] = strtr('{postcode} {label}', [
'{postcode}' => $addr->getPostcode()->getCode(),
'{label}' => $addr->getPostcode()->getName(),
]);
if (null !== $addr->getPostCode()) {
if ($addr->getPostCode()->getCountry()->getCountryCode() === 'FR') {
$lines[] = $this->renderIntraBuildingLine($addr);
$lines[] = $this->renderBuildingLine($addr);
$lines[] = $this->renderStreetLine($addr);
$lines[] = $this->renderDeliveryLine($addr);
$lines[] = $this->renderCityLine($addr);
$lines[] = $this->renderCountryLine($addr);
} else {
$lines[] = $this->renderBuildingLine($addr);
$lines[] = $this->renderDeliveryLine($addr);
$lines[] = $this->renderStreetLine($addr);
$lines[] = $this->renderCityLine($addr);
$lines[] = $this->renderCountryLine($addr);
}
}
return implode(' - ', $lines);
return array_values(array_filter($lines, static fn ($l) => null !== $l));
}
public function supports($entity, array $options): bool
public function renderStreetLine(Address $addr): ?string
{
return $entity instanceof Address;
}
private function renderStreetLine($addr): string
{
if (!empty($addr->getStreet())) {
if (null !== $addr->getStreet() && $addr->getStreet() !== '') {
$street = $addr->getStreet();
} else {
$street = '';
}
if (!empty($addr->getStreetNumber())) {
if (null !== $addr->getStreetNumber() && $addr->getStreetNumber() !== '') {
$streetNumber = $addr->getStreetNumber();
} else {
$streetNumber = '';
@@ -100,8 +111,111 @@ class AddressRender implements ChillEntityRenderInterface
}
}
if (',' === $res) {
$res = '';
if ((',' === $res) || ('' === $res)) {
$res = null;
}
return $res;
}
/**
* @param Address addr
* @param mixed $addr
*/
public function renderString($addr, array $options): string
{
return implode(' - ', $this->renderLines($addr));
}
public function supports($entity, array $options): bool
{
return $entity instanceof Address;
}
private function renderBuildingLine(Address $addr): ?string
{
if (null !== $addr->getBuildingName() && $addr->getBuildingName() !== '') {
$building = $addr->getBuildingName();
} else {
$building = '';
}
$intraBuilding = $this->renderIntraBuildingLine($addr);
if (null === $intraBuilding) {
$intraBuilding = '';
}
$res = trim($building . ' - ' . $intraBuilding, ' - ');
if ('' === $res) {
$res = null;
}
if (null !== $addr->getPostCode()->getCountry()->getCountryCode()) {
if ($addr->getPostCode()->getCountry()->getCountryCode() === 'FR') {
$res = $addr->getBuildingName();
}
}
return $res;
}
private function renderCityLine($addr): string
{
if (null !== $addr->getPostcode()) {
$res = strtr('{postcode} {label}', [
'{postcode}' => $addr->getPostcode()->getCode(),
'{label}' => $addr->getPostcode()->getName(),
]);
if (null !== $addr->getPostCode()->getCountry()->getCountryCode()) {
if ($addr->getPostCode()->getCountry()->getCountryCode() === 'FR') {
if ($addr->getDistribution()) {
$res = $res . ' ' . $addr->getDistribution();
}
}
}
}
return $res ?? '';
}
private function renderCountryLine($addr): ?string
{
return $this->translatableStringHelper->localize(
$addr->getPostCode()->getCountry()->getName()
);
}
private function renderDeliveryLine($addr): ?string
{
return $addr->getExtra();
}
private function renderIntraBuildingLine($addr): ?string
{
$arr = [];
if ($addr->getFlat()) {
$arr[] = 'appart ' . $addr->getFlat();
}
if ($addr->getFloor()) {
$arr[] = 'ét ' . $addr->getFloor();
}
if ($addr->getCorridor()) {
$arr[] = 'coul ' . $addr->getCorridor();
}
if ($addr->getSteps()) {
$arr[] = 'esc ' . $addr->getSteps();
}
$res = implode(' - ', $arr);
if ('' === $res) {
$res = null;
}
return $res;