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

51 lines
1.3 KiB
PHP
Raw Normal View History

2023-10-06 18:47:50 +03:00
<?php
namespace Lucent\Schema\Ui;
use Illuminate\Support\Str;
use Lucent\Schema\FieldInfo;
use Lucent\Schema\FieldInterface;
use Lucent\Schema\FieldType;
use Lucent\Schema\Nullable;
use Lucent\Schema\Validator\RequiredInterface;
class Slug implements FieldInterface, RequiredInterface
{
public FieldInfo $info;
public function __construct(
public string $name,
public string $label,
public bool $required = false,
public bool $nullable = 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-06 18:47:50 +03:00
public bool $readonly = false,
public string $source = "",
public string $group = "",
)
{
$this->info = new FieldInfo("slug", "Slug", FieldType::STRING);
}
public function format(array $input, array $output): array
{
2023-10-25 14:19:44 +03:00
$value = !empty($input[$this->name]) ? (string)$input[$this->name] : null;
2023-10-06 18:47:50 +03:00
if(empty($value)){
$value = Str::slug($input[$this->source]);
}
$output[$this->name] = (new Nullable($this->nullable, $value, ""))->value();
return $output;
}
public function failRequired(mixed $value): bool
{
return empty(trim($value));
}
}