1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Http\OAuth2; |
4: | |
5: | use Salient\Contract\Core\Entity\Readable; |
6: | use Salient\Contract\Core\Immutable; |
7: | use Salient\Contract\Http\AccessTokenInterface; |
8: | use Salient\Core\Concern\ReadableProtectedPropertiesTrait; |
9: | use Salient\Utility\Date; |
10: | use DateTimeImmutable; |
11: | use DateTimeInterface; |
12: | use InvalidArgumentException; |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | |
19: | |
20: | |
21: | |
22: | |
23: | final class AccessToken implements AccessTokenInterface, Immutable, Readable |
24: | { |
25: | use ReadableProtectedPropertiesTrait; |
26: | |
27: | protected string $Token; |
28: | protected string $Type; |
29: | protected ?DateTimeImmutable $Expires; |
30: | |
31: | protected array $Scopes; |
32: | |
33: | protected array $Claims; |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | public function __construct( |
43: | string $token, |
44: | string $type, |
45: | $expires, |
46: | ?array $scopes = null, |
47: | ?array $claims = null |
48: | ) { |
49: | if (is_int($expires) && $expires < 0) { |
50: | throw new InvalidArgumentException(sprintf( |
51: | 'Invalid $expires: %d', |
52: | $expires |
53: | )); |
54: | } |
55: | |
56: | $this->Token = $token; |
57: | $this->Type = $type; |
58: | $this->Expires = $expires instanceof DateTimeInterface |
59: | ? Date::immutable($expires) |
60: | : ($expires === null |
61: | ? null |
62: | : new DateTimeImmutable("@$expires")); |
63: | $this->Scopes = $scopes ?: []; |
64: | $this->Claims = $claims ?: []; |
65: | } |
66: | |
67: | |
68: | |
69: | |
70: | public function getToken(): string |
71: | { |
72: | return $this->Token; |
73: | } |
74: | |
75: | |
76: | |
77: | |
78: | public function getTokenType(): string |
79: | { |
80: | return $this->Type; |
81: | } |
82: | } |
83: | |