false, 'with_valid_to' => false, 'with_picto' => false, 'with_delimiter' => false, 'has_no_address' => false, 'multiline' => true, 'extended_infos' => false, ]; private EngineInterface $templating; private TranslatableStringHelper $translatableStringHelper; public function __construct(EngineInterface $templating, TranslatableStringHelper $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), ]); } /** * @param Address addr * @param mixed $addr * * @return string[] */ public function renderLines($addr): 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); $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 array_values(array_filter($lines, fn ($l) => null !== $l)); } /** * @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; } public function renderStreetLine($addr): ?string { if (!empty($addr->getStreet())) { $street = $addr->getStreet(); } else { $street = ''; } if (!empty($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; } private function renderBuildingLine($addr): ?string { if (!empty($addr->getBuildingName())) { $building = $addr->getBuildingName(); } else { $building = ''; } if (!empty($this->renderIntraBuildingLine($addr))) { $intraBuilding = $this->renderIntraBuildingLine($addr); } else { $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 (!empty($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; } }