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

namespace VisualM\FormBundle\Form\TypeHandler;

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

/**
 * Number type handler
 *
 * @author Elze Kool <info@visualmedia.nl>
 */
class NumberTypeHandler implements FormTypeHandlerInterface
{
    /**
     * {@inheritdoc}
     */
    public function getSymfonyType(FormFieldInterface $formField)
    {
        return NumberType::class;
    }

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

        if ($formField->getRegex() != '' AND $formField->getRegex() != null) {
            $constraints[] = new Assert\Regex([ 'pattern' => $formField->getRegex()]);
        }

        if ($formField->getVerplicht()) {
            $constraints[] = new Assert\NotBlank();
        }

        if (null !== $formField->getLengte()) {
            $constraints[] = new Assert\Length(['max' => $formField->getLengte()]);
        }

        return [
            'required' => $formField->getVerplicht(),
            'scale' => $formField->getSchaal(),
            'constraints' => $constraints
        ];
    }

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

