2021-09-10 16:02:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Views\Components\Forms;
|
|
|
|
|
|
|
|
use ViewComponents\Component;
|
|
|
|
|
2024-05-09 17:55:41 +00:00
|
|
|
abstract class FormComponent extends Component
|
2021-09-10 16:02:25 +00:00
|
|
|
{
|
2024-05-09 17:55:41 +00:00
|
|
|
protected array $props = [
|
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'value',
|
2024-12-19 12:33:57 +00:00
|
|
|
'defaultValue',
|
2024-05-09 17:55:41 +00:00
|
|
|
'isRequired',
|
|
|
|
'isReadonly',
|
|
|
|
];
|
2021-09-10 16:02:25 +00:00
|
|
|
|
2024-05-09 17:55:41 +00:00
|
|
|
protected array $casts = [
|
|
|
|
'isRequired' => 'boolean',
|
|
|
|
'isReadonly' => 'boolean',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected string $id;
|
|
|
|
|
|
|
|
protected string $name;
|
2021-09-10 16:02:25 +00:00
|
|
|
|
2024-12-23 15:35:47 +00:00
|
|
|
/**
|
|
|
|
* @var string|string[]|null
|
|
|
|
*/
|
|
|
|
protected string|array|null $value = null;
|
2024-12-19 12:33:57 +00:00
|
|
|
|
2024-12-23 15:35:47 +00:00
|
|
|
/**
|
2024-12-29 13:21:50 +00:00
|
|
|
* @var string|string[]|null
|
2024-12-23 15:35:47 +00:00
|
|
|
*/
|
2024-12-29 13:21:50 +00:00
|
|
|
protected string|array|null $defaultValue = null;
|
2021-09-10 16:02:25 +00:00
|
|
|
|
2024-05-09 17:55:41 +00:00
|
|
|
protected bool $isRequired = false;
|
2021-09-10 16:02:25 +00:00
|
|
|
|
2024-05-09 17:55:41 +00:00
|
|
|
protected bool $isReadonly = false;
|
2021-09-15 15:58:21 +00:00
|
|
|
|
2022-03-04 14:33:48 +00:00
|
|
|
/**
|
|
|
|
* @param array<string, string> $attributes
|
|
|
|
*/
|
|
|
|
public function __construct(array $attributes)
|
2021-09-10 16:02:25 +00:00
|
|
|
{
|
2024-05-09 17:55:41 +00:00
|
|
|
$parentVars = get_class_vars(self::class);
|
|
|
|
$this->casts = [...$parentVars['casts'], ...$this->casts];
|
|
|
|
$this->props = [...$parentVars['props'], $this->props];
|
|
|
|
|
2021-09-10 16:02:25 +00:00
|
|
|
parent::__construct($attributes);
|
|
|
|
|
2024-05-09 17:55:41 +00:00
|
|
|
if (! isset($this->id)) {
|
2021-09-10 16:02:25 +00:00
|
|
|
$this->id = $this->name;
|
|
|
|
}
|
|
|
|
|
2024-05-09 17:55:41 +00:00
|
|
|
$this->attributes['id'] = $this->id;
|
|
|
|
$this->attributes['name'] = $this->name;
|
2022-03-25 14:37:14 +00:00
|
|
|
|
2024-05-09 17:55:41 +00:00
|
|
|
if ($this->isRequired) {
|
2023-06-21 16:17:11 +00:00
|
|
|
$this->attributes['required'] = 'required';
|
|
|
|
}
|
2021-09-15 15:58:21 +00:00
|
|
|
|
2024-05-09 17:55:41 +00:00
|
|
|
if ($this->isReadonly) {
|
2021-09-15 15:58:21 +00:00
|
|
|
$this->attributes['readonly'] = 'readonly';
|
|
|
|
}
|
2021-09-10 16:02:25 +00:00
|
|
|
}
|
2024-12-19 12:33:57 +00:00
|
|
|
|
2024-12-23 15:35:47 +00:00
|
|
|
protected function getValue(): string|array
|
2024-12-19 12:33:57 +00:00
|
|
|
{
|
2024-12-23 15:35:47 +00:00
|
|
|
$valueCast = $this->casts['value'] ?? '';
|
|
|
|
if ($valueCast === 'array') {
|
|
|
|
return old($this->name, in_array($this->value, [[], null], true) ? $this->defaultValue : $this->value) ?? [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return old(
|
|
|
|
$this->name,
|
2025-03-01 13:08:00 +00:00
|
|
|
in_array($this->value, ['', null], true) ? $this->defaultValue : $this->value,
|
2024-12-23 15:35:47 +00:00
|
|
|
) ?? '';
|
2024-12-19 12:33:57 +00:00
|
|
|
}
|
2021-09-10 16:02:25 +00:00
|
|
|
}
|