-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Subscription.php
164 lines (134 loc) · 3.99 KB
/
Subscription.php
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
/*
* The MIT License (MIT)
*
* Copyright (c) 2020-2021 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace WebPush;
use function array_key_exists;
use Assert\Assertion;
use DateTimeInterface;
use JsonSerializable;
use Safe\DateTimeImmutable;
use function Safe\json_decode;
class Subscription implements JsonSerializable
{
private string $endpoint;
/**
* @var string[]
*/
private array $keys;
/**
* @var string[]
*/
private array $supportedContentEncodings = ['aesgcm'];
private ?int $expirationTime = null;
public function __construct(string $endpoint)
{
$this->endpoint = $endpoint;
$this->keys = [];
}
public static function create(string $endpoint): self
{
return new self($endpoint);
}
/**
* @param string[] $contentEncodings
*/
public function withContentEncodings(array $contentEncodings): self
{
$this->supportedContentEncodings = $contentEncodings;
return $this;
}
public function getKeys(): array
{
return $this->keys;
}
public function hasKey(string $key): bool
{
return isset($this->keys[$key]);
}
public function getKey(string $key): string
{
Assertion::keyExists($this->keys, $key, 'The key does not exist');
return $this->keys[$key];
}
public function setKey(string $key, string $value): self
{
$this->keys[$key] = $value;
return $this;
}
public function getExpirationTime(): ?int
{
return $this->expirationTime;
}
public function setExpirationTime(?int $expirationTime): self
{
$this->expirationTime = $expirationTime;
return $this;
}
public function expiresAt(): ?DateTimeInterface
{
return null === $this->expirationTime ? null : (new DateTimeImmutable())->setTimestamp($this->expirationTime);
}
public function getEndpoint(): string
{
return $this->endpoint;
}
/**
* @return string[]
*/
public function getSupportedContentEncodings(): array
{
return $this->supportedContentEncodings;
}
public static function createFromString(string $input): self
{
$data = json_decode($input, true);
Assertion::isArray($data, 'Invalid input');
return self::createFromAssociativeArray($data);
}
/**
* @return array<string, string|string[]>
*/
public function jsonSerialize(): array
{
return [
'endpoint' => $this->endpoint,
'supportedContentEncodings' => $this->supportedContentEncodings,
'keys' => $this->keys,
];
}
/**
* @param array<string, mixed> $input
*/
private static function createFromAssociativeArray(array $input): self
{
Assertion::keyExists($input, 'endpoint', 'Invalid input');
Assertion::string($input['endpoint'], 'Invalid input');
$object = new self($input['endpoint']);
if (array_key_exists('supportedContentEncodings', $input)) {
$encodings = $input['supportedContentEncodings'];
Assertion::isArray($encodings, 'Invalid input');
Assertion::allString($encodings, 'Invalid input');
$object->withContentEncodings($encodings);
}
if (array_key_exists('expirationTime', $input)) {
Assertion::nullOrInteger($input['expirationTime'], 'Invalid input');
$object->setExpirationTime($input['expirationTime']);
}
if (array_key_exists('keys', $input)) {
Assertion::isArray($input['keys'], 'Invalid input');
foreach ($input['keys'] as $k => $v) {
Assertion::string($k, 'Invalid key name');
Assertion::string($v, 'Invalid key value');
$object->setKey($k, $v);
}
}
return $object;
}
}