adrema/app/Contribution/Data/MemberData.php

95 lines
2.4 KiB
PHP
Raw Normal View History

2023-05-17 00:22:43 +02:00
<?php
namespace App\Contribution\Data;
use App\Gender;
use App\Member\Member;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Spatie\LaravelData\Data;
class MemberData extends Data
{
public function __construct(
public string $firstname,
public string $lastname,
public string $address,
public string $zip,
public string $location,
public Carbon $birthday,
public ?Gender $gender,
public bool $isLeader
) {
}
/**
* @param array<int, int> $ids
*
* @return Collection<int, static>
*/
public static function fromModels(array $ids): Collection
{
2024-09-21 22:46:38 +02:00
return Member::whereIn('id', $ids)->orderByRaw('lastname, firstname')->get()->map(fn ($member) => self::factory()->withoutMagicalCreation()->from([
2023-05-17 00:22:43 +02:00
...$member->toArray(),
'birthday' => $member->birthday->toAtomString(),
'isLeader' => $member->isLeader(),
'gender' => $member->gender,
]))->toBase();
}
/**
* @param array<int, ContributionMemberData> $data
*
* @return Collection<int, static>
*/
public static function fromApi(array $data): Collection
{
2024-09-21 22:46:38 +02:00
return collect($data)->map(fn ($member) => self::factory()->withoutMagicalCreation()->from([
2023-05-17 00:22:43 +02:00
...$member,
'birthday' => Carbon::parse($member['birthday'])->toAtomString(),
'gender' => Gender::fromString($member['gender']),
'isLeader' => $member['is_leader'],
]));
}
public function fullname(): string
{
2024-09-21 22:46:38 +02:00
return $this->firstname . ' ' . $this->lastname;
2023-05-17 00:22:43 +02:00
}
public function separatedName(): string
{
2024-09-21 22:46:38 +02:00
return $this->lastname . ', ' . $this->firstname;
2023-05-17 00:22:43 +02:00
}
public function fullAddress(): string
{
2024-09-21 22:46:38 +02:00
return $this->address . ', ' . $this->zip . ' ' . $this->location;
2023-05-17 00:22:43 +02:00
}
public function city(): string
{
2024-09-21 22:46:38 +02:00
return $this->zip . ' ' . $this->location;
}
2023-05-17 00:22:43 +02:00
public function age(): string
{
return (string) $this->birthday->diffInYears(now()) ?: '';
}
public function birthYear(): string
{
return (string) $this->birthday->year;
}
2024-12-13 01:59:09 +01:00
public function birthdayHuman(): string
{
return $this->birthday->format('d.m.Y');
}
public function genderLetter(): string
{
return $this->gender->short;
}
2023-05-17 00:22:43 +02:00
}