Skip to content

Commit

Permalink
Add BC localizeddate twig filter
Browse files Browse the repository at this point in the history
  • Loading branch information
pablothedude committed Dec 9, 2024
1 parent d10ad3c commit f0a1b14
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
5 changes: 5 additions & 0 deletions ci/qa/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }]
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* Copyright 2024 SURFnet bv
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Surfnet\StepupMiddleware\CommandHandlingBundle\Tests\Twig;

use DateTime;
use PHPUnit\Framework\TestCase;
use Surfnet\StepupMiddleware\CommandHandlingBundle\Twig\BackwardsCompatibleExtension;
use Twig\Environment;
use Twig\Extra\Intl\IntlExtension;
use Twig\Loader\ArrayLoader;

/**
* @requires extension intl
*/
class BackwardsCompatibleExtensionTest extends TestCase
{
/**
* @dataProvider templateProvider
*/
public function testLocalizedData(string $template, string $expected, string $locale): void
{
$dateString = "2024-12-05 13:12:10";
$date = new DateTime($dateString);
$twig = new Environment(new ArrayLoader(['template' => $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'],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

/**
* Copyright 2024 SURFnet bv
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Surfnet\StepupMiddleware\CommandHandlingBundle\Twig;

use DateTimeInterface;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\Extra\Intl\IntlExtension;
use Twig\TwigFilter;

/**
* This class is introduced to handle BC twig changes in email templates used in the institution configuration.
* * We need to support both versions to support two versions of the codebase to support rolling updates.
* * An idea was to move the email templates to disk but that would cost too much time and we still should support
* * all historical events due to the nature of event sourcing.
*/
class BackwardsCompatibleExtension extends AbstractExtension
{
private IntlExtension $intlExtension;

public function __construct(IntlExtension $intlExtension)
{
$this->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);
}
}

0 comments on commit f0a1b14

Please sign in to comment.