2023-10-02 23:10:49 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Lucent\Schema\Ui;
|
|
|
|
|
|
|
|
|
|
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 Number 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 int $decimals = 0,
|
|
|
|
|
public ?int $min = null,
|
|
|
|
|
public ?int $max = null,
|
|
|
|
|
public ?float $default = null,
|
|
|
|
|
public bool $readonly = false,
|
2023-10-21 19:40:24 +03:00
|
|
|
public string $help = "",
|
2023-10-02 23:10:49 +03:00
|
|
|
public string $optionsFrom = "",
|
|
|
|
|
public string $optionsField = "",
|
|
|
|
|
public bool $optionsSuggest = false,
|
|
|
|
|
public string $group = "",
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
$this->info = new FieldInfo("number", "Number", FieldType::NUMBER);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function format(array $input, array $output): array
|
|
|
|
|
{
|
|
|
|
|
$value = $input[$this->name] ?? null;
|
|
|
|
|
if (!is_numeric($value)) {
|
|
|
|
|
$newValue = null;
|
|
|
|
|
} else {
|
|
|
|
|
$newValue = number_format(
|
|
|
|
|
$value,
|
|
|
|
|
$this->decimals,
|
|
|
|
|
".",
|
|
|
|
|
""
|
|
|
|
|
);
|
|
|
|
|
$newValue = $this->decimals === 0 ? (int)$newValue : floatval($newValue);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$output[$this->name] = (new Nullable($this->nullable, $newValue, 0))->value();
|
|
|
|
|
return $output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function failRequired(mixed $value): bool
|
|
|
|
|
{
|
|
|
|
|
return is_null($value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function failMin(mixed $value): bool
|
|
|
|
|
{
|
|
|
|
|
if (is_null($value)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $value < $this->min;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function failMax(mixed $value): bool
|
|
|
|
|
{
|
|
|
|
|
if (is_null($value)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $value > $this->min;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|