Add presenters

This commit is contained in:
philipp lang 2024-02-09 23:22:49 +01:00
parent a0bcd99642
commit 891f0c21ad
12 changed files with 116 additions and 36 deletions

View File

@ -2,6 +2,8 @@
namespace App\Form\Fields;
use App\Form\Presenters\EnumPresenter;
use App\Form\Presenters\Presenter;
use Faker\Generator;
use Illuminate\Validation\Rule;
@ -63,4 +65,9 @@ class CheckboxesField extends Field
{
return [];
}
public function getPresenter(): Presenter
{
return app(EnumPresenter::class);
}
}

View File

@ -3,6 +3,8 @@
namespace App\Form\Fields;
use App\Form\Contracts\Displayable;
use App\Form\Presenters\DatePresenter;
use App\Form\Presenters\Presenter;
use Carbon\Carbon;
use Faker\Generator;
@ -74,20 +76,8 @@ class DateField extends Field
];
}
/**
* @param mixed $value
* @return mixed
*/
public function presentValue($value)
public function getPresenter(): Presenter
{
return [
$this->key => $value,
$this->key . '_human' => $value ? Carbon::parse($value)->format('d.m.Y') : null,
];
}
public function displayAttribute(): string
{
return $this->key . '_human';
return app(DatePresenter::class);
}
}

View File

@ -2,6 +2,8 @@
namespace App\Form\Fields;
use App\Form\Presenters\DefaultPresenter;
use App\Form\Presenters\Presenter;
use Faker\Generator;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Attributes\MapInputName;
@ -81,6 +83,7 @@ abstract class Field extends Data
{
return [
$this->key => $value,
$this->getDisplayAttribute() => $this->getPresenter()->present($value),
];
}
@ -131,8 +134,13 @@ abstract class Field extends Data
];
}
public function displayAttribute(): string
public function getPresenter(): Presenter
{
return $this->key;
return app(DefaultPresenter::class);
}
public function getDisplayAttribute(): string
{
return $this->key . '_display';
}
}

View File

@ -2,6 +2,8 @@
namespace App\Form\Fields;
use App\Form\Presenters\GroupPresenter;
use App\Form\Presenters\Presenter;
use App\Group;
use App\Group\Enums\Level;
use Faker\Generator;
@ -78,20 +80,8 @@ class GroupField extends Field
return [];
}
/**
* @param mixed $value
* @return mixed
*/
public function presentValue($value)
public function getPresenter(): Presenter
{
return [
$this->key => $value,
$this->key . '_name' => Group::find($value)?->display() ?: ''
];
}
public function displayAttribute(): string
{
return $this->key . '_name';
return app(GroupPresenter::class);
}
}

View File

@ -2,6 +2,7 @@
namespace App\Form\Fields;
use App\Form\Presenters\Presenter;
use Faker\Generator;
use Illuminate\Validation\Rule;

View File

@ -0,0 +1,16 @@
<?php
namespace App\Form\Presenters;
use Carbon\Carbon;
class DatePresenter extends Presenter
{
public function present($value): string
{
return $value ? Carbon::parse($value)->format('d.m.Y') : '';
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace App\Form\Presenters;
class DefaultPresenter extends Presenter
{
/**
* @param mixed $value
*/
public function present($value): string
{
return ((string) $value) ?: '';
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace App\Form\Presenters;
class EnumPresenter extends Presenter
{
/**
* @param array<int, string> $value
*/
public function present($value): string
{
return is_array($value)
? implode(', ', $value)
: '';
}
}

View File

@ -0,0 +1,22 @@
<?php
namespace App\Form\Presenters;
use App\Group;
use Carbon\Carbon;
class GroupPresenter extends Presenter
{
/**
* @param ?int $value
*/
public function present($value): string
{
if (!$value) {
return '';
}
return Group::find($value)?->display() ?: '';
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace App\Form\Presenters;
abstract class Presenter
{
/* @var mixed */
public $value;
abstract public function present($value): string;
}

View File

@ -42,7 +42,7 @@ class ParticipantResource extends JsonResource
'name' => $field->name,
'base_type' => class_basename($field),
'id' => $field->key,
'display_attribute' => $field->displayAttribute(),
'display_attribute' => $field->getdisplayAttribute(),
];
})
];

View File

@ -23,7 +23,7 @@ class ParticipantIndexActionTest extends TestCase
$this->login()->loginNami()->withoutExceptionHandling();
$group = Group::factory()->innerName('Stamm')->create();
$form = Form::factory()
->has(Participant::factory()->data(['vorname' => 'Max', 'select' => 'A', 'stufe' => 'Pfadfinder', 'test1' => '', 'test2' => '', 'test3' => '', 'birthday' => '1991-04-20', 'bezirk' => $group->id]))
->has(Participant::factory()->data(['vorname' => 'Max', 'select' => ['A', 'B'], 'stufe' => 'Pfadfinder', 'test1' => '', 'test2' => '', 'test3' => '', 'birthday' => '1991-04-20', 'bezirk' => $group->id]))
->sections([
FormtemplateSectionRequest::new()->fields([
FormtemplateFieldRequest::type(TextField::class)->name('Vorname')->key('vorname'),
@ -41,16 +41,19 @@ class ParticipantIndexActionTest extends TestCase
$this->callFilter('form.participant.index', [], ['form' => $form])
->assertOk()
->assertJsonPath('data.0.vorname', 'Max')
->assertJsonPath('data.0.vorname_display', 'Max')
->assertJsonPath('data.0.stufe', 'Pfadfinder')
->assertJsonPath('data.0.bezirk', $group->id)
->assertJsonPath('data.0.bezirk_name', 'Stamm')
->assertJsonPath('data.0.birthday_human', '20.04.1991')
->assertJsonPath('data.0.bezirk_display', 'Stamm')
->assertJsonPath('data.0.birthday_display', '20.04.1991')
->assertJsonPath('data.0.birthday', '1991-04-20')
->assertJsonPath('data.0.select', ['A', 'B'])
->assertJsonPath('data.0.select_display', 'A, B')
->assertJsonPath('meta.columns.0.name', 'Vorname')
->assertJsonPath('meta.columns.0.base_type', class_basename(TextField::class))
->assertJsonPath('meta.columns.0.id', 'vorname')
->assertJsonPath('meta.columns.6.display_attribute', 'birthday_human')
->assertJsonPath('meta.columns.0.display_attribute', 'vorname')
->assertJsonPath('meta.columns.6.display_attribute', 'birthday_display')
->assertJsonPath('meta.columns.0.display_attribute', 'vorname_display')
->assertJsonPath('meta.active_columns', ['vorname', 'select', 'stufe', 'test1']);
}
}