Files
lucent-laravel/src/Schema/Ui/Textarea.php
T
2024-03-30 13:42:38 +02:00

72 lines
2.0 KiB
PHP

<?php
namespace Lucent\Schema\Ui;
use Lucent\Graph\Data\FieldData;
use Lucent\JsonSchema\Property\Property;
use Lucent\JsonSchema\Property\PropertyType;
use Lucent\JsonSchema\Property\TypeProperty;
use Lucent\Schema\Field\FieldDataInterface;
use Lucent\Schema\Field\FieldInfo;
use Lucent\Schema\Field\FieldInterface;
use Lucent\Schema\Field\FieldType;
use Lucent\Schema\Validator\RequiredInterface;
use PhpOption\Option;
class Textarea implements FieldInterface,FieldDataInterface, RequiredInterface
{
public FieldInfo $info;
public function __construct(
public string $name,
public string $label,
public bool $required = false,
public ?int $min = null,
public ?int $max = null,
public string $default = "",
public string $help = "",
public bool $readonly = false,
public string $group = "",
)
{
$this->info = new FieldInfo("textarea", "Textarea", FieldType::STRING);
}
public function format(FieldData $input, FieldData $output): FieldData
{
$value = $input->get($this->name);
$output->set($this->name,$value);
return $output;
}
public function failRequired(mixed $value): bool
{
return empty(trim($value));
}
public function isRequired(): bool
{
return $this->required;
}
public function toJsonSchema(): Property
{
return new TypeProperty(
type: PropertyType::STRING,
id: $this->name,
title: Option::fromValue($this->label),
description: Option::fromValue($this->help, ""),
default: Option::fromValue($this->default, ""),
minLength: Option::fromValue($this->min),
maxLength: Option::fromValue($this->max),
readOnly: Option::fromValue($this->readonly, false),
enum: none(),
comment: Option::fromValue($this->group, ""),
format: none(),
);
}
}