<?php

namespace Plainwork\Ui\Component\Form;

use Plainwork\Http\Request;
use Plainwork\Ui\Component\Form\AbstractInput;

class NumberInput extends AbstractInput
{
    private ?int $value = null;
    private float $min = 1;
    private float $max = 99;
    private float $step = 1;

    public function grab(Request $request): ?int
    {
        $ret = $request->getPostParam($this->getName());
        if (null !== $ret) {
            $ret = (int) $ret;
        }

        return $ret;
    }

    public function setValue(int $v): self
    {
        $this->value = $v;

        return $this;
    }

    public function getValue(): ?int
    {
        return $this->value;
    }

    public function setMin(int $min): self
    {
        $this->min = $min;

        if (null === $this->value) {
            $this->setValue($min);
        }

        return $this;
    }

    public function getMin(): float
    {
        return $this->min;
    }

    public function setMax(int $max): self
    {
        $this->max = $max;

        return $this;
    }

    public function getMax(): float
    {
        return $this->max;
    }

    public function setStep(int $v): self
    {
        $this->step = $v;

        return $this;
    }

    public function getStep(): float
    {
        return $this->step;
    }
}
