<?php
/**
 * Textarea type handler
 *
 * @author Elze Kool <info@visualmedia.nl>
 */

namespace VisualM\FormBundle\Form\TypeHandler;

use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Validator\Constraints as Assert;
use VisualM\FormBundle\Entity\FormField;

/**
 * Textarea type handler
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class TextareaTypeHandler implements FormTypeHandlerInterface
{
    /**
     * {@inheritdoc}
     */
    public function getSymfonyType(FormField $formField)
    {
        return TextareaType::class;
    }

    /**
     * {@inheritdoc}
     */
    public function getOptions(FormField $formField)
    {
        $constraints = [];

        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,
            'attr' => [ 'rows' => $formField->getRijen()]
        ];
        
    }

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

}

