Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
59 changes: 59 additions & 0 deletions src/Form/Type/CurrencyMoneyPHPType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Money\MoneyBundle\Form\Type;

use Money\Currencies;
use Money\Currency;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CurrencyMoneyPHPType extends AbstractType
{
/**
* @var Currencies
*/
private $currencies;

/**
* @param Currencies $currencies
*/
public function __construct(Currencies $currencies)
{
$this->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,
])
;
}
}
39 changes: 39 additions & 0 deletions src/Form/Type/MoneyPHPType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Money\MoneyBundle\Form\Type;

use Money\Money;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class MoneyPHPType extends AbstractType
{
/**
* {@inheritDoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver
->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'],
])
;
}
}