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

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

        $choices = $this->createChoices($formField->getWaarden());
        $constraints[] = new Assert\Choice(array('multiple' => true, 'choices' => array_values($choices)));
        if ($formField->getVerplicht()) {
            $constraints[] = new Assert\Count([ 'min' => 1]);
        }

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

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

}

