浏览代码

Adds phone number formatting Twig extension

Adds a Twig extension to format phone numbers using libphonenumber.
This allows phone numbers to be displayed in international and national formats in templates.
Also formats the phone number in the sales admin notification email.
Olivier Massot 2 月之前
父节点
当前提交
14a5e68cd4

+ 4 - 0
config/services.yaml

@@ -89,6 +89,10 @@ services:
             tags: [ 'app.mailer.builder' ]
         App\Service\Twig\AssetsExtension:
             tags: [ 'twig.extension' ]
+        App\Service\Twig\ToBase64Extension:
+            tags: [ 'twig.extension' ]
+        App\Service\Twig\PhoneNumberExtension:
+            tags: [ 'twig.extension' ]
         App\Service\Cron\CronjobInterface:
             tags: [ 'app.cronjob' ]
         App\Service\File\Storage\FileStorageInterface:

+ 49 - 0
src/Service/Twig/PhoneNumberExtension.php

@@ -0,0 +1,49 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Service\Twig;
+
+use libphonenumber\PhoneNumber;
+use libphonenumber\PhoneNumberFormat;
+use libphonenumber\PhoneNumberUtil;
+use Twig\Extension\AbstractExtension;
+use Twig\TwigFunction;
+use Twig\TwigFilter;
+
+/**
+ * Provides Twig filters for formatting phone numbers.
+ */
+class PhoneNumberExtension extends AbstractExtension
+{
+    public function __construct(
+        private readonly PhoneNumberUtil $phoneNumberUtil
+    ) {
+    }
+
+    public function getFilters(): array
+    {
+        return [
+            new TwigFilter('phone_international', [$this, 'formatPhoneInternational']),
+            new TwigFilter('phone_national', [$this, 'formatPhoneNational']),
+        ];
+    }
+
+    public function formatPhoneInternational(?PhoneNumber $phoneNumber): string
+    {
+        if ($phoneNumber === null) {
+            return '';
+        }
+
+        return $this->phoneNumberUtil->format($phoneNumber, PhoneNumberFormat::INTERNATIONAL);
+    }
+
+    public function formatPhoneNational(?PhoneNumber $phoneNumber): string
+    {
+        if ($phoneNumber === null) {
+            return '';
+        }
+
+        return $this->phoneNumberUtil->format($phoneNumber, PhoneNumberFormat::NATIONAL);
+    }
+}

+ 1 - 1
templates/emails/shop/NewStructureArtistPremium/notification-to-sales-admin.html.twig

@@ -79,7 +79,7 @@
         </tr>
         <tr>
             <td style="border: 1px solid #ddd; padding: 8px;">Téléphone</td>
-            <td style="border: 1px solid #ddd; padding: 8px;">{{ trialRequest.representativePhone }}</td>
+            <td style="border: 1px solid #ddd; padding: 8px;">{{ trialRequest.representativePhone | phone_international }}</td>
         </tr>
         <tr style="background-color: #f2f2f2;">
             <td style="border: 1px solid #ddd; padding: 8px;">Représentant légal</td>