2023-10-02 23:10:49 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Lucent\Schema\Ui;
|
|
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
|
use Lucent\Schema\FieldInfo;
|
|
|
|
|
use Lucent\Schema\FieldInterface;
|
|
|
|
|
use Lucent\Schema\FieldType;
|
|
|
|
|
use Lucent\Schema\Nullable;
|
|
|
|
|
use Lucent\Schema\Validator\MinMaxInterface;
|
|
|
|
|
use Lucent\Schema\Validator\RequiredInterface;
|
|
|
|
|
|
|
|
|
|
class Date implements FieldInterface, RequiredInterface, MinMaxInterface
|
|
|
|
|
{
|
|
|
|
|
public FieldInfo $info;
|
|
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
public string $name,
|
|
|
|
|
public string $label,
|
|
|
|
|
public bool $required = false,
|
|
|
|
|
public bool $nullable = false,
|
|
|
|
|
public ?Carbon $min = null,
|
|
|
|
|
public ?Carbon $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 $optionsFrom = "",
|
|
|
|
|
public string $optionsField = "",
|
|
|
|
|
public bool $optionsSuggest = false,
|
|
|
|
|
public string $group = "",
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
$this->info = new FieldInfo("date", "Date", FieldType::STRING);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function format(array $input, array $output): array
|
|
|
|
|
{
|
|
|
|
|
$value = $input[$this->name] ?? null;
|
|
|
|
|
if (empty($value)) {
|
|
|
|
|
$newValue = (new Nullable($this->nullable, null, ""))->value();
|
|
|
|
|
} else {
|
|
|
|
|
$date = Carbon::parse($value);
|
|
|
|
|
$dateFormatted = $date->format("Y-m-d");
|
|
|
|
|
$newValue = (new Nullable($this->nullable, $dateFormatted, ""))->value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$output[$this->name] = $newValue;
|
|
|
|
|
return $output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function failRequired(mixed $value): bool
|
|
|
|
|
{
|
|
|
|
|
return empty(trim($value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function failMin(mixed $value): bool
|
|
|
|
|
{
|
|
|
|
|
if (empty($this->value)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $value->lessThanOrEqualTo($this->min);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function failMax(mixed $value): bool
|
|
|
|
|
{
|
|
|
|
|
if (empty($this->value)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $value->greaterThan($this->max);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|