friendship ended with social-app. php is my new best friend
1<?php
2/**
3 * Class AuthenticatedUser
4 *
5 * @created 23.03.2024
6 * @author smiley <smiley@chillerlan.net>
7 * @copyright 2024 smiley
8 * @license MIT
9 *
10 * @filesource
11 */
12declare(strict_types=1);
13
14namespace chillerlan\OAuth\Core;
15
16use chillerlan\Settings\SettingsContainerAbstract;
17use function intval, is_int, is_numeric, trim;
18
19/**
20 * A simple read-only container for user data responses
21 *
22 * @see \chillerlan\OAuth\Core\UserInfo::me()
23 *
24 * @property string|null $handle
25 * @property string|null $displayName
26 * @property string|null $email
27 * @property string|int|null $id
28 * @property string|null $avatar
29 * @property string|null $url
30 * @property array<string, mixed> $data
31 */
32final class AuthenticatedUser extends SettingsContainerAbstract{
33
34 /**
35 * (magic) The user handle, account or tag name
36 */
37 protected string|null $handle = null;
38
39 /**
40 * (magic) The user's display name
41 */
42 protected string|null $displayName = null;
43
44 /**
45 * (magic) The (main) email address
46 */
47 protected string|null $email = null;
48
49 /**
50 * (magic) A user ID, may be string or integer
51 */
52 protected string|int|null $id = null;
53
54 /**
55 * (magic) An avatar URL
56 */
57 protected string|null $avatar = null;
58
59 /**
60 * (magic) URL to the user profile
61 */
62 protected string|null $url = null;
63
64 /**
65 * (magic) The full user endpoint response
66 *
67 * @var array<string, mixed>
68 */
69 protected array $data = [];
70
71 /**
72 * @noinspection PhpMissingParentConstructorInspection
73 */
74 public function __construct(iterable|null $properties = null){
75
76 if(!empty($properties)){
77 // call the parent's setter here
78 foreach($properties as $property => $value){
79 parent::__set($property, $value);
80 }
81
82 }
83
84 }
85
86 /*
87 * make this class readonly
88 */
89
90 /** @codeCoverageIgnore */
91 public function __set(string $property, mixed $value):void{
92 // noop
93 }
94
95 /** @codeCoverageIgnore */
96 public function fromIterable(iterable $properties):static{ // phpcs:ignore
97 // noop
98 return $this;
99 }
100
101 /** @codeCoverageIgnore */
102 public function fromJSON(string $json):static{
103 // noop
104 return $this;
105 }
106
107 /*
108 * setters
109 */
110
111 /**
112 * set the user id, convert to int if possible
113 */
114 protected function set_id(string|int|null $id):void{
115
116 if($id === null){
117 return;
118 }
119
120 $this->id = $id;
121
122 if(!is_int($id) && is_numeric($id)){
123 $intID = intval($id);
124
125 if((string)$intID === $id){
126 $this->id = $intID;
127 }
128 }
129
130 }
131
132 /**
133 * trim and set the display name
134 */
135 protected function set_displayName(string|null $displayName):void{
136
137 if($displayName === null){
138 return;
139 }
140
141 $displayName = trim($displayName);
142
143 if($displayName !== ''){
144 $this->displayName = $displayName;
145 }
146
147 }
148
149}