<?php
/**
 * Subform Generator Type
 *
 * Used by FormHelper, not for general usage!
 *
 * @author Elze Kool <info@visualmedia.nl>
 */

namespace VisualM\FormBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
 * Subform Generator Type
 *
 * Used by FormHelper, not for general usage!
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class SubformGeneratorType extends AbstractType
{

    private $name;
    private $builderCallback;

    /**
     * Constructor
     *
     * @param string   $name            Name
     * @param callable $builderCallback Callback
     */
    public function __construct($name, $builderCallback)
    {
        $this->name = $name;
        $this->builderCallback = $builderCallback;
    }

    /**
     * {@inhertDoc@}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        call_user_func_array($this->builderCallback, [ &$builder ]);
    }


    /**
     * {@inhertDoc@}
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * {@inhertDoc@}
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'translation_domain' => 'forms'
        ));
    }

}