-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuthenticatedUser.php
149 lines (118 loc) · 2.68 KB
/
AuthenticatedUser.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
<?php
/**
* Class AuthenticatedUser
*
* @created 23.03.2024
* @author smiley <smiley@chillerlan.net>
* @copyright 2024 smiley
* @license MIT
*
* @filesource
*/
declare(strict_types=1);
namespace chillerlan\OAuth\Core;
use chillerlan\Settings\SettingsContainerAbstract;
use function intval, is_int, is_numeric, trim;
/**
* A simple read-only container for user data responses
*
* @see \chillerlan\OAuth\Core\UserInfo::me()
*
* @property string|null $handle
* @property string|null $displayName
* @property string|null $email
* @property string|int|null $id
* @property string|null $avatar
* @property string|null $url
* @property array<string, mixed> $data
*/
final class AuthenticatedUser extends SettingsContainerAbstract{
/**
* (magic) The user handle, account or tag name
*/
protected string|null $handle = null;
/**
* (magic) The user's display name
*/
protected string|null $displayName = null;
/**
* (magic) The (main) email address
*/
protected string|null $email = null;
/**
* (magic) A user ID, may be string or integer
*/
protected string|int|null $id = null;
/**
* (magic) An avatar URL
*/
protected string|null $avatar = null;
/**
* (magic) URL to the user profile
*/
protected string|null $url = null;
/**
* (magic) The full user endpoint response
*
* @var array<string, mixed>
*/
protected array $data = [];
/**
* @noinspection PhpMissingParentConstructorInspection
*/
public function __construct(iterable|null $properties = null){
if(!empty($properties)){
// call the parent's setter here
foreach($properties as $property => $value){
parent::__set($property, $value);
}
}
}
/*
* make this class readonly
*/
/** @codeCoverageIgnore */
public function __set(string $property, mixed $value):void{
// noop
}
/** @codeCoverageIgnore */
public function fromIterable(iterable $properties):static{ // phpcs:ignore
// noop
return $this;
}
/** @codeCoverageIgnore */
public function fromJSON(string $json):static{
// noop
return $this;
}
/*
* setters
*/
/**
* set the user id, convert to int if possible
*/
protected function set_id(string|int|null $id):void{
if($id === null){
return;
}
$this->id = $id;
if(!is_int($id) && is_numeric($id)){
$intID = intval($id);
if((string)$intID === $id){
$this->id = $intID;
}
}
}
/**
* trim and set the display name
*/
protected function set_displayName(string|null $displayName):void{
if($displayName === null){
return;
}
$displayName = trim($displayName);
if($displayName !== ''){
$this->displayName = $displayName;
}
}
}