adrema/app/Membership/MembershipResource.php

65 lines
2.1 KiB
PHP
Raw Normal View History

2021-08-22 22:23:17 +02:00
<?php
namespace App\Membership;
2023-09-12 16:54:13 +02:00
use App\Activity;
use App\Lib\HasMeta;
use App\Member\Data\NestedGroup;
use App\Member\Member;
2021-08-22 22:23:17 +02:00
use Illuminate\Http\Resources\Json\JsonResource;
2022-01-02 12:32:57 +01:00
/**
* @mixin \App\Member\Membership
*/
2021-08-22 22:23:17 +02:00
class MembershipResource extends JsonResource
{
/**
* Transform the resource into an array.
*
2022-03-11 20:19:17 +01:00
* @param \Illuminate\Http\Request $request
*
2021-08-22 22:23:17 +02:00
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'group_id' => $this->group_id,
2021-08-22 22:23:17 +02:00
'activity_id' => $this->activity_id,
'activity_name' => $this->activity->name,
'subactivity_id' => $this->subactivity_id,
2022-11-16 22:28:42 +01:00
'subactivity_name' => $this->subactivity?->name,
2023-02-27 22:00:27 +01:00
'human_date' => $this->from->format('d.m.Y'),
2022-12-14 15:49:12 +01:00
'promised_at' => $this->promised_at?->format('Y-m-d'),
2023-09-05 16:29:22 +02:00
'is_active' => $this->isActive(),
2023-09-12 16:54:13 +02:00
'links' => [
'update' => route('membership.update', ['membership' => $this->getModel()]),
'destroy' => route('membership.destroy', ['membership' => $this->getModel()]),
]
];
}
/**
* @return array<string, mixed>
*/
public static function memberMeta(Member $member): array
{
$activities = Activity::with('subactivities')->get();
return [
'links' => [
'store' => route('member.membership.store', ['member' => $member]),
],
'groups' => NestedGroup::cacheForSelect(),
'activities' => $activities->map(fn ($activity) => ['id' => $activity->id, 'name' => $activity->name]),
'subactivities' => $activities->mapWithKeys(fn ($activity) => [$activity->id => $activity->subactivities->map(fn ($subactivity) => ['id' => $subactivity->id, 'name' => $subactivity->name])]),
'default' => [
'group_id' => $member->group_id,
'activity_id' => null,
'subactivity_id' => null,
'promised_at' => null,
],
2021-08-22 22:23:17 +02:00
];
}
}