PluginProbe ʕ •ᴥ•ʔ
FAPI Member / 2.2.24
FAPI Member v2.2.24
2.2.33 2.2.32 trunk 1.9.47 2.1.18 2.2.24 2.2.25 2.2.26 2.2.28 2.2.29 2.2.30 2.2.31
fapi-member / src / Model / MembershipChange.php
fapi-member / src / Model Last commit date
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
MembershipChange.php
109 lines
1 <?php declare(strict_types=1);
2
3 namespace FapiMember\Model;
4
5
6 use DateTimeImmutable;
7 use FapiMember\Container\Container;
8 use FapiMember\Library\SmartEmailing\Types\IntType;
9 use FapiMember\Library\SmartEmailing\Types\StringType;
10 use FapiMember\Model\Enums\Format;
11 use FapiMember\Model\Enums\Types\MembershipChangeType;
12 use FapiMember\Repository\LevelRepository;
13 use FapiMember\Utils\DateTimeHelper;
14
15 class MembershipChange
16 {
17 private LevelRepository $levelRepository;
18
19 private int $userId;
20
21 private int $levelId;
22
23 private DateTimeImmutable|null $registered;
24
25 private DateTimeImmutable|null $until;
26
27 private string $type;
28
29 private DateTimeImmutable|null $timestamp;
30
31 public function __construct(array $data)
32 {
33 $this->levelRepository = Container::get(LevelRepository::class);
34
35 $this->userId = IntType::extract($data, 'user_id');
36 $this->levelId = IntType::extract($data, 'level_id');
37 $this->registered = DateTimeHelper::createOrNull($data['registered'], Format::DATE_TIME_BASIC);
38 $this->until = DateTimeHelper::createOrNull($data['until'], Format::DATE_TIME_BASIC);
39 $timestamp = DateTimeHelper::createOrNull($data['timestamp'], Format::DATE_TIME_BASIC);
40 $this->type = StringType::extract($data, 'type');
41
42 if ($timestamp === null) {
43 $timestamp = DateTimeHelper::getNow();
44 }
45
46 $this->timestamp = $timestamp;
47 }
48
49 public function isValid(): bool
50 {
51 if (!$this->levelRepository->exists($this->levelId)) {
52 return false;
53 }
54
55 return true;
56 }
57
58 public function getUserId(): int
59 {
60 return $this->userId;
61 }
62
63 public function getLevelId(): int
64 {
65 return $this->levelId;
66 }
67
68 public function getType(): string
69 {
70 return $this->type;
71 }
72
73
74 public function getTimestamp(): DateTimeImmutable|null
75 {
76 return $this->timestamp;
77 }
78
79 public function isActive(): bool
80 {
81 return !in_array($this->type, [MembershipChangeType::EXPIRED, MembershipChangeType::DELETED]);
82 }
83
84 public function toArray(): array
85 {
86 return [
87 'user_id' => $this->userId,
88 'level_id' => $this->levelId,
89 'timestamp' => $this->timestamp?->format(Format::DATE_TIME),
90 'registered' => $this->registered?->format(Format::DATE_TIME),
91 'until' => $this->until?->format(Format::DATE_TIME) ?? null,
92 'type' => $this->type,
93 ];
94 }
95
96 public function toJson(): array
97 {
98 return [
99 'user_id' => $this->userId,
100 'level' => $this->levelRepository->getLevelById($this->levelId)->toArray(),
101 'timestamp' => $this->timestamp?->format(Format::DATE_TIME),
102 'registered' => $this->registered?->format(Format::DATE_TIME),
103 'until' => $this->until?->format(Format::DATE_TIME) ?? null,
104 'type' => $this->type,
105 ];
106 }
107
108 }
109