Skip to content

[Form] allow to pass the \DateTimeZone::PER_COUNTRY flag to list identifier per country #25165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/Symfony/Component/Form/Extension/Core/Type/TimezoneType.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,22 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setDefaults(array(
'choice_loader' => function (Options $options) {
$regions = $options['regions'];
$country = $options['country'];

return new CallbackChoiceLoader(function () use ($regions) {
return self::getTimezones($regions);
return new CallbackChoiceLoader(function () use ($regions, $country) {
return self::getTimezones($regions, $country);
});
},
'choice_translation_domain' => false,
'input' => 'string',
'regions' => \DateTimeZone::ALL,
'regions' => isset($options['country']) ? \DateTimeZone::PER_COUNTRY : \DateTimeZone::ALL,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$options doesn't exist in this context, this is always evaluated to \DateTimeZone::ALL

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, this option needs a lazy default to evaluate the country value. (See example)

'country' => null,
));

$resolver->setAllowedValues('input', array('string', 'datetimezone'));

$resolver->setAllowedTypes('regions', 'int');
$resolver->setAllowedTypes('country', array('string', 'null'));
}

/**
Expand All @@ -72,11 +75,11 @@ public function getBlockPrefix()
/**
* Returns a normalized array of timezone choices.
*/
private static function getTimezones(int $regions): array
private static function getTimezones(int $regions, ?string $country = null): array
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no big fan of ? when nulled by default :)

{
$timezones = array();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering.. should we throw if country is set and regions is not PER_COUNTRY? Or perhaps even better, let OptionsResolver forbid such a pair.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idk, do we really want to enforce this ? I guess if it's not passed then it will not work as expected. OptionsResolver seems a good idea to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ogizanagi WDYT ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it can be easily achieved using the OptionsResolver (and it probably is through normalizers), that would be a better DX indeed :)


foreach (\DateTimeZone::listIdentifiers($regions) as $timezone) {
foreach (\DateTimeZone::listIdentifiers($regions, $country) as $timezone) {
$parts = explode('/', $timezone);

if (count($parts) > 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public function testSubmitNull($expected = null, $norm = null, $view = null)
parent::testSubmitNull($expected, $norm, '');
}

public function testFilterByRegionsAndByCountry()
{
$choices = $this->factory->create(static::TESTED_TYPE, null, array('regions' => \DateTimeZone::PER_COUNTRY, 'country' => 'US'))
->createView()->vars['choices'];
$this->assertEquals(new ChoiceView('America/Adak', 'America/Adak', 'Adak'), $choices['America']->getIterator()[0]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is strictly no guarantee that getIterator returns an object implementing ArrayAccess, so this code is very fragile (refactoring the way we build the iterator might break things)

}

public function testDateTimeZoneInput()
{
$form = $this->factory->create(static::TESTED_TYPE, new \DateTimeZone('America/New_York'), array('input' => 'datetimezone'));
Expand Down