adrema/app/Form/Fields/Field.php

182 lines
4.9 KiB
PHP
Raw Normal View History

2023-12-26 00:44:49 +01:00
<?php
namespace App\Form\Fields;
2024-03-07 00:58:14 +01:00
use App\Form\Data\ColumnData;
2024-02-16 14:18:16 +01:00
use App\Form\Enums\NamiType;
2024-03-13 13:48:35 +01:00
use App\Form\Enums\SpecialType;
2024-04-24 15:39:07 +02:00
use App\Form\Matchers\Matcher;
use App\Form\Matchers\SingleValueMatcher;
2024-02-16 14:18:16 +01:00
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;
2024-03-07 00:58:14 +01:00
use Spatie\LaravelData\Attributes\MapOutputName;
2024-02-06 01:45:25 +01:00
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
2023-12-26 00:44:49 +01:00
2024-02-06 01:45:25 +01:00
#[MapInputName(SnakeCaseMapper::class)]
2024-03-07 00:58:14 +01:00
#[MapOutputName(SnakeCaseMapper::class)]
2024-02-06 01:45:25 +01:00
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-03-07 00:58:14 +01:00
public ColumnData $columns;
public bool $forMembers;
2024-03-13 13:48:35 +01:00
public ?SpecialType $specialType = null;
2024-04-10 00:11:58 +02:00
public ?string $hint;
2024-06-07 00:48:54 +02:00
public ?string $intro;
2024-02-16 14:18:16 +01:00
2024-03-13 15:48:08 +01:00
/** @var mixed */
public $value;
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)
2024-04-08 19:22:52 +02:00
->map(fn ($fieldClass) => str($fieldClass)->replaceFirst(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-08 23:09:51 +01:00
/**
* @return mixed
*/
2024-03-14 23:54:41 +01:00
public function present()
2024-02-08 23:09:51 +01:00
{
2024-02-09 00:21:33 +01:00
return [
2024-03-14 23:54:41 +01:00
$this->key => $this->value,
2024-03-15 01:52:27 +01:00
$this->getDisplayAttribute() => $this->presentRaw(),
2024-02-09 00:21:33 +01:00
];
2024-02-08 23:09:51 +01:00
}
2024-03-15 01:52:27 +01:00
/**
* @return mixed
*/
public function presentRaw()
{
return $this->getPresenter()->present($this->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],
2024-03-13 15:48:08 +01:00
'value' => static::default(),
2024-02-16 14:18:16 +01:00
'nami_type' => null,
2024-02-19 02:32:46 +01:00
'for_members' => true,
2024-03-13 13:48:35 +01:00
'special_type' => null,
2024-04-10 00:00:20 +02:00
'hint' => '',
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
}
public function matches(string $comparator, mixed $value): bool
{
return $this->getMatcher()->setValue($this->value)->matches($comparator, $value);
}
2024-04-24 15:39:07 +02:00
public function getMatcher(): Matcher
{
return app(SingleValueMatcher::class);
}
2023-12-26 00:44:49 +01:00
}