Skip to content

Commit a766bc4

Browse files
Crovitche-1623nicolas-grekas
authored andcommitted
[Form][Intl] Allow the developer to choose if he want to include/exclude currencies that do not have validity dates.
1 parent 4a374b6 commit a766bc4

File tree

2 files changed

+8
-5
lines changed

2 files changed

+8
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ CHANGELOG
66

77
* Add `input=date_point` to `DateTimeType`, `DateType` and `TimeType`
88
* Add support for guessing form type of enum properties
9-
* Add `active_at`, `not_active_at` and `legal_tender` options to `CurrencyType`
9+
* Add `active_at`, `not_active_at` and `legal_tender`, `include_undated` options to `CurrencyType`
1010
* Add `FormFlow` for multistep forms management
1111

1212
7.3

Extension/Core/Type/CurrencyType.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,22 @@ public function configureOptions(OptionsResolver $resolver): void
3535
$activeAt = $options['active_at'];
3636
$notActiveAt = $options['not_active_at'];
3737
$legalTender = $options['legal_tender'];
38+
$includeUndated = $options['include_undated'];
3839

3940
if (null !== $activeAt && null !== $notActiveAt) {
4041
throw new InvalidOptionsException('The "active_at" and "not_active_at" options cannot be used together.');
4142
}
4243

4344
$legalTenderCacheKey = match ($legalTender) {
44-
null => '',
45+
null => 'X',
4546
true => '1',
4647
false => '0',
4748
};
4849

4950
return ChoiceList::loader(
5051
$this,
5152
new IntlCallbackChoiceLoader(
52-
static function () use ($choiceTranslationLocale, $activeAt, $notActiveAt, $legalTender) {
53+
static function () use ($choiceTranslationLocale, $activeAt, $notActiveAt, $legalTender, $includeUndated) {
5354
if (null === $activeAt && null === $notActiveAt && null === $legalTender) {
5455
return array_flip(Currencies::getNames($choiceTranslationLocale));
5556
}
@@ -63,7 +64,7 @@ static function () use ($choiceTranslationLocale, $activeAt, $notActiveAt, $lega
6364
};
6465

6566
foreach (Currencies::getCurrencyCodes() as $code) {
66-
if (!Currencies::isValidInAnyCountry($code, $legalTender, $active, $activeAt ?? $notActiveAt)) {
67+
if (!Currencies::isValidInAnyCountry($code, $legalTender, $active, $activeAt ?? $notActiveAt, $includeUndated)) {
6768
continue;
6869
}
6970

@@ -73,13 +74,14 @@ static function () use ($choiceTranslationLocale, $activeAt, $notActiveAt, $lega
7374
return array_flip($filteredCurrencyNames);
7475
},
7576
),
76-
$choiceTranslationLocale.($activeAt ?? $notActiveAt)?->format('Y-m-d\TH:i:s').$legalTenderCacheKey,
77+
$choiceTranslationLocale.($activeAt ?? $notActiveAt)?->format('Y-m-d\TH:i:s').$legalTenderCacheKey.(int) $includeUndated,
7778
);
7879
},
7980
'choice_translation_domain' => false,
8081
'choice_translation_locale' => null,
8182
'active_at' => new \DateTimeImmutable('today', new \DateTimeZone('Etc/UTC')),
8283
'not_active_at' => null,
84+
'include_undated' => true,
8385
'legal_tender' => true,
8486
'invalid_message' => 'Please select a valid currency.',
8587
]);
@@ -88,6 +90,7 @@ static function () use ($choiceTranslationLocale, $activeAt, $notActiveAt, $lega
8890
$resolver->setAllowedTypes('active_at', [\DateTimeInterface::class, 'null']);
8991
$resolver->setAllowedTypes('not_active_at', [\DateTimeInterface::class, 'null']);
9092
$resolver->setAllowedTypes('legal_tender', ['bool', 'null']);
93+
$resolver->setAllowedTypes('include_undated', 'bool');
9194
}
9295

9396
public function getParent(): ?string

0 commit comments

Comments
 (0)