adrema/app/Form/Fields/RadioField.php

101 lines
2.3 KiB
PHP
Raw Normal View History

2023-12-26 00:44:49 +01:00
<?php
namespace App\Form\Fields;
2024-12-12 00:30:59 +01:00
use App\Form\Contracts\Filterable;
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;
2023-12-26 20:06:57 +01:00
use Faker\Generator;
2024-02-06 01:45:25 +01:00
use Illuminate\Validation\Rule;
2023-12-26 20:06:57 +01:00
2024-12-12 00:30:59 +01:00
class RadioField extends Field implements Filterable
2023-12-26 00:44:49 +01:00
{
2024-02-06 01:45:25 +01:00
public bool $required;
/** @var array<int, string> */
public array $options;
2024-04-16 23:15:05 +02:00
public bool $allowcustom;
2024-02-06 01:45:25 +01:00
2023-12-26 00:44:49 +01:00
public static function name(): string
{
return 'Radio';
}
public static function meta(): array
{
return [
2023-12-26 20:24:57 +01:00
['key' => 'options', 'default' => [], 'rules' => ['options' => 'present|array', 'options.*' => 'required|string'], 'label' => 'Optionen'],
2024-04-12 15:23:18 +02:00
['key' => 'required', 'default' => true, 'rules' => ['required' => 'present|boolean'], 'label' => 'Erforderlich'],
2024-04-16 23:15:05 +02:00
['key' => 'allowcustom', 'default' => false, 'rules' => ['required' => 'present|boolean'], 'label' => 'Eigene Option erlauben'],
2023-12-26 00:44:49 +01:00
];
}
public static function default()
{
return null;
}
2023-12-26 20:06:57 +01:00
public static function fake(Generator $faker): array
{
2023-12-27 22:39:23 +01:00
return [
'options' => $faker->words(4),
'required' => $faker->boolean(),
2024-04-16 23:15:05 +02:00
'allowcustom' => $faker->boolean(),
2023-12-27 22:39:23 +01:00
];
2023-12-26 20:06:57 +01:00
}
2024-02-06 01:45:25 +01:00
/**
* @inheritdoc
*/
2024-02-19 02:10:58 +01:00
public function getRegistrationRules(Form $form): array
2024-02-06 01:45:25 +01:00
{
2024-04-16 23:15:05 +02:00
$rules = $this->required ? ['required', 'string'] : ['nullable', 'string'];
if (!$this->allowcustom) {
$rules[] = Rule::in($this->options);
}
2024-02-06 01:45:25 +01:00
return [
2024-04-16 23:15:05 +02:00
$this->key => $rules
2024-02-06 01:45:25 +01:00
];
}
/**
* @inheritdoc
*/
2024-02-19 02:10:58 +01:00
public function getRegistrationAttributes(Form $form): array
2024-02-06 01:45:25 +01:00
{
return [$this->key => $this->name];
}
/**
* @inheritdoc
*/
2024-02-19 02:10:58 +01:00
public function getRegistrationMessages(Form $form): array
2024-02-06 01:45:25 +01:00
{
return [];
}
2024-02-16 14:18:16 +01:00
2024-02-16 14:32:16 +01:00
/**
* @inheritdoc
*/
2024-02-16 14:18:16 +01:00
public function afterRegistration(Form $form, Participant $participant, array $input): void
{
}
public function getMatcher(): Matcher
{
return app(SingleValueMatcher::class);
}
2024-12-12 00:30:59 +01:00
public function filter($value): string
{
if (is_null($value)) {
return "{$this->key} IS NULL";
}
return $this->key . ' = \'' . $value . '\'';
}
2023-12-26 00:44:49 +01:00
}