Skip to content

Instantly share code, notes, and snippets.

@mmoreram
Created June 22, 2015 17:30
Show Gist options
  • Save mmoreram/baa50c81ffd9192117f9 to your computer and use it in GitHub Desktop.
Save mmoreram/baa50c81ffd9192117f9 to your computer and use it in GitHub Desktop.
Money form type
<?php
/*
* This file is part of the Elcodi package.
*
* Copyright (c) 2014-2015 Elcodi.com
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* Feel free to edit as you please, and have fun.
*
* @author Marc Morera <[email protected]>
* @author Aldo Chiecchia <[email protected]>
* @author Elcodi Team <[email protected]>
*/
namespace Elcodi\Admin\CurrencyBundle\Form\Type;
use Elcodi\Component\Currency\Entity\Interfaces\CurrencyInterface;
use Elcodi\Component\Currency\Repository\CurrencyRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Elcodi\Component\Core\Wrapper\Interfaces\WrapperInterface;
use Elcodi\Component\Currency\Entity\Money;
/**
* Class MoneyType
*/
class MoneyType extends AbstractType
{
/**
* @var WrapperInterface
*
* Currency Wrapper
*/
protected $defaultCurrencyWrapper;
/**
* @var string
*
* Currency interface
*/
protected $currencyInterface;
/**
* @var string
*
* Default currency iso
*/
protected $defaultCurrencyIso;
/**
* Construct method
*
* @param WrapperInterface $defaultCurrencyWrapper Default Currency Wrapper
* @param string $currencyInterface Currency interface
*/
public function __construct(
WrapperInterface $defaultCurrencyWrapper,
$currencyInterface
)
{
$this->defaultCurrencyWrapper = $defaultCurrencyWrapper;
$this->currencyInterface = $currencyInterface;
$this->defaultCurrencyIso = $defaultCurrencyWrapper
->get()
->getIso();
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('amount', 'money', [
'divisor' => 100,
'currency' => false,
])
->add('currency', 'entity', [
'class' => $this->currencyInterface,
'query_builder' => function (CurrencyRepository $repository) {
return $repository
->createQueryBuilder('c')
->where('c.enabled = :enabled')
->setParameter('enabled', true);
},
'required' => true,
'multiple' => false,
'property' => 'symbol',
]);
}
/**
* Configures the options for this type.
*
* @param OptionsResolver $resolver The resolver for the options.
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => 'Elcodi\Component\Currency\Entity\Money',
'empty_data' => function (FormInterface $form) {
$currency = $form
->get('currency')
->getData();
return ($currency instanceof CurrencyInterface)
? Money::create(
$form->get('amount')->getData(),
$form->get('currency')->getData()
)
: Money::create(
0,
$this
->defaultCurrencyWrapper
->get()
);
}
]);
}
/**
* Returns the name of this type.
*
* @return string The name of this type
*/
public function getName()
{
return 'money_object';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment