<?php
/**
 * OptionList type handler
 *
 * @author Elze Kool <info@visualmedia.nl>
 */

namespace VisualM\FormBundle\Form\TypeHandler;

use Symfony\Component\Validator\Constraints as Assert;
use VisualM\FormBundle\Entity\FormFieldInterface;

/**
 * OptionList type handler
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class OptionListTypeHandler extends AbstractChoiceTypeHandler
{
    /**
     * {@inheritdoc}
     */
    public function getOptions(FormFieldInterface $formField)
    {
        $constraints = [];

        $choices = $this->createChoices($formField->getWaarden());
        $constraints[] = new Assert\Choice(array('choices' => array_values($choices)));
        if ($formField->getRegex() != '' AND $formField->getRegex() != null) {
            $constraints[] = new Assert\Regex([ 'pattern' => $formField->getRegex()]);
        }

        if ($formField->getVerplicht()) {
            $constraints[] = new Assert\NotBlank();
        }

        return [
            'required' => $formField->getVerplicht(),
            'constraints' => $constraints,
            'choices' => $choices,
            'multiple' => false,
            'expanded' => true
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function normalizeValue(FormFieldInterface $formField, $value)
    {
        return $value;
    }

}

