Files
lucent-laravel/src/Schema/Ui/Textarea.php
T

72 lines
2.0 KiB
PHP
Raw Normal View History

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\PropertyType;
use Lucent\JsonSchema\Property\TypeProperty;
2024-03-21 22:33:41 +02:00
use Lucent\Record\RecordData;
use Lucent\Schema\Field\FieldDataInterface;
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\RequiredInterface;
2024-03-14 22:12:26 +02:00
use PhpOption\Option;
2023-10-02 23:10:49 +03:00
2024-03-21 22:33:41 +02:00
class Textarea implements FieldInterface,FieldDataInterface, RequiredInterface
2023-10-02 23:10:49 +03:00
{
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 = "",
2023-10-21 19:40:24 +03:00
public string $help = "",
2023-10-02 23:10:49 +03:00
public bool $readonly = false,
public string $group = "",
)
{
$this->info = new FieldInfo("textarea", "Textarea", FieldType::STRING);
}
2024-03-21 22:33:41 +02:00
public function format(RecordData $input, RecordData $output): RecordData
2023-10-02 23:10:49 +03:00
{
2024-03-21 22:33:41 +02:00
$value = $input->get($this->name);
$output->set($this->name,$value);
2023-10-02 23:10:49 +03:00
return $output;
}
public function failRequired(mixed $value): bool
{
return empty(trim($value));
}
2024-03-14 22:12:26 +02:00
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(),
);
}
2023-10-02 23:10:49 +03:00
}