-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
TimezoneType.php
123 lines (100 loc) · 4.56 KB
/
TimezoneType.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeZoneToStringTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\IntlTimeZoneToStringTransformer;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Intl\Intl;
use Symfony\Component\Intl\Timezones;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TimezoneType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
if ('datetimezone' === $options['input']) {
$builder->addModelTransformer(new DateTimeZoneToStringTransformer($options['multiple']));
} elseif ('intltimezone' === $options['input']) {
$builder->addModelTransformer(new IntlTimeZoneToStringTransformer($options['multiple']));
}
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'intl' => false,
'choice_loader' => function (Options $options) {
$input = $options['input'];
if ($options['intl']) {
if (!class_exists(Intl::class)) {
throw new LogicException(sprintf('The "symfony/intl" component is required to use "%s" with option "intl=true". Try running "composer require symfony/intl".', static::class));
}
$choiceTranslationLocale = $options['choice_translation_locale'];
return ChoiceList::loader($this, new IntlCallbackChoiceLoader(static fn () => self::getIntlTimezones($input, $choiceTranslationLocale)), [$input, $choiceTranslationLocale]);
}
return ChoiceList::lazy($this, static fn () => self::getPhpTimezones($input), $input);
},
'choice_translation_domain' => false,
'choice_translation_locale' => null,
'input' => 'string',
'invalid_message' => 'Please select a valid timezone.',
'regions' => \DateTimeZone::ALL,
]);
$resolver->setAllowedTypes('intl', ['bool']);
$resolver->setAllowedTypes('choice_translation_locale', ['null', 'string']);
$resolver->setNormalizer('choice_translation_locale', static function (Options $options, $value) {
if (null !== $value && !$options['intl']) {
throw new LogicException('The "choice_translation_locale" option can only be used if the "intl" option is set to true.');
}
return $value;
});
$resolver->setAllowedValues('input', ['string', 'datetimezone', 'intltimezone']);
$resolver->setNormalizer('input', static function (Options $options, $value) {
if ('intltimezone' === $value && !class_exists(\IntlTimeZone::class)) {
throw new LogicException('Cannot use "intltimezone" input because the PHP intl extension is not available.');
}
return $value;
});
}
public function getParent(): ?string
{
return ChoiceType::class;
}
public function getBlockPrefix(): string
{
return 'timezone';
}
private static function getPhpTimezones(string $input): array
{
$timezones = [];
foreach (\DateTimeZone::listIdentifiers(\DateTimeZone::ALL) as $timezone) {
if ('intltimezone' === $input && 'Etc/Unknown' === \IntlTimeZone::createTimeZone($timezone)->getID()) {
continue;
}
$timezones[str_replace(['/', '_'], [' / ', ' '], $timezone)] = $timezone;
}
return $timezones;
}
private static function getIntlTimezones(string $input, ?string $locale = null): array
{
$timezones = array_flip(Timezones::getNames($locale));
if ('intltimezone' === $input) {
foreach ($timezones as $name => $timezone) {
if ('Etc/Unknown' === \IntlTimeZone::createTimeZone($timezone)->getID()) {
unset($timezones[$name]);
}
}
}
return $timezones;
}
}