Skip to content

Instantly share code, notes, and snippets.

@webmozart
Created June 22, 2015 18:55
Show Gist options
  • Save webmozart/36b1cd8a68a5f7844268 to your computer and use it in GitHub Desktop.
Save webmozart/36b1cd8a68a5f7844268 to your computer and use it in GitHub Desktop.

Revisions

  1. webmozart created this gist Jun 22, 2015.
    31 changes: 31 additions & 0 deletions MoneyType.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    class MoneyType extends AbstractType implements DataMapperInterface
    {
    public function buildForm(FormBuilder $builder, array $options)
    {
    $builder
    ->add('amount', 'integer')
    ->add('currency', 'string')
    ->setDataMapper($this)
    ;
    }

    public function mapDataToForms($data, $forms)
    {
    foreach ($forms as $form) {
    switch ($form->getName()) {
    case 'amount':
    $form->setData($data->getAmount());
    break;
    case 'currency':
    $form->setData($data->getCurrency());
    break;
    }
    }
    }

    public function mapFormsToData($forms, &$data)
    {
    $forms = iterator_to_array($forms);
    $data = new Money($forms['amount']->getData(), $forms['currency']->getData());
    }
    }