8000 [Form] allow to pass the \DateTimeZone::PER_COUNTRY flag to list identifier per country by Simperfit · Pull Request #25165 · symfony/symfony · GitHub
[go: up one dir, main page]

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

Conversation

Simperfit
Copy link
Contributor
@Simperfit Simperfit commented Nov 26, 2017
Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #25105
License MIT
Doc PR todo

img_2813
Working with a hot chocolate ;).

This PR allows passing the PER_COUNTRY flag to DateTimeZone in the TimeZoneType.

Copy link
Contributor
@ro0NL ro0NL left a comment

Choose a reason for hiding this comment

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

i tend to agree a country option might be useful / is currently missing.

{
$timezones = array();

foreach (\DateTimeZone::listIdentifiers($regions) as $timezone) {
foreach (\DateTimeZone::listIdentifiers($regions, $perCountry) as $timezone) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm, this is not how listIdentifiers works; you'd pass regions=PER_COUNTRY, country=US

https://3v4l.org/SMYkN

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ow, OK, ill change the test

@Simperfit Simperfit force-pushed the feature/add-argument-to-pass-per-country branch 2 times, most recently from 54633ce to 493c5f6 Compare November 26, 2017 18:36
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,
Copy link
Contributor

Choose a reason for hiding this comment

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

i think to get the "right" DX we should determine a default (ALL | PER_COUNTRY) based on country is set yes/no.

Copy link
Member
@yceruto yceruto left a comment

Choose a reason for hiding this comment

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

👍 - could you add also setAllowedTypes?

@nicolas-grekas nicolas-grekas added this to the 4.1 milestone Nov 27, 2017
@Simperfit Simperfit force-pushed the feature/add-argument-to-pass-per-country branch 2 times, most recently from 633042d to 6fadaf2 Compare December 6, 2017 06:15
@Simperfit
Copy link
Contributor Author

I think the travis failure could be linked but I don't understand why

});
},
'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.

- 'regions' => isset($options['country']) ? \DateTimeZone::PER_COUNTRY :\DateTimeZone::ALL,
+ 'regions' => isset($options['country']) ? \DateTimeZone::PER_COUNTRY : \DateTimeZone::ALL,

));

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

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

Choose a reason for hiding this comment

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

- $resolver->setAllowedTypes('country', array('string', null));
+ $resolver->setAllowedTypes('country', array('string', 'null'));

?

@Simperfit Simperfit force-pushed the feature/add-argument-to-pass-per-country branch from 6fadaf2 to 62fbd44 Compare December 9, 2017 08:39
@Simperfit
Copy link
Contributor Author

@ogizanagi @yceruto thanks for the review, PR fixed.

@@ -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
{
$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 :)

@@ -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 :)

@Simperfit Simperfit force-pushed the feature/add-argument-to-pass-per-country branch from 62fbd44 to 3cc50de Compare December 11, 2017 21:01
});
},
'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)

@fabpot
Copy link
Member
fabpot commented Sep 4, 2018

@Simperfit Do you have to finish this one?

{
$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)

@fabpot
Copy link
Member
fabpot commented Oct 10, 2018

Closing as there is no more activity on this PR.

@nicolas-grekas nicolas-grekas modified the milestones: next, 4.2 Nov 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants
0