1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This software may be modified and distributed under the terms |
5
|
|
|
* of the MIT license. See the LICENSE file for details. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Happyr\Auth0Bundle\Model; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @author Tobias Nyholm <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
final class UserInfo implements \ArrayAccess |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array the raw data from the API |
17
|
|
|
*/ |
18
|
|
|
private $data; |
19
|
|
|
|
20
|
|
|
private function __construct(array $data) |
21
|
|
|
{ |
22
|
|
|
$this->data = $data; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public static function create(array $data): UserInfo |
26
|
|
|
{ |
27
|
|
|
return new self($data); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public function __toString() |
34
|
|
|
{ |
35
|
|
|
if (!empty($this->data['email'])) { |
36
|
|
|
return (string) $this->data['email']; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (!empty($this->data['name'])) { |
40
|
|
|
return (string) $this->data['name']; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (!empty($this->data['nickname'])) { |
44
|
|
|
return (string) $this->data['nickname']; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return ''; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function offsetExists($offset): bool |
51
|
|
|
{ |
52
|
|
|
return array_key_exists($offset, $this->data); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function offsetGet($offset) |
56
|
|
|
{ |
57
|
|
|
return $this->data[$offset]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function offsetSet($offset, $value): void |
61
|
|
|
{ |
62
|
|
|
throw new \LogicException('The UserInfo object is read only'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function offsetUnset($offset): void |
66
|
|
|
{ |
67
|
|
|
throw new \LogicException('The UserInfo object is read only'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function isEmailVerified(): bool |
71
|
|
|
{ |
72
|
|
|
return $this->data['email_verified'] ?? false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getEmail(): ?string |
76
|
|
|
{ |
77
|
|
|
return $this->data['email'] ?? null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getUsername(): ?string |
81
|
|
|
{ |
82
|
|
|
return $this->data['username'] ?? null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function getClientId(): ?string |
86
|
|
|
{ |
87
|
|
|
return $this->data['clientID'] ?? null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getUpdatedAt(): \DateTimeImmutable |
91
|
|
|
{ |
92
|
|
|
return new \DateTimeImmutable($this->data['updated_at'] ?? 'now'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getName(): string |
96
|
|
|
{ |
97
|
|
|
return $this->data['name'] ?? $this->data['nickname'] ?? ''; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getPicture(): ?string |
101
|
|
|
{ |
102
|
|
|
return $this->data['picture'] ?? null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getNickname(): string |
106
|
|
|
{ |
107
|
|
|
return $this->data['nickname'] ?? ''; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function getIdentities(): array |
111
|
|
|
{ |
112
|
|
|
return $this->data['identities'] ?? []; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getCreatedAt(): \DateTimeInterface |
116
|
|
|
{ |
117
|
|
|
return new \DateTimeImmutable($this->data['created_at'] ?? 'now'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function getUserId(): ?string |
124
|
|
|
{ |
125
|
|
|
return $this->data['sub'] ?? $this->data['user_id'] ?? null; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getRoles(): array |
129
|
|
|
{ |
130
|
|
|
if (!isset($this->data['roles'])) { |
131
|
|
|
return []; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->data['roles']; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getAppMetadata(string $name, $default = null) |
138
|
|
|
{ |
139
|
|
|
if (!isset($this->data['app_metadata'])) { |
140
|
|
|
return $default; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (!array_key_exists($name, $this->data['app_metadata'])) { |
144
|
|
|
return $default; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $this->data['app_metadata'][$name]; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function getUserMetadata(string $name, $default = null) |
151
|
|
|
{ |
152
|
|
|
if (!isset($this->data['user_metadata'])) { |
153
|
|
|
return $default; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if (!array_key_exists($name, $this->data['user_metadata'])) { |
157
|
|
|
return $default; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $this->data['user_metadata'][$name]; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|