<?php

namespace Plainwork\Ui\Component\Form;

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

class FileInput extends AbstractInput
{
    private ?object $value = null;

    public function grab(Request $request): ?object
    {
        $ret = null;

        $n = $this->getName();
        $file = $_FILES[$n] ?? null;

        if (null === $file) {
            return $ret;
        }

  // if ($file['error'] !== UPLOAD_ERR_OK) {
        // die("Upload failed with error code: " . $file['error']);
    // }
        $ret = new \stdClass();

        $ret->name = $file['name'];
        $ret->tmpName = $file['tmp_name'];
        $ret->size = $file['size'];
        $ret->type = $file['type'];
        $ret->error = $file['error'];

        return $ret;
    }

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

        return $this;
    }

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