<?php
/**
 * Event triggered when field is added to the form builder
 * allows modifing the arguments that are passed to FormBuilder:add()
 *
 * Use VisualM\FormBundle\Helper\Event\FormEvents::ON_FIELD_ADD
 * to attach a listener
 *
 * @author Elze Kool <info@visualmedia.nl>
 */

namespace VisualM\FormBundle\Helper\Event;

use RuntimeException;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormTypeInterface;
use VisualM\FormBundle\Entity\FormField as FormFieldEntity;

/**
 * Event called after populating the form builder
 * allows modifing the form just before it is about to be
 * rendered
 *
 * Use VisualM\FormBundle\Helper\Event\FormEvents::AFTER_POPULATE_BUILDER
 * to attach a listener
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class OnFieldAddEvent extends Event
{
    /**
     * Indicator that we dont want to add the field
     * @var bool
     */
    protected $doNotAdd = false;

    /**
     * Child
     * @var string|int|FormBuilderInterface
     */
    protected $child;

    /**
     * FormType
     * @var string|FormTypeInterface
     */
    protected $type;

    /**
     * Options
     * @var array
     */
    protected $options;

    /**
     * Form Field Entity
     * @var FormFieldEntity
     */
    protected $formFieldEntity;

    /**
     * Constructor
     *
     * @param string|int|FormBuilderInterface $child           Child
     * @param string|FormTypeInterface        $type            Type
     * @param array                           $options         Options
     * @param FormFieldEntity                 $formFieldEntity FormFieldEntity
     */
    public function __construct($child, $type, array $options, FormFieldEntity $formFieldEntity)
    {
        $this->child = $child;
        $this->type = $type;
        $this->options = $options;
        $this->formFieldEntity = $formFieldEntity;
    }


    /**
     * Indicate that we dont want to add field
     */
    public function doNotAdd()
    {
        $this->doNotAdd = true;
    }

    /**
     * Get indicator if we want to add form Field
     *
     * @return bool Do not add
     */
    public function getDoNotAdd()
    {
        return $this->doNotAdd;
    }

    /**
     * Get Form Field Entity
     * @return FormFieldEntity
     */
    public function getFormFieldEntity()
    {
        return $this->formFieldEntity;
    }

    /**
     * Get Child
     *
     * @return string|int|FormBuilderInterface2 Child
     **/
    public function getChild()
    {
        return $this->child;
    }

    /**
     * Set Child
     *
     * @param string|int|FormBuilderInterface $child Child
     **/
    public function setChild($child)
    {
        if (!is_string($child) AND !is_int($child) AND !($child instanceof FormBuilderInterface)) {
            throw new RuntimeException(
                sprintf(
                    'Child for %s must be string,int or implement FormBuilderInterface, %s given',
                    get_class($this),
                    is_object($child) ? get_class($child) : gettype($child)
                )
            );
        }
        $this->child = $child;
    }

    /**
     * Get Type
     *
     * @return string|FormTypeInterface Type
     **/
    public function getType()
    {        
        return $this->type;
    }

    /**
     * Set Type
     *
     * @param string|FormTypeInterface $type Type
     **/
    public function setType($type)
    {
        if (!is_string($type) AND !($child instanceof FormTypeInterface)) {
            throw new RuntimeException(
                sprintf(
                    'Type for %s must be string or implement FormTypeInterface, %s given',
                    get_class($this),
                    is_object($type) ? get_class($type) : gettype($type)
                )
            );
        }
        $this->type = $type;
    }

    /**
     * Get Options
     *
     * @return array Options
     **/
    public function getOptions()
    {
        return $this->options;
    }

    /**
     * Set Options
     *
     * @param array $options Options
     **/
    public function setOptions(array $options)
    {
        $this->options = $options;
    }



}