<?php
/**
 * SelectList 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;

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

        $choices = $this->createChoices($formField->getWaarden());
        if ($formField->getVerplicht()) {
            $constraints[] = new Assert\Count([ 'min' => 1, 'max' => count($choices)]);
        }

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

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

}

