Token.php 2.77 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
6
7
8
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php


namespace Source\Entities;


use Carbon\Carbon;

/**
 * Class Token
 *
 * @package Source\Entities
 */
class Token {
    /**
     * @var int
     */
    protected int $id;

    /**
     * @var int
     */
    protected int $userId;

    /**
     * @var string
     */
    protected string $tokenString;

    /**
     * @var string|null
     */
    protected ?string $name;

    /**
     * @var Carbon|null
     */
    protected ?Carbon $expiresAt;

    /**
     * @var Carbon|null
     */
    protected ?Carbon $createdAt;

    /**
     * @var Carbon|null
     */
    protected ?Carbon $updatedAt;

    /**
     * @param int         $id
     * @param int         $userId
     * @param string|null $name
     * @param string      $tokenString
     * @param Carbon|null $expiresAt
     * @param Carbon|null $createdAt
     * @param Carbon|null $updatedAt
     */
    public function __construct(int $id,
                                int $userId,
                                string $tokenString,
                                ?string $name = null,
                                ?Carbon $expiresAt = null,
                                ?Carbon $createdAt = null,
                                ?Carbon $updatedAt = null) {
        $this->id = $id;
        $this->userId = $userId;
        $this->name = $name;
        $this->tokenString = $tokenString;
        $this->expiresAt = $expiresAt;
        $this->createdAt = $createdAt;
        $this->updatedAt = $updatedAt;
    }

    /**
     * @param string|null $token
     * @return bool
     */
    public function matches(?string $token): bool {
        return $this->tokenString === $token;
    }

    /**
     * @return int
     */
    public function getId(): int {
        return $this->id;
    }

    /**
     * @return int
     */
    public function getUserId(): int {
        return $this->userId;
    }

    /**
     * @return string
     */
    public function getTokenString(): string {
        return $this->tokenString;
    }

    /**
     * @return string|null
     */
    public function getName(): ?string {
        return $this->name;
    }

    /**
     * @return Carbon|null
     */
    public function getExpiresAt(): ?Carbon {
        return $this->expiresAt;
    }

    /**
     * @return Carbon|null
     */
    public function getCreatedAt(): ?Carbon {
        return $this->createdAt;
    }

    /**
     * @return Carbon|null
     */
    public function getUpdatedAt(): ?Carbon {
        return $this->updatedAt;
    }
Jacob Priddy's avatar
Jacob Priddy committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145

    /**
     * @param int $id
     */
    public function setId(int $id): void {
        $this->id = $id;
    }

    /**
     * @param Carbon $date
     * @return bool
     */
    public function isValidAtTime(Carbon $date): bool {
        return $this->expiresAt === null || $this->expiresAt->isAfter($date);
    }
Jacob Priddy's avatar
Jacob Priddy committed
146
}