adrema/app/Form/Fields/Field.php

158 lines
4.3 KiB
PHP
Raw Normal View History

2023-12-26 00:44:49 +01:00
<?php
namespace App\Form\Fields;
2024-02-16 14:18:16 +01:00
use App\Form\Enums\NamiType;
use App\Form\Models\Form;
use App\Form\Models\Participant;
2024-02-09 23:22:49 +01:00
use App\Form\Presenters\DefaultPresenter;
use App\Form\Presenters\Presenter;
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;
2024-02-16 14:18:16 +01:00
public ?NamiType $namiType = null;
2024-02-16 14:32:16 +01:00
/**
* @param array<array-key, mixed> $input
*/
2024-02-16 14:18:16 +01:00
abstract public function afterRegistration(Form $form, Participant $participant, array $input): void;
2024-02-08 23:09:51 +01:00
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> */
2024-02-19 02:10:58 +01:00
abstract public function getRegistrationRules(Form $form): array;
2024-02-06 01:45:25 +01:00
/** @return array<string, mixed> */
2024-02-19 02:10:58 +01:00
abstract public function getRegistrationAttributes(Form $form): array;
2024-02-06 01:45:25 +01:00
/** @return array<string, mixed> */
2024-02-19 02:10:58 +01:00
abstract public function getRegistrationMessages(Form $form): array;
2024-02-06 01:45:25 +01:00
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)
{
2024-02-09 00:21:33 +01:00
return [
$this->key => $value,
2024-02-09 23:22:49 +01:00
$this->getDisplayAttribute() => $this->getPresenter()->present($value),
2024-02-09 00:21:33 +01:00
];
2024-02-08 23:09:51 +01:00
}
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,
2024-02-16 14:18:16 +01:00
'nami_type' => null,
2024-02-19 02:32:46 +01:00
'for_members' => true,
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
2024-02-09 23:22:49 +01:00
public function getPresenter(): Presenter
2024-02-08 23:09:51 +01:00
{
2024-02-09 23:22:49 +01:00
return app(DefaultPresenter::class);
}
public function getDisplayAttribute(): string
{
return $this->key . '_display';
2024-02-08 23:09:51 +01:00
}
2023-12-26 00:44:49 +01:00
}