User
in package
A user represented as a rich domain object.
Users own all learning data (languages, texts, words, etc.) and can authenticate via built-in auth or WordPress integration.
This class enforces domain invariants and encapsulates business logic.
Tags
Table of Contents
Constants
- ROLE_ADMIN = 'admin'
- ROLE_USER = 'user'
Properties
- $apiToken : string|null
- $apiTokenExpires : DateTimeImmutable|null
- $created : DateTimeImmutable
- $email : string
- $googleId : string|null
- $id : UserId
- $isActive : bool
- $lastLogin : DateTimeImmutable|null
- $microsoftId : string|null
- $passwordHash : string|null
- $passwordResetToken : string|null
- $passwordResetTokenExpires : DateTimeImmutable|null
- $rememberToken : string|null
- $rememberTokenExpires : DateTimeImmutable|null
- $role : string
- $username : string
- $wordPressId : int|null
Methods
- activate() : void
- Activate the user account.
- apiToken() : string|null
- apiTokenExpires() : DateTimeImmutable|null
- canLogin() : bool
- Check if the user can log in.
- changeEmail() : void
- Update the email address.
- changePassword() : void
- Update the password hash.
- changeUsername() : void
- Update the username.
- create() : self
- Create a new user with username, email, and password.
- created() : DateTimeImmutable
- createFromGoogle() : self
- Create a user from Google OAuth.
- createFromMicrosoft() : self
- Create a user from Microsoft OAuth.
- createFromWordPress() : self
- Create a user from WordPress integration.
- deactivate() : void
- Deactivate the user account.
- demoteFromAdmin() : void
- Demote the user from admin.
- email() : string
- googleId() : string|null
- hasPassword() : bool
- Check if the user has a password set.
- hasValidApiToken() : bool
- Check if the API token is valid (not expired).
- hasValidPasswordResetToken() : bool
- Check if the password reset token is valid (not expired).
- hasValidRememberToken() : bool
- Check if the remember-me token is valid (not expired).
- id() : UserId
- invalidateApiToken() : void
- Invalidate the API token.
- invalidatePasswordResetToken() : void
- Invalidate the password reset token.
- invalidateRememberToken() : void
- Invalidate the remember-me token.
- isActive() : bool
- isAdmin() : bool
- Check if the user is an admin.
- isLinkedToGoogle() : bool
- Check if the user is linked to Google.
- isLinkedToMicrosoft() : bool
- Check if the user is linked to Microsoft.
- isLinkedToWordPress() : bool
- Check if the user is linked to WordPress.
- lastLogin() : DateTimeImmutable|null
- linkGoogle() : void
- Link to a Google account.
- linkMicrosoft() : void
- Link to a Microsoft account.
- linkWordPress() : void
- Link to a WordPress account.
- microsoftId() : string|null
- passwordHash() : string|null
- passwordResetToken() : string|null
- passwordResetTokenExpires() : DateTimeImmutable|null
- promoteToAdmin() : void
- Promote the user to admin.
- recordLogin() : void
- Record a login.
- rememberToken() : string|null
- rememberTokenExpires() : DateTimeImmutable|null
- role() : string
- setApiToken() : void
- Set a new API token.
- setPasswordResetToken() : void
- Set a new password reset token.
- setRememberToken() : void
- Set a new remember-me token.
- unlinkGoogle() : void
- Unlink from Google account.
- unlinkMicrosoft() : void
- Unlink from Microsoft account.
- unlinkWordPress() : void
- Unlink from WordPress account.
- username() : string
- wordPressId() : int|null
- __construct() : mixed
- Private constructor - use factory methods instead.
- validateEmail() : void
- Validate an email address.
- validateUsername() : void
- Validate a username.
Constants
ROLE_ADMIN
public
mixed
ROLE_ADMIN
= 'admin'
ROLE_USER
public
mixed
ROLE_USER
= 'user'
Properties
$apiToken
private
string|null
$apiToken
$apiTokenExpires
private
DateTimeImmutable|null
$apiTokenExpires
$created
private
DateTimeImmutable
$created
private
string
$email
$googleId
private
string|null
$googleId
$id
private
UserId
$id
$isActive
private
bool
$isActive
$lastLogin
private
DateTimeImmutable|null
$lastLogin
$microsoftId
private
string|null
$microsoftId
$passwordHash
private
string|null
$passwordHash
$passwordResetToken
private
string|null
$passwordResetToken
$passwordResetTokenExpires
private
DateTimeImmutable|null
$passwordResetTokenExpires
$rememberToken
private
string|null
$rememberToken
$rememberTokenExpires
private
DateTimeImmutable|null
$rememberTokenExpires
$role
private
string
$role
$username
private
string
$username
$wordPressId
private
int|null
$wordPressId
Methods
activate()
Activate the user account.
public
activate() : void
apiToken()
public
apiToken() : string|null
Return values
string|nullapiTokenExpires()
public
apiTokenExpires() : DateTimeImmutable|null
Return values
DateTimeImmutable|nullcanLogin()
Check if the user can log in.
public
canLogin() : bool
Return values
boolchangeEmail()
Update the email address.
public
changeEmail(string $email) : void
Parameters
- $email : string
-
The new email address
Tags
changePassword()
Update the password hash.
public
changePassword(string $passwordHash) : void
Parameters
- $passwordHash : string
-
The new password hash
changeUsername()
Update the username.
public
changeUsername(string $username) : void
Parameters
- $username : string
-
The new username
Tags
create()
Create a new user with username, email, and password.
public
static create(string $username, string $email, string $passwordHash) : self
Parameters
- $username : string
-
The username
- $email : string
-
The email address
- $passwordHash : string
-
The hashed password
Tags
Return values
selfcreated()
public
created() : DateTimeImmutable
Return values
DateTimeImmutablecreateFromGoogle()
Create a user from Google OAuth.
public
static createFromGoogle(string $googleId, string $username, string $email) : self
Parameters
- $googleId : string
-
The Google user ID
- $username : string
-
The username
- $email : string
-
The email address
Tags
Return values
selfcreateFromMicrosoft()
Create a user from Microsoft OAuth.
public
static createFromMicrosoft(string $microsoftId, string $username, string $email) : self
Parameters
- $microsoftId : string
-
The Microsoft user ID
- $username : string
-
The username
- $email : string
-
The email address
Tags
Return values
selfcreateFromWordPress()
Create a user from WordPress integration.
public
static createFromWordPress(int $wordPressId, string $username, string $email) : self
Parameters
- $wordPressId : int
-
The WordPress user ID
- $username : string
-
The username
- $email : string
-
The email address
Tags
Return values
selfdeactivate()
Deactivate the user account.
public
deactivate() : void
demoteFromAdmin()
Demote the user from admin.
public
demoteFromAdmin() : void
email()
public
email() : string
Return values
stringgoogleId()
public
googleId() : string|null
Return values
string|nullhasPassword()
Check if the user has a password set.
public
hasPassword() : bool
Return values
boolhasValidApiToken()
Check if the API token is valid (not expired).
public
hasValidApiToken() : bool
Return values
boolhasValidPasswordResetToken()
Check if the password reset token is valid (not expired).
public
hasValidPasswordResetToken() : bool
Return values
boolhasValidRememberToken()
Check if the remember-me token is valid (not expired).
public
hasValidRememberToken() : bool
Return values
boolid()
public
id() : UserId
Return values
UserIdinvalidateApiToken()
Invalidate the API token.
public
invalidateApiToken() : void
invalidatePasswordResetToken()
Invalidate the password reset token.
public
invalidatePasswordResetToken() : void
invalidateRememberToken()
Invalidate the remember-me token.
public
invalidateRememberToken() : void
isActive()
public
isActive() : bool
Return values
boolisAdmin()
Check if the user is an admin.
public
isAdmin() : bool
Return values
boolisLinkedToGoogle()
Check if the user is linked to Google.
public
isLinkedToGoogle() : bool
Return values
boolisLinkedToMicrosoft()
Check if the user is linked to Microsoft.
public
isLinkedToMicrosoft() : bool
Return values
boolisLinkedToWordPress()
Check if the user is linked to WordPress.
public
isLinkedToWordPress() : bool
Return values
boollastLogin()
public
lastLogin() : DateTimeImmutable|null
Return values
DateTimeImmutable|nulllinkGoogle()
Link to a Google account.
public
linkGoogle(string $googleId) : void
Parameters
- $googleId : string
-
The Google user ID
Tags
linkMicrosoft()
Link to a Microsoft account.
public
linkMicrosoft(string $microsoftId) : void
Parameters
- $microsoftId : string
-
The Microsoft user ID
Tags
linkWordPress()
Link to a WordPress account.
public
linkWordPress(int $wordPressId) : void
Parameters
- $wordPressId : int
-
The WordPress user ID
Tags
microsoftId()
public
microsoftId() : string|null
Return values
string|nullpasswordHash()
public
passwordHash() : string|null
Return values
string|nullpasswordResetToken()
public
passwordResetToken() : string|null
Return values
string|nullpasswordResetTokenExpires()
public
passwordResetTokenExpires() : DateTimeImmutable|null
Return values
DateTimeImmutable|nullpromoteToAdmin()
Promote the user to admin.
public
promoteToAdmin() : void
recordLogin()
Record a login.
public
recordLogin() : void
rememberToken()
public
rememberToken() : string|null
Return values
string|nullrememberTokenExpires()
public
rememberTokenExpires() : DateTimeImmutable|null
Return values
DateTimeImmutable|nullrole()
public
role() : string
Return values
stringsetApiToken()
Set a new API token.
public
setApiToken(string $token, DateTimeImmutable $expires) : void
Parameters
- $token : string
-
The API token
- $expires : DateTimeImmutable
-
When the token expires
setPasswordResetToken()
Set a new password reset token.
public
setPasswordResetToken(string $token, DateTimeImmutable $expires) : void
Parameters
- $token : string
-
The password reset token (hashed)
- $expires : DateTimeImmutable
-
When the token expires
setRememberToken()
Set a new remember-me token.
public
setRememberToken(string $token, DateTimeImmutable $expires) : void
Parameters
- $token : string
-
The remember token
- $expires : DateTimeImmutable
-
When the token expires
unlinkGoogle()
Unlink from Google account.
public
unlinkGoogle() : void
unlinkMicrosoft()
Unlink from Microsoft account.
public
unlinkMicrosoft() : void
unlinkWordPress()
Unlink from WordPress account.
public
unlinkWordPress() : void
username()
public
username() : string
Return values
stringwordPressId()
public
wordPressId() : int|null
Return values
int|null__construct()
Private constructor - use factory methods instead.
private
__construct(UserId $id, string $username, string $email, string|null $passwordHash, string|null $apiToken, DateTimeImmutable|null $apiTokenExpires, string|null $rememberToken, DateTimeImmutable|null $rememberTokenExpires, string|null $passwordResetToken, DateTimeImmutable|null $passwordResetTokenExpires, int|null $wordPressId, string|null $googleId, string|null $microsoftId, DateTimeImmutable $created, DateTimeImmutable|null $lastLogin, bool $isActive, string $role) : mixed
Parameters
- $id : UserId
- $username : string
- $email : string
- $passwordHash : string|null
- $apiToken : string|null
- $apiTokenExpires : DateTimeImmutable|null
- $rememberToken : string|null
- $rememberTokenExpires : DateTimeImmutable|null
- $passwordResetToken : string|null
- $passwordResetTokenExpires : DateTimeImmutable|null
- $wordPressId : int|null
- $googleId : string|null
- $microsoftId : string|null
- $created : DateTimeImmutable
- $lastLogin : DateTimeImmutable|null
- $isActive : bool
- $role : string
validateEmail()
Validate an email address.
private
static validateEmail(string $email) : void
Parameters
- $email : string
-
The email to validate
Tags
validateUsername()
Validate a username.
private
static validateUsername(string $username) : void
Parameters
- $username : string
-
The username to validate