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


namespace Source\UseCases;


use Carbon\Carbon;
Jacob Priddy's avatar
Jacob Priddy committed
8
use Source\Entities\User;
Jacob Priddy's avatar
Jacob Priddy committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

abstract class BasePresenter {
    /**
     * @param bool|null $bool
     * @param string    $true
     * @param string    $false
     * @return string
     */
    public function formatBool(?bool $bool, string $true = 'Yes', string $false = 'No'): ?string {
        if ($bool === null) {
            return null;
        }

        return $bool ? $true : $false;
    }

    /**
     * @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);
    }

    /**
     * @param Carbon|null $time
     * @param string      $format
     * @return string|null
     */
    public function formatTime(?Carbon $time, string $format = 'g:i A'): ?string {
        if ($time === null) {
            return null;
        }

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

Jacob Priddy's avatar
Jacob Priddy committed
51
52
53
54
55
56
57
58
59
60
61
62
63
    /**
     * @param Carbon|null $datetime
     * @param string      $format
     * @return string|null
     */
    public function formatDateTime(?Carbon $datetime, string $format = 'c'): ?string {
        if ($datetime === null) {
            return null;
        }

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

Jacob Priddy's avatar
Jacob Priddy committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    /**
     * @param User $user
     * @return array
     */
    public function formatUser(User $user): array {
        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
81
}