2024-02-08 21:04:00 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Form\Resources;
|
|
|
|
|
2024-02-08 23:09:51 +01:00
|
|
|
use App\Form\Fields\Field;
|
2024-02-08 21:04:00 +01:00
|
|
|
use App\Form\Models\Form;
|
2024-02-09 00:21:33 +01:00
|
|
|
use App\Form\Models\Participant;
|
2024-02-08 21:04:00 +01:00
|
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
|
2024-02-09 00:21:33 +01:00
|
|
|
/**
|
|
|
|
* @mixin Participant
|
|
|
|
*/
|
2024-02-08 21:04:00 +01:00
|
|
|
class ParticipantResource extends JsonResource
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Transform the resource into an array.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
2024-02-09 00:21:33 +01:00
|
|
|
* @return array<string, mixed>
|
2024-02-08 21:04:00 +01:00
|
|
|
*/
|
|
|
|
public function toArray($request)
|
|
|
|
{
|
2024-02-08 23:09:51 +01:00
|
|
|
$attributes = collect([]);
|
|
|
|
|
|
|
|
foreach ($this->form->getFields() as $field) {
|
|
|
|
$attributes = $attributes->merge(Field::fromConfig($field)->presentValue($this->data[$field['key']]));
|
|
|
|
}
|
|
|
|
|
2024-02-09 00:21:33 +01:00
|
|
|
return $attributes->toArray();
|
2024-02-08 21:04:00 +01:00
|
|
|
}
|
|
|
|
|
2024-02-09 00:21:33 +01:00
|
|
|
/**
|
|
|
|
* @return array<string, mixed>
|
|
|
|
*/
|
2024-02-08 21:04:00 +01:00
|
|
|
public static function meta(Form $form): array
|
|
|
|
{
|
|
|
|
return [
|
2024-02-21 23:18:31 +01:00
|
|
|
'form_meta' => $form->meta,
|
|
|
|
'links' => [
|
|
|
|
'update_form_meta' => route('form.update-meta', ['form' => $form]),
|
|
|
|
],
|
2024-02-20 01:04:53 +01:00
|
|
|
'columns' => $form->getFields()
|
|
|
|
->map(fn ($field) => Field::fromConfig($field))
|
|
|
|
->map(fn ($field) => [
|
2024-02-08 23:09:51 +01:00
|
|
|
'name' => $field->name,
|
|
|
|
'base_type' => class_basename($field),
|
|
|
|
'id' => $field->key,
|
2024-02-09 23:22:49 +01:00
|
|
|
'display_attribute' => $field->getdisplayAttribute(),
|
2024-02-20 01:04:53 +01:00
|
|
|
])
|
2024-02-08 21:04:00 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|