false, 'with_valid_to' => false, 'with_picto' => false, 'with_delimiter' => false, 'has_no_address' => false, 'multiline' => true, /* deprecated */ 'extended_infos' => false, ]; private EngineInterface $templating; private TranslatableStringHelperInterface $translatableStringHelper; public function __construct( EngineInterface $templating, TranslatableStringHelperInterface $translatableStringHelper) { $this->templating = $templating; $this->translatableStringHelper = $translatableStringHelper; } /** * @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, 'streetLine' => $this->renderStreetLine($addr), 'render' => $options['render'] ?? 'bloc', 'options' => $options, 'lines' => $this->renderLines($addr), ]); } /** * @return string[] */ public function renderLines(Address $addr, bool $includeCityLine = true, bool $includeCountry = true): array { $lines = []; 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); if ($includeCityLine) { $lines[] = $this->renderCityLine($addr); } if ($includeCountry) { $lines[] = $this->renderCountryLine($addr); } } else { $lines[] = $this->renderBuildingLine($addr); $lines[] = $this->renderDeliveryLine($addr); $lines[] = $this->renderStreetLine($addr); if ($includeCityLine) { $lines[] = $this->renderCityLine($addr); } if ($includeCountry) { $lines[] = $this->renderCountryLine($addr); } } } return array_values(array_filter($lines, static fn ($l) => '' !== (string) $l)); } public function renderStreetLine(Address $addr): ?string { if ('' !== $addr->getStreet()) { $street = $addr->getStreet(); } else { $street = ''; } if ('' !== $addr->getStreetNumber()) { $streetNumber = $addr->getStreetNumber(); } else { $streetNumber = ''; } $res = trim($street . ', ' . $streetNumber, ', '); if (null !== $addr->getPostCode()->getCountry()->getCountryCode()) { if ($addr->getPostCode()->getCountry()->getCountryCode() === 'FR') { $res = trim($streetNumber . ', ' . $street, ', '); } } 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 ($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(Address $addr): string { if (null !== $addr->getPostcode()) { $res = strtr('{postcode} {label}', [ '{postcode}' => $addr->getPostcode()->getCode(), '{label}' => $addr->getPostcode()->getName(), ]); if ($addr->getPostcode()->getCountry()->getCountryCode() === 'FR') { if ('' !== $addr->getDistribution()) { $res = $res . ' ' . $addr->getDistribution(); } } } return $res ?? ''; } private function renderCountryLine(Address $addr): ?string { return $this->translatableStringHelper->localize( $addr->getPostcode()->getCountry()->getName() ); } private function renderDeliveryLine(Address $addr): string { return $addr->getExtra(); } private function renderIntraBuildingLine(Address $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; } }