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

/**
 * CheckboxList type handler
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class CheckboxListTypeHandler 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' => true
        ];
    }

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

