Files
lucent-laravel/src/Schema/Ui/File.php
T
2024-03-23 12:12:13 +02:00

75 lines
1.7 KiB
PHP

<?php
namespace Lucent\Schema\Ui;
use Lucent\JsonSchema\Property\Property;
use Lucent\JsonSchema\Property\RefProperty;
use Lucent\Schema\Field\FieldInfo;
use Lucent\Schema\Field\FieldInterface;
use Lucent\Schema\Field\FieldType;
use Lucent\Schema\Validator\MinMaxInterface;
use Lucent\Support\Collection;
use PhpOption\Option;
class File implements FieldInterface, MinMaxInterface
{
public FieldInfo $info;
/**
* @param string[] $collections
*/
public function __construct(
public string $name,
public string $label,
public string $mime = "",
public string $help = "",
public ?int $min = null,
public ?int $max = null,
public array $collections = [],
public string $data = "",
public string $group = "",
)
{
$this->info = new FieldInfo("file", "File", FieldType::FILE);
}
public function failMin(mixed $value): bool
{
if (is_null($value)) {
return false;
}
return count($value) < $this->min;
}
public function failMax(mixed $value): bool
{
if (is_null($value)) {
return false;
}
return count($value) < $this->min;
}
public function toJsonSchema(): Property
{
return new RefProperty(
id: $this->name,
title: Option::fromValue($this->label),
description: Option::fromValue($this->help, ""),
_ref: new Collection($this->collections),
minItems: Option::fromValue($this->min),
maxItems: Option::fromValue($this->max),
comment: Option::fromValue($this->group, ""),
);
}
public function isRequired(): bool
{
return $this->min > 0;
}
}