1: <?php declare(strict_types=1);
2:
3: namespace Salient\Http;
4:
5: use Salient\Utility\Date;
6: use DateTimeImmutable;
7: use DateTimeInterface;
8: use InvalidArgumentException;
9:
10: /**
11: * @api
12: */
13: class GenericToken extends GenericCredential
14: {
15: private ?DateTimeImmutable $Expires;
16:
17: /**
18: * @api
19: *
20: * @param DateTimeInterface|int|null $expires `null` if the token's lifetime
21: * is unknown or unlimited, otherwise a {@see DateTimeInterface} or Unix
22: * timestamp representing its expiration time.
23: */
24: public function __construct(
25: string $token,
26: string $authenticationScheme,
27: $expires = null
28: ) {
29: if (is_int($expires) && $expires < 0) {
30: throw new InvalidArgumentException(
31: sprintf('Invalid timestamp: %d', $expires),
32: );
33: }
34:
35: $this->Expires = $expires instanceof DateTimeInterface
36: ? Date::immutable($expires)
37: : ($expires !== null
38: ? new DateTimeImmutable('@' . $expires)
39: : null);
40:
41: parent::__construct($token, $authenticationScheme);
42: }
43:
44: /**
45: * Get the expiration time of the token, or null if its lifetime is unknown
46: * or unlimited
47: */
48: public function getExpires(): ?DateTimeImmutable
49: {
50: return $this->Expires;
51: }
52: }
53: