BasePresenter.php 3.25 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
6
<?php


namespace Source\UseCases;

use Carbon\Carbon;
Jacob Priddy's avatar
Jacob Priddy committed
7
use Source\Entities\Door;
Jacob Priddy's avatar
Jacob Priddy committed
8
use Source\Entities\User;
Jacob Priddy's avatar
Jacob Priddy committed
9
use Source\Entities\Group;
Jacob Priddy's avatar
Jacob Priddy committed
10
use Source\Entities\Token;
Jacob Priddy's avatar
Jacob Priddy committed
11

Jacob Priddy's avatar
Jacob Priddy committed
12
13
abstract class BasePresenter
{
Jacob Priddy's avatar
Jacob Priddy committed
14
15
16
17
18
    /**
     * @param Carbon|null $datetime
     * @param string      $format
     * @return string|null
     */
Jacob Priddy's avatar
Jacob Priddy committed
19
20
    public function formatDateTime(?Carbon $datetime, string $format = 'c'): ?string
    {
Jacob Priddy's avatar
Jacob Priddy committed
21
22
23
24
25
26
27
        if ($datetime === null) {
            return null;
        }

        return $datetime->format($format);
    }

Jacob Priddy's avatar
Jacob Priddy committed
28
    /**
29
     * @param \Source\Entities\User $user
Jacob Priddy's avatar
Jacob Priddy committed
30
31
     * @return array
     */
32
    public function formatFullUser(User $user): array
Jacob Priddy's avatar
Jacob Priddy committed
33
    {
Jacob Priddy's avatar
Jacob Priddy committed
34
35
36
37
38
39
40
        return [
            'id' => $user->getId(),
            'first_name' => $user->getFirstName(),
            'last_name' => $user->getLastName(),
            'display_name' => $user->getDisplayName(),
            'emplid' => $user->getEmplid(),
            'email' => $user->getEmail(),
41
42
43
            'expires_at' => $this->formatDateTime($user->getExpiresAt()),
            'created_at' => $this->formatDateTime($user->getCreatedAt()),
            'updated_at' => $this->formatDateTime($user->getUpdatedAt()),
Jacob Priddy's avatar
Jacob Priddy committed
44
45
        ];
    }
Jacob Priddy's avatar
Jacob Priddy committed
46

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
    /**
     * @param \Source\Entities\User $user
     * @return array
     */
    public function formatPartialUser(User $user): array
    {
        $expired = false;
        if ($user->getExpiresAt()) {
            $expired = $user->getExpiresAt() < Carbon::now();
        }

        return [
            'id' => $user->getId(),
            'first_name' => $user->getFirstName(),
            'last_name' => $user->getLastName(),
            'display_name' => $user->getDisplayName(),
            'email' => $user->getEmail(),
            'expired' => $expired,
        ];
    }

    /**
     * @param \Source\Entities\Group $group
     * @return array
     */
Jacob Priddy's avatar
Jacob Priddy committed
72
73
74
75
76
77
78
79
80
81
    public function formatGroup(Group $group): array
    {
        return [
            'id' => $group->getId(),
            'title' => $group->getTitle(),
            'description' => $group->getDescription(),
            'created_at' => $this->formatDateTime($group->getCreatedAt()),
            'updated_at' => $this->formatDateTime($group->getUpdatedAt()),
        ];
    }
Jacob Priddy's avatar
Jacob Priddy committed
82
83

    /**
84
     * @param \Source\Entities\Door|null $door
Jacob Priddy's avatar
Jacob Priddy committed
85
86
     * @return array
     */
87
    public function formatDoor(?Door $door): array
Jacob Priddy's avatar
Jacob Priddy committed
88
    {
89
90
91
92
        if (!$door) {
            return null;
        }

Jacob Priddy's avatar
Jacob Priddy committed
93
94
95
96
97
98
99
100
        return [
            'id' => $door->getId(),
            'name' => $door->getName(),
            'location' => $door->getLocation(),
            'created_at' => $this->formatDateTime($door->getCreatedAt()),
            'updated_at' => $this->formatDateTime($door->getUpdatedAt()),
        ];
    }
Jacob Priddy's avatar
Jacob Priddy committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120

    /**
     * @param \Source\Entities\Token|null $token
     * @return array
     */
    public function formatToken(?Token $token): array
    {
        if (!$token) {
            return [];
        }

        return [
            'id' => $token->getId(),
            'name' => $token->getName(),
            'user_id' => $token->getUserId(),
            'expires_at' => $this->formatDateTime($token->getExpiresAt()),
            'created_at' => $this->formatDateTime($token->getCreatedAt()),
            'updated_at' => $this->formatDateTime($token->getUpdatedAt()),
        ];
    }
Jacob Priddy's avatar
Jacob Priddy committed
121
}