<?php

namespace Plainwork\Ui\Component\Form;

use Plainwork\Http\Request;
use Plainwork\Ui\Component\Form\CheckboxInput;
use Plainwork\Ui\Component\Form\AbstractFieldset;

class RadioSetInput extends AbstractFieldset
{
    private array $options = [];
    private ?string $value = null;
    private bool $isVertical = false;

    public function setVertical(bool $v = true): self
    {
        $this->isVertical = $v;

        return $this;
    }

    public function isVertical(): bool
    {
        return $this->isVertical;
    }

    public function getOptions(): array
    {
        return $this->options;
    }

    public function setOptions(array $v): self
    {
        $this->options = $v;

        return $this;
    }

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

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

        return $ret;
    }

    public function setValue(?string $v): self
    {
        $this->value = array_key_exists($v, $this->options) ? $v : null;

        return $this;
    }

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