diff --git a/ci/qa/phpstan-baseline.neon b/ci/qa/phpstan-baseline.neon index 5a12babb..03251e4a 100644 --- a/ci/qa/phpstan-baseline.neon +++ b/ci/qa/phpstan-baseline.neon @@ -3435,6 +3435,11 @@ parameters: count: 1 path: ../../src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/SensitiveData/SensitiveDataTest.php + - + message: "#^Method Surfnet\\\\StepupMiddleware\\\\CommandHandlingBundle\\\\Tests\\\\Twig\\\\BackwardsCompatibleExtensionTest\\:\\:templateProvider\\(\\) return type has no value type specified in iterable type array\\.$#" + count: 1 + path: ../../src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/Twig/BackwardsCompatibleExtensionTest.php + - message: "#^Method Surfnet\\\\StepupMiddleware\\\\CommandHandlingBundle\\\\Tests\\\\Value\\\\InstitutionTest\\:\\:nonStringOrNonEmptyStringProvider\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 diff --git a/config/services.yaml b/config/services.yaml index 082d1bdf..93190a76 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -14,6 +14,10 @@ services: - "@surfnet_stepup.service.second_factor_type" - '%skip_prove_possession_second_factors%' + Surfnet\StepupMiddleware\CommandHandlingBundle\Twig\BackwardsCompatibleExtension: + arguments: [ "@twig.extension.intl"] + tags: [{ name: twig.extension }] + twig.extension.stringloader: class: Twig\Extension\StringLoaderExtension tags: [{ name: twig.extension }] @@ -27,7 +31,7 @@ services: class: Twig\Sandbox\SecurityPolicy arguments: - [ if, else, elseif, for ] # Allowed tags - - [ escape, format_datetime ] # Allowed filters + - [ escape, localizeddate ] # Allowed filters - # Allowed methods Surfnet\Stepup\Identity\Value\CommonName: - __toString diff --git a/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/Twig/BackwardsCompatibleExtensionTest.php b/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/Twig/BackwardsCompatibleExtensionTest.php new file mode 100644 index 00000000..02d08068 --- /dev/null +++ b/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Tests/Twig/BackwardsCompatibleExtensionTest.php @@ -0,0 +1,61 @@ + $template]), ['debug' => true, 'cache' => false, 'autoescape' => 'html', 'optimizations' => 0]); + $twig->addExtension( new BackwardsCompatibleExtension(new IntlExtension())); + + $output = $twig->render('template', ['date' => $date, 'locale' => $locale]); + $this->assertEquals($expected, $output); + + $output = $twig->render('template', ['date' => $dateString, 'locale' => $locale]); + $this->assertEquals($expected, $output); + } + + public function templateProvider(): array + { + return [ + 'date en' => ["{{ date | localizeddate('full', 'none', locale) }}", 'Thursday, 5 December 2024', 'en_GB'], + 'date nl' => ["{{ date | localizeddate('full', 'none', locale) }}", 'donderdag 5 december 2024', 'nl_NL'], + 'date and time nl' => ["{{ date | localizeddate('full', 'medium', locale) }}", 'Thursday, 5 December 2024 at 13:12:10', 'en_GB'], + 'date and time en' => ["{{ date | localizeddate('full', 'medium', locale) }}", 'donderdag 5 december 2024 om 13:12:10', 'nl_NL'], + 'time nl' => ["{{ date | localizeddate('none', 'medium', locale) }}", '13:12:10', 'en_GB'], + 'time en' => ["{{ date | localizeddate('none', 'medium', locale) }}", '13:12:10', 'nl_NL'], + ]; + } +} \ No newline at end of file diff --git a/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Twig/BackwardsCompatibleExtension.php b/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Twig/BackwardsCompatibleExtension.php new file mode 100644 index 00000000..8c254acf --- /dev/null +++ b/src/Surfnet/StepupMiddleware/CommandHandlingBundle/Twig/BackwardsCompatibleExtension.php @@ -0,0 +1,59 @@ +intlExtension = $intlExtension; + } + + public function getFilters(): array + { + return [ + new TwigFilter('localizeddate', [$this, 'localizedDate'], ['needs_environment' => true]), + ]; + } + + // localizeddate('full', 'none', locale) + public function localizedDate( + Environment $env, + DateTimeInterface|string|null $date, + ?string $dateFormat = 'medium', + ?string $timeFormat = 'medium', + string $locale = null + ): string { + return $this->intlExtension->formatDateTime($env, $date, $dateFormat, $timeFormat, locale: $locale); + } +}