BasePresenter.php 2.39 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\User;
Jacob Priddy's avatar
Jacob Priddy committed
8
use Source\Entities\Group;
Jacob Priddy's avatar
Jacob Priddy committed
9

Jacob Priddy's avatar
Jacob Priddy committed
10
11
abstract class BasePresenter
{
Jacob Priddy's avatar
Jacob Priddy committed
12
13
14
15
16
17
    /**
     * @param bool|null $bool
     * @param string    $true
     * @param string    $false
     * @return string
     */
Jacob Priddy's avatar
Jacob Priddy committed
18
19
    public function formatBool(?bool $bool, string $true = 'Yes', string $false = 'No'): ?string
    {
Jacob Priddy's avatar
Jacob Priddy committed
20
21
22
23
24
25
26
27
28
29
30
31
        if ($bool === null) {
            return null;
        }

        return $bool ? $true : $false;
    }

    /**
     * @param Carbon|null $time
     * @param string      $format
     * @return string|null
     */
Jacob Priddy's avatar
Jacob Priddy committed
32
33
    public function formatTime(?Carbon $time, string $format = 'g:i A'): ?string
    {
Jacob Priddy's avatar
Jacob Priddy committed
34
35
36
37
38
39
        if ($time === null) {
            return null;
        }

        return $time->format($format);
    }
Jacob Priddy's avatar
Jacob Priddy committed
40

Jacob Priddy's avatar
Jacob Priddy committed
41
42
43
44
45
    /**
     * @param Carbon|null $datetime
     * @param string      $format
     * @return string|null
     */
Jacob Priddy's avatar
Jacob Priddy committed
46
47
    public function formatDateTime(?Carbon $datetime, string $format = 'c'): ?string
    {
Jacob Priddy's avatar
Jacob Priddy committed
48
49
50
51
52
53
54
        if ($datetime === null) {
            return null;
        }

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

Jacob Priddy's avatar
Jacob Priddy committed
55
56
57
58
    /**
     * @param User $user
     * @return array
     */
Jacob Priddy's avatar
Jacob Priddy committed
59
60
    public function formatUser(User $user): array
    {
Jacob Priddy's avatar
Jacob Priddy committed
61
62
63
64
65
66
67
68
69
70
71
72
        return [
            'id' => $user->getId(),
            'first_name' => $user->getFirstName(),
            'last_name' => $user->getLastName(),
            'display_name' => $user->getDisplayName(),
            'emplid' => $user->getEmplid(),
            'email' => $user->getEmail(),
            'expires_at' => $this->formatDate($user->getExpiresAt()),
            'created_at' => $this->formatDate($user->getCreatedAt()),
            'updated_at' => $this->formatDate($user->getUpdatedAt()),
        ];
    }
Jacob Priddy's avatar
Jacob Priddy committed
73

Jacob Priddy's avatar
Jacob Priddy committed
74
75
76
77
78
79
80
81
82
83
84
    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
85
86
87
88
89
90
91
92
93
94
95
96
97
    /**
     * @param Carbon|null $date
     * @param string      $format
     * @return string|null
     */
    public function formatDate(?Carbon $date, string $format = 'M j, Y'): ?string
    {
        if ($date === null) {
            return null;
        }

        return $date->format($format);
    }
Jacob Priddy's avatar
Jacob Priddy committed
98
}