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

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

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

}

