Enums
11 months ago
ApiConnection.php
11 months ago
MemberLevel.php
11 months ago
MemberSection.php
11 months ago
Membership.php
11 months ago
MembershipChange.php
11 months ago
Page.php
11 months ago
Settings.php
11 months ago
User.php
11 months ago
Membership.php
105 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace FapiMember\Model; |
| 4 | |
| 5 | use DateTimeImmutable; |
| 6 | use FapiMember\Model\Enums\Format; |
| 7 | use FapiMember\Library\SmartEmailing\Types\BoolType; |
| 8 | use FapiMember\Utils\DateTimeHelper; |
| 9 | use FapiMember\Utils\DateTimesImmutable; |
| 10 | use FapiMember\Library\SmartEmailing\Types\IntType; |
| 11 | |
| 12 | class Membership |
| 13 | { |
| 14 | private int $levelId; |
| 15 | private int $userId; |
| 16 | private DateTimeImmutable|null $registered; |
| 17 | private DateTimeImmutable|null $until; |
| 18 | private bool $isUnlimited; |
| 19 | |
| 20 | public function __construct($data) |
| 21 | { |
| 22 | $this->levelId = IntType::extract($data, 'level_id'); |
| 23 | $this->userId = IntType::extract($data, 'user_id'); |
| 24 | $this->registered = DateTimesImmutable::extractOrNull($data, 'registered'); |
| 25 | $this->until = DateTimesImmutable::extractOrNull($data, 'until'); |
| 26 | $this->isUnlimited = BoolType::extractOrNull($data, 'is_unlimited') ?? false; |
| 27 | } |
| 28 | |
| 29 | public function getLevelId(): int |
| 30 | { |
| 31 | return $this->levelId; |
| 32 | } |
| 33 | |
| 34 | public function getUserId(): int |
| 35 | { |
| 36 | return $this->userId; |
| 37 | } |
| 38 | |
| 39 | public function getRegistered(): DateTimeImmutable|null |
| 40 | { |
| 41 | return $this->registered; |
| 42 | } |
| 43 | |
| 44 | public function getUntil(): DateTimeImmutable|null |
| 45 | { |
| 46 | return $this->until; |
| 47 | } |
| 48 | |
| 49 | public function isUnlimited(): bool |
| 50 | { |
| 51 | return $this->isUnlimited; |
| 52 | } |
| 53 | |
| 54 | public function setRegistered(DateTimeImmutable|null $registered): void |
| 55 | { |
| 56 | $this->registered = DateTimesImmutable::fromOrNull($registered); |
| 57 | } |
| 58 | |
| 59 | public function setUntil(DateTimeImmutable|null $until): void |
| 60 | { |
| 61 | $this->until = DateTimesImmutable::fromOrNull($until); |
| 62 | } |
| 63 | |
| 64 | public function setIsUnlimited(bool $isUnlimited): void |
| 65 | { |
| 66 | $this->isUnlimited = BoolType::fromOrNull($isUnlimited); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * @return array<mixed> |
| 71 | */ |
| 72 | public function jsonSerialize(): array |
| 73 | { |
| 74 | return array( |
| 75 | 'level' => $this->levelId, |
| 76 | 'registered' => $this->registered->format(Format::DATE_TIME) ?? null, |
| 77 | 'until' => $this->until?->format(Format::DATE_TIME) ?? null, |
| 78 | 'isUnlimited' => $this->isUnlimited, |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | public function toMembershipChange(string $type, DateTimeImmutable|null $timestamp = null): MembershipChange |
| 83 | { |
| 84 | return new MembershipChange([ |
| 85 | 'level_id' => $this->levelId, |
| 86 | 'user_id' => $this->userId, |
| 87 | 'type' => $type, |
| 88 | 'registered' => $this->registered?->format(Format::DATE_TIME_BASIC), |
| 89 | 'until' => $this->until?->format(Format::DATE_TIME_BASIC), |
| 90 | 'timestamp' => $timestamp?->format(Format::DATE_TIME_BASIC), |
| 91 | ]); |
| 92 | } |
| 93 | |
| 94 | public function toArray(): array |
| 95 | { |
| 96 | return [ |
| 97 | 'level' => $this->levelId, |
| 98 | 'user_id' => $this->userId, |
| 99 | 'registered' => $this->registered->format(Format::DATE_TIME) ?? null, |
| 100 | 'until' => $this->until?->format(Format::DATE_TIME) ?? null, |
| 101 | 'is_unlimited' => $this->isUnlimited, |
| 102 | ]; |
| 103 | } |
| 104 | } |
| 105 |