From 8b94218937a43151f0404e2e3d95edc585395def Mon Sep 17 00:00:00 2001 From: Xavier Coureau Date: Mon, 3 Feb 2020 14:42:43 +0100 Subject: [PATCH 1/4] Adds form types --- composer.json | 3 +- src/Form/Type/CurrencyMoneyPHPType.php | 58 ++++++++++++++++++++++++++ src/Form/Type/MoneyPHPType.php | 36 ++++++++++++++++ 3 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 src/Form/Type/CurrencyMoneyPHPType.php create mode 100644 src/Form/Type/MoneyPHPType.php diff --git a/composer.json b/composer.json index f736b54..6dc594e 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ "moneyphp/money": "^3.0" }, "require-dev": { - "phpunit/phpunit": "^8.5" + "phpunit/phpunit": "^8.5", + "symfony/form": "^4.2 || ^5.0" }, "autoload": { "psr-4": { diff --git a/src/Form/Type/CurrencyMoneyPHPType.php b/src/Form/Type/CurrencyMoneyPHPType.php new file mode 100644 index 0000000..d611d95 --- /dev/null +++ b/src/Form/Type/CurrencyMoneyPHPType.php @@ -0,0 +1,58 @@ +currencies = $currencies; + } + + /** + * {@inheritDoc} + */ + public function getParent() + { + return ChoiceType::class; + } + + /** + * {@inheritDoc} + */ + public function configureOptions(OptionsResolver $resolver) + { + $resolver + ->setDefaults([ + 'choice_loader' => new CallbackChoiceLoader(function () { + $currencies = array_map( + static function (Currency $currency) { + return $currency->getCode(); + }, iterator_to_array($this->currencies) + ); + + ksort($currencies); + + return array_combine($currencies, $currencies); + }), + ]) + ; + } +} diff --git a/src/Form/Type/MoneyPHPType.php b/src/Form/Type/MoneyPHPType.php new file mode 100644 index 0000000..374ca51 --- /dev/null +++ b/src/Form/Type/MoneyPHPType.php @@ -0,0 +1,36 @@ +setDefaults([ + 'data_class' => Money::class, + ]); + } + + /** + * {@inheritDoc} + */ + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder + ->add('amount', NumberType::class) + ->add('currency', CurrencyMoneyPHPType::class) + ; + } +} From 819ec01a4e63a18632ea5afdefe7cb6fea36844e Mon Sep 17 00:00:00 2001 From: Xavier Coureau Date: Mon, 3 Feb 2020 15:15:57 +0100 Subject: [PATCH 2/4] Disables translation on currency choices --- src/Form/Type/CurrencyMoneyPHPType.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Form/Type/CurrencyMoneyPHPType.php b/src/Form/Type/CurrencyMoneyPHPType.php index d611d95..ed12b47 100644 --- a/src/Form/Type/CurrencyMoneyPHPType.php +++ b/src/Form/Type/CurrencyMoneyPHPType.php @@ -52,6 +52,7 @@ static function (Currency $currency) { return array_combine($currencies, $currencies); }), + 'choice_translation_domain' => false, ]) ; } From e1133b70211524580bf83906200c721228705a3c Mon Sep 17 00:00:00 2001 From: Xavier Coureau Date: Mon, 3 Feb 2020 15:23:50 +0100 Subject: [PATCH 3/4] Adds preferred_choices option for currencies --- src/Form/Type/MoneyPHPType.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Form/Type/MoneyPHPType.php b/src/Form/Type/MoneyPHPType.php index 374ca51..93b8956 100644 --- a/src/Form/Type/MoneyPHPType.php +++ b/src/Form/Type/MoneyPHPType.php @@ -20,6 +20,7 @@ public function configureOptions(OptionsResolver $resolver) $resolver ->setDefaults([ 'data_class' => Money::class, + 'currencies_preferred_choices' => [], ]); } @@ -30,7 +31,9 @@ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('amount', NumberType::class) - ->add('currency', CurrencyMoneyPHPType::class) + ->add('currency', CurrencyMoneyPHPType::class, [ + 'preferred_choices' => $options['currencies_preferred_choices'], + ]) ; } } From a06dcd31ceaf4066132fd314fe2f3e40318fb2a0 Mon Sep 17 00:00:00 2001 From: Xavier Coureau Date: Mon, 3 Feb 2020 15:39:20 +0100 Subject: [PATCH 4/4] Fixes sorting method --- src/Form/Type/CurrencyMoneyPHPType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Form/Type/CurrencyMoneyPHPType.php b/src/Form/Type/CurrencyMoneyPHPType.php index ed12b47..8089120 100644 --- a/src/Form/Type/CurrencyMoneyPHPType.php +++ b/src/Form/Type/CurrencyMoneyPHPType.php @@ -48,7 +48,7 @@ static function (Currency $currency) { }, iterator_to_array($this->currencies) ); - ksort($currencies); + sort($currencies); return array_combine($currencies, $currencies); }),