<?php

namespace Plainwork\Ui\Component\Form;

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

class TextInput extends AbstractInput
{
    private string $value = '';

    public function grab(Request $request): ?string
    {
        $ret = $request->getPostParam($this->getName());

        if ($this->isRequired()) {
            if ((null === $ret) or (!strlen($ret))) {
                $this->addError($this->getRequiredLabel());
            }
        }

        return $ret;
    }

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

        return $this;
    }

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