adrema/app/Form/Fields/CheckboxField.php

97 lines
2.1 KiB
PHP
Raw Normal View History

2023-12-26 01:05:45 +01:00
<?php
namespace App\Form\Fields;
2024-12-12 00:30:59 +01:00
use App\Form\Contracts\Filterable;
use App\Form\Matchers\BooleanMatcher;
use App\Form\Matchers\Matcher;
2024-02-16 14:18:16 +01:00
use App\Form\Models\Form;
use App\Form\Models\Participant;
2024-04-12 15:42:43 +02:00
use App\Form\Presenters\BooleanPresenter;
use App\Form\Presenters\Presenter;
2023-12-26 20:06:57 +01:00
use Faker\Generator;
2024-12-12 00:30:59 +01:00
class CheckboxField extends Field implements Filterable
2023-12-26 01:05:45 +01:00
{
2024-02-06 01:45:25 +01:00
public bool $required;
public string $description;
2023-12-26 01:05:45 +01:00
public static function name(): string
{
return 'Checkbox';
}
public static function meta(): array
{
return [
2023-12-26 20:06:57 +01:00
['key' => 'description', 'default' => '', 'rules' => ['description' => 'required|string'], 'label' => 'Beschreibung'],
2024-04-12 15:23:18 +02:00
['key' => 'required', 'default' => true, 'rules' => ['required' => 'present|boolean'], 'label' => 'Erforderlich'],
2023-12-26 01:05:45 +01:00
];
}
public static function default()
{
return false;
}
2023-12-26 20:06:57 +01:00
public static function fake(Generator $faker): array
{
2023-12-27 22:39:23 +01:00
return [
'description' => $faker->text(),
'required' => $faker->boolean(),
];
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
{
return [
$this->key => $this->required ? ['boolean', 'accepted'] : ['present', 'boolean'],
];
}
/**
* @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
{
}
2024-04-12 15:42:43 +02:00
public function getPresenter(): Presenter
{
return app(BooleanPresenter::class);
}
public function getMatcher(): Matcher
{
return app(BooleanMatcher::class);
}
2024-12-12 00:30:59 +01:00
public function filter($value): string
{
$asString = $value ? 'true' : 'false';
return "{$this->key} = $asString";
}
2023-12-26 01:05:45 +01:00
}