2023-10-02 23:10:49 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Lucent\Schema\Ui;
|
|
|
|
|
|
2024-03-14 22:12:26 +02:00
|
|
|
use Lucent\JsonSchema\Property\Property;
|
|
|
|
|
use Lucent\JsonSchema\Property\RefProperty;
|
|
|
|
|
use Lucent\Primitive\Collection;
|
2024-03-21 22:33:41 +02:00
|
|
|
use Lucent\Schema\Field\FieldInfo;
|
|
|
|
|
use Lucent\Schema\Field\FieldInterface;
|
|
|
|
|
use Lucent\Schema\Field\FieldType;
|
2023-10-02 23:10:49 +03:00
|
|
|
use Lucent\Schema\Validator\MinMaxInterface;
|
2024-03-14 22:12:26 +02:00
|
|
|
use PhpOption\Option;
|
2023-10-02 23:10:49 +03:00
|
|
|
|
|
|
|
|
class File implements FieldInterface, MinMaxInterface
|
|
|
|
|
{
|
|
|
|
|
public FieldInfo $info;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string[] $collections
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
public string $name,
|
|
|
|
|
public string $label,
|
|
|
|
|
public string $mime = "",
|
2023-10-21 19:40:24 +03:00
|
|
|
public string $help = "",
|
2023-10-02 23:10:49 +03:00
|
|
|
public ?int $min = null,
|
|
|
|
|
public ?int $max = null,
|
|
|
|
|
public array $collections = [],
|
2024-03-21 22:33:41 +02:00
|
|
|
public string $data = "",
|
2023-10-02 23:10:49 +03:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-14 22:12:26 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-02 23:10:49 +03:00
|
|
|
}
|
|
|
|
|
|