adrema/app/Form/Fields/Field.php

137 lines
3.6 KiB
PHP
Raw Normal View History

2023-12-26 00:44:49 +01:00
<?php
namespace App\Form\Fields;
2023-12-26 20:06:57 +01:00
use Faker\Generator;
2024-02-06 01:45:25 +01:00
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
2023-12-26 00:44:49 +01:00
2024-02-06 01:45:25 +01:00
#[MapInputName(SnakeCaseMapper::class)]
abstract class Field extends Data
2023-12-26 00:44:49 +01:00
{
2024-02-08 23:09:51 +01:00
public string $key;
public string $name;
2023-12-26 00:44:49 +01:00
abstract public static function name(): string;
2023-12-26 20:06:57 +01:00
/** @return array<int, array{key: string, default: mixed, label: string, rules: array<string, mixed>}> */
2023-12-26 00:44:49 +01:00
abstract public static function meta(): array;
2023-12-26 20:06:57 +01:00
/** @return mixed */
2023-12-26 00:44:49 +01:00
abstract public static function default();
2024-02-06 01:45:25 +01:00
/** @return array<string, mixed> */
abstract public function getRegistrationRules(): array;
/** @return array<string, mixed> */
abstract public function getRegistrationAttributes(): array;
/** @return array<string, mixed> */
abstract public function getRegistrationMessages(): array;
2023-12-26 20:06:57 +01:00
/** @return array<string, mixed> */
abstract public static function fake(Generator $faker): array;
/**
* @return array<int, array<string, mixed>>
*/
2023-12-26 00:44:49 +01:00
public static function asMeta(): array
{
2023-12-26 20:06:57 +01:00
return array_map(fn ($class) => $class::allMeta(), self::classNames());
2023-12-26 00:44:49 +01:00
}
/**
2023-12-26 20:06:57 +01:00
* @return array<int, class-string<self>>
2023-12-26 00:44:49 +01:00
*/
2023-12-26 20:06:57 +01:00
private static function classNames(): array
2023-12-26 00:44:49 +01:00
{
return collect(glob(base_path('app/Form/Fields/*.php')))
->filter(fn ($fieldClass) => preg_match('/[A-Za-z]Field\.php$/', $fieldClass) === 1)
->map(fn ($fieldClass) => str($fieldClass)->replace(base_path(''), '')->replace('/app', '/App')->replace('.php', '')->replace('/', '\\')->toString())
2023-12-26 20:06:57 +01:00
->values()
->toArray();
2023-12-26 00:44:49 +01:00
}
2023-12-26 20:06:57 +01:00
public static function classFromType(string $type): ?string
{
/** @var class-string<Field> */
$fieldClass = '\\App\\Form\\Fields\\' . $type;
if (!class_exists($fieldClass)) {
return null;
}
return $fieldClass;
}
2024-02-06 01:45:25 +01:00
/**
* @param array<string, mixed> $config
*/
public static function fromConfig(array $config): static
{
return static::classFromType($config['type'])::withoutMagicalCreationFrom($config);
}
2024-02-08 23:09:51 +01:00
/**
* @param mixed $value
* @return mixed
*/
public function presentValue($value)
{
return [$this->key => $value];
}
2023-12-26 20:06:57 +01:00
/**
* @return array<string, string>
*/
public static function metaAttributes(): array
{
return collect(static::meta())->mapWithKeys(fn ($meta) => [$meta['key'] => $meta['label']])->toArray();
}
/**
* @return array<string, mixed>
**/
public static function metaRules(): array
{
$result = [];
foreach (static::meta() as $meta) {
foreach ($meta['rules'] as $fieldName => $rules) {
$result[$fieldName] = $rules;
}
}
return $result;
}
public static function type(): string
{
return class_basename(static::class);
}
/**
* @return array<string, mixed>
*/
2023-12-26 00:44:49 +01:00
public static function allMeta(): array
{
return [
2023-12-26 20:06:57 +01:00
'id' => static::type(),
2023-12-26 00:44:49 +01:00
'name' => static::name(),
'default' => [
'name' => '',
2023-12-26 20:06:57 +01:00
'type' => static::type(),
2023-12-26 01:18:59 +01:00
'columns' => ['mobile' => 2, 'tablet' => 4, 'desktop' => 6],
2023-12-26 00:44:49 +01:00
'default' => static::default(),
'required' => false,
2023-12-26 20:06:57 +01:00
...collect(static::meta())->mapWithKeys(fn ($meta) => [$meta['key'] => $meta['default']])->toArray(),
2023-12-26 00:44:49 +01:00
],
];
}
2024-02-08 23:09:51 +01:00
public function displayAttribute(): string
{
return $this->key;
}
2023-12-26 00:44:49 +01:00
}