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..8089120 --- /dev/null +++ b/src/Form/Type/CurrencyMoneyPHPType.php @@ -0,0 +1,59 @@ +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) + ); + + sort($currencies); + + return array_combine($currencies, $currencies); + }), + 'choice_translation_domain' => false, + ]) + ; + } +} diff --git a/src/Form/Type/MoneyPHPType.php b/src/Form/Type/MoneyPHPType.php new file mode 100644 index 0000000..93b8956 --- /dev/null +++ b/src/Form/Type/MoneyPHPType.php @@ -0,0 +1,39 @@ +setDefaults([ + 'data_class' => Money::class, + 'currencies_preferred_choices' => [], + ]); + } + + /** + * {@inheritDoc} + */ + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder + ->add('amount', NumberType::class) + ->add('currency', CurrencyMoneyPHPType::class, [ + 'preferred_choices' => $options['currencies_preferred_choices'], + ]) + ; + } +}