|
|
@@ -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);
|
|
|
+ }
|
|
|
+}
|