<?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\FormField;

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

        $choices = $this->createChoices($formField->getWaarden());
        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(FormField $formField, $value)
    {
        return $value;
    }

}

