Enums
7 months ago
ApiConnection.php
7 months ago
MemberLevel.php
7 months ago
MemberSection.php
7 months ago
Membership.php
7 months ago
MembershipChange.php
7 months ago
Page.php
7 months ago
Settings.php
7 months ago
User.php
7 months ago
User.php
111 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace FapiMember\Model; |
| 4 | |
| 5 | use DateTimeImmutable; |
| 6 | use FapiMember\Library\SmartEmailing\Types\IntArray; |
| 7 | use FapiMember\Library\SmartEmailing\Types\IntType; |
| 8 | use FapiMember\Library\SmartEmailing\Types\StringArray; |
| 9 | use FapiMember\Library\SmartEmailing\Types\StringType; |
| 10 | use FapiMember\Model\Enums\Format; |
| 11 | use FapiMember\Utils\DateTimeHelper; |
| 12 | |
| 13 | class User |
| 14 | { |
| 15 | private int $id; |
| 16 | private string $email; |
| 17 | private string|null $login; |
| 18 | private string|null $loginName; |
| 19 | private string|null $firstName; |
| 20 | private string|null $lastName; |
| 21 | private DateTimeImmutable|null $createDate; |
| 22 | private array $roles; |
| 23 | private array|null $levelIds; |
| 24 | private string|null $picture; |
| 25 | |
| 26 | public function __construct(array $data) |
| 27 | { |
| 28 | $this->id = IntType::extractOrNull($data, 'id'); |
| 29 | $this->email = StringType::extract($data, 'email'); |
| 30 | $this->login = StringType::extractOrNull($data, 'login'); |
| 31 | $this->loginName = StringType::extractOrNull($data, 'login_name'); |
| 32 | $this->firstName = StringType::extractOrNull($data, 'first_name'); |
| 33 | $this->lastName = StringType::extractOrNull($data, 'last_name'); |
| 34 | $this->createDate = DateTimeHelper::createOrNull( |
| 35 | StringType::extractOrNull($data, 'create_date'), |
| 36 | Format::DATE_TIME_BASIC, |
| 37 | ); |
| 38 | |
| 39 | $roles = StringArray::extractOrNull($data, 'roles', true); |
| 40 | |
| 41 | if ($roles === null) { |
| 42 | $roles = []; |
| 43 | } |
| 44 | |
| 45 | $this->roles = $roles; |
| 46 | $this->levelIds = IntArray::extractOrNull($data, 'level_ids', true); |
| 47 | $this->picture = StringType::extractOrNull($data, 'picture', true); |
| 48 | } |
| 49 | |
| 50 | public function getId(): int |
| 51 | { |
| 52 | return $this->id; |
| 53 | } |
| 54 | |
| 55 | public function getEmail(): string |
| 56 | { |
| 57 | return $this->email; |
| 58 | } |
| 59 | |
| 60 | public function getLoginName(): string|null |
| 61 | { |
| 62 | return $this->loginName; |
| 63 | } |
| 64 | |
| 65 | public function getRoles(): array |
| 66 | { |
| 67 | return $this->roles; |
| 68 | } |
| 69 | |
| 70 | public function isMemberOrSubscriber(): bool |
| 71 | { |
| 72 | return in_array('member', $this->roles) || in_array('subscriber', $this->roles); |
| 73 | } |
| 74 | |
| 75 | public function getFirstName(): string|null |
| 76 | { |
| 77 | return $this->firstName; |
| 78 | } |
| 79 | |
| 80 | public function getLastName(): string|null |
| 81 | { |
| 82 | return $this->lastName; |
| 83 | } |
| 84 | |
| 85 | public function getLogin(): string|null |
| 86 | { |
| 87 | return $this->login; |
| 88 | } |
| 89 | |
| 90 | public function setLevelIds(array $levelIds): void |
| 91 | { |
| 92 | $this->levelIds = $levelIds; |
| 93 | } |
| 94 | |
| 95 | public function toArray(): array |
| 96 | { |
| 97 | return [ |
| 98 | 'id' => $this->id, |
| 99 | 'email' => $this->email, |
| 100 | 'first_name' => $this->firstName, |
| 101 | 'last_name' => $this->lastName, |
| 102 | 'login_name' => $this->loginName, |
| 103 | 'create_date' => $this->createDate?->format(Format::DATE_TIME), |
| 104 | 'roles' => $this->roles, |
| 105 | 'level_ids' => $this->levelIds, |
| 106 | 'picture' => $this->picture, |
| 107 | ]; |
| 108 | } |
| 109 | |
| 110 | } |
| 111 |