-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, this option needs a lazy default to evaluate the |
||
'country' => null, | ||
)); | ||
|
||
$resolver->setAllowedValues('input', array('string', 'datetimezone')); | ||
|
||
$resolver->setAllowedTypes('regions', 'int'); | ||
$resolver->setAllowedTypes('country', array('string', 'null')); | ||
} | ||
|
||
/** | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no big fan of |
||
{ | ||
$timezones = array(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ogizanagi WDYT ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is strictly no guarantee that |
||
} | ||
|
||
public function testDateTimeZoneInput() | ||
{ | ||
$form = $this->factory->create(static::TESTED_TYPE, new \DateTimeZone('America/New_York'), array('input' => 'datetimezone')); | ||
|
Uh oh!
There was an error while loading. Please reload this page.