75 lines
2.2 KiB
PHP
75 lines
2.2 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 Text 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 $help = "",
|
|
public string $default = "",
|
|
public bool $readonly = false,
|
|
public string $optionsFrom = "",
|
|
public string $optionsField = "",
|
|
public bool $optionsSuggest = false,
|
|
public ?array $selectOptions = null,
|
|
public string $group = "",
|
|
)
|
|
{
|
|
$this->info = new FieldInfo("text", "Text", 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 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: Option::fromValue(array_keys($this->selectOptions ?? []),[]),
|
|
comment: Option::fromValue($this->group, ""),
|
|
format: none(),
|
|
);
|
|
}
|
|
|
|
public function isRequired(): bool
|
|
{
|
|
return $this->required;
|
|
}
|
|
}
|
|
|