1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FMCSSOClient; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
|
7
|
|
|
class SSOUser |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The user's access token. |
11
|
|
|
* |
12
|
|
|
* @var Token |
13
|
|
|
*/ |
14
|
|
|
protected ?Token $token = null; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The unique identifier for the user. |
18
|
|
|
* |
19
|
|
|
* @var int|string |
20
|
|
|
*/ |
21
|
|
|
protected int|string $id; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The user's e-mail address. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected string $email; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The user's title. |
32
|
|
|
* |
33
|
|
|
* @var string|null |
34
|
|
|
*/ |
35
|
|
|
protected ?string $title; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The user's first name. |
39
|
|
|
* |
40
|
|
|
* @var string|null |
41
|
|
|
*/ |
42
|
|
|
protected ?string $first_name; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The user's last name. |
46
|
|
|
* |
47
|
|
|
* @var string|null |
48
|
|
|
*/ |
49
|
|
|
protected ?string $last_name; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The user's raw attributes. |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected array $user; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set the token on the user. |
60
|
|
|
* |
61
|
|
|
* @param Token $token |
62
|
|
|
* |
63
|
|
|
* @return static |
64
|
|
|
*/ |
65
|
2 |
|
public function setToken(Token $token): SSOUser |
66
|
|
|
{ |
67
|
2 |
|
$this->token = $token; |
68
|
|
|
|
69
|
2 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Access token instance. |
74
|
|
|
* |
75
|
|
|
* @return Token|null |
76
|
|
|
*/ |
77
|
1 |
|
public function token(): ?Token |
78
|
|
|
{ |
79
|
1 |
|
return $this->token; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get the unique identifier for the user. |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
1 |
|
public function getId(): string |
88
|
|
|
{ |
89
|
1 |
|
return $this->id; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Get the name title of the user. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
1 |
|
public function getTitle(): string |
98
|
|
|
{ |
99
|
1 |
|
return (string) $this->title; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the first name of the user. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
1 |
|
public function getFirstName(): string |
108
|
|
|
{ |
109
|
1 |
|
return (string) $this->first_name; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the last name of the user. |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
1 |
|
public function getLastName(): string |
118
|
|
|
{ |
119
|
1 |
|
return (string) $this->last_name; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get the name of the user. |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
1 |
|
public function getName(): string |
128
|
|
|
{ |
129
|
1 |
|
return implode(' ', array_filter([ |
130
|
1 |
|
$this->title, |
131
|
1 |
|
$this->first_name, |
132
|
1 |
|
$this->last_name, |
133
|
|
|
])); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the e-mail address of the user. |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
1 |
|
public function getEmail(): string |
142
|
|
|
{ |
143
|
1 |
|
return $this->email; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get the raw user array. |
148
|
|
|
* |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
1 |
|
public function getRaw(): array |
152
|
|
|
{ |
153
|
1 |
|
return $this->user; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Set the raw user array from the provider. |
158
|
|
|
* |
159
|
|
|
* @param array $user |
160
|
|
|
* |
161
|
|
|
* @return $this |
162
|
|
|
*/ |
163
|
2 |
|
public function setRaw(array $user): SSOUser |
164
|
|
|
{ |
165
|
2 |
|
$this->user = $user; |
166
|
|
|
|
167
|
2 |
|
return $this; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Map the given array onto the user's properties. |
172
|
|
|
* |
173
|
|
|
* @param array $attributes |
174
|
|
|
* |
175
|
|
|
* @return $this |
176
|
|
|
*/ |
177
|
2 |
|
public function map(array $attributes): SSOUser |
178
|
|
|
{ |
179
|
2 |
|
foreach ($attributes as $key => $value) { |
180
|
2 |
|
$this->{$key} = $value; |
181
|
|
|
} |
182
|
|
|
|
183
|
2 |
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Determine if the given raw user attribute exists. |
188
|
|
|
* |
189
|
|
|
* @param string $offset |
190
|
|
|
* |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
1 |
|
public function offsetExists(string $offset): bool |
194
|
|
|
{ |
195
|
1 |
|
return Arr::exists($this->user, $offset); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get the given key from the raw user. |
200
|
|
|
* |
201
|
|
|
* @param string $offset |
202
|
|
|
* @param mixed|null $default |
203
|
|
|
* |
204
|
|
|
* @return mixed |
205
|
|
|
*/ |
206
|
1 |
|
public function offsetGet(string $offset, mixed $default = null): mixed |
207
|
|
|
{ |
208
|
1 |
|
return Arr::get($this->user, $offset, $default); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Set the given attribute on the raw user array. |
213
|
|
|
* |
214
|
|
|
* @param string $offset |
215
|
|
|
* @param mixed $value |
216
|
|
|
* |
217
|
|
|
* @return static |
218
|
|
|
*/ |
219
|
1 |
|
public function offsetSet(string $offset, mixed $value): static |
220
|
|
|
{ |
221
|
1 |
|
Arr::set($this->user, $offset, $value); |
222
|
|
|
|
223
|
1 |
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Unset the given value from the raw user array. |
228
|
|
|
* |
229
|
|
|
* @param string $offset |
230
|
|
|
* |
231
|
|
|
* @return static |
232
|
|
|
*/ |
233
|
1 |
|
public function offsetUnset(string $offset): static |
234
|
|
|
{ |
235
|
1 |
|
Arr::forget($this->user, $offset); |
236
|
|
|
|
237
|
1 |
|
return $this; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|