Documentation

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
since
3.0.0

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

$email

private string $email

$googleId

private string|null $googleId

$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|null

apiTokenExpires()

public apiTokenExpires() : DateTimeImmutable|null
Return values
DateTimeImmutable|null

canLogin()

Check if the user can log in.

public canLogin() : bool
Return values
bool

changeEmail()

Update the email address.

public changeEmail(string $email) : void
Parameters
$email : string

The new email address

Tags
throws
InvalidArgumentException

If email is invalid

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
throws
InvalidArgumentException

If username is invalid

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
throws
InvalidArgumentException

If username or email is invalid

Return values
self

created()

public created() : DateTimeImmutable
Return values
DateTimeImmutable

createFromGoogle()

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
throws
InvalidArgumentException

If Google ID is empty

Return values
self

createFromMicrosoft()

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
throws
InvalidArgumentException

If Microsoft ID is empty

Return values
self

createFromWordPress()

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
throws
InvalidArgumentException

If WordPress ID is invalid

Return values
self

deactivate()

Deactivate the user account.

public deactivate() : void

demoteFromAdmin()

Demote the user from admin.

public demoteFromAdmin() : void

email()

public email() : string
Return values
string

googleId()

public googleId() : string|null
Return values
string|null

hasPassword()

Check if the user has a password set.

public hasPassword() : bool
Return values
bool

hasValidApiToken()

Check if the API token is valid (not expired).

public hasValidApiToken() : bool
Return values
bool

hasValidPasswordResetToken()

Check if the password reset token is valid (not expired).

public hasValidPasswordResetToken() : bool
Return values
bool

hasValidRememberToken()

Check if the remember-me token is valid (not expired).

public hasValidRememberToken() : bool
Return values
bool

invalidateApiToken()

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
bool

isAdmin()

Check if the user is an admin.

public isAdmin() : bool
Return values
bool

isLinkedToGoogle()

Check if the user is linked to Google.

public isLinkedToGoogle() : bool
Return values
bool

isLinkedToMicrosoft()

Check if the user is linked to Microsoft.

public isLinkedToMicrosoft() : bool
Return values
bool

isLinkedToWordPress()

Check if the user is linked to WordPress.

public isLinkedToWordPress() : bool
Return values
bool

lastLogin()

public lastLogin() : DateTimeImmutable|null
Return values
DateTimeImmutable|null

linkGoogle()

Link to a Google account.

public linkGoogle(string $googleId) : void
Parameters
$googleId : string

The Google user ID

Tags
throws
InvalidArgumentException

If Google ID is empty

linkMicrosoft()

Link to a Microsoft account.

public linkMicrosoft(string $microsoftId) : void
Parameters
$microsoftId : string

The Microsoft user ID

Tags
throws
InvalidArgumentException

If Microsoft ID is empty

linkWordPress()

Link to a WordPress account.

public linkWordPress(int $wordPressId) : void
Parameters
$wordPressId : int

The WordPress user ID

Tags
throws
InvalidArgumentException

If WordPress ID is invalid

microsoftId()

public microsoftId() : string|null
Return values
string|null

passwordHash()

public passwordHash() : string|null
Return values
string|null

passwordResetToken()

public passwordResetToken() : string|null
Return values
string|null

passwordResetTokenExpires()

public passwordResetTokenExpires() : DateTimeImmutable|null
Return values
DateTimeImmutable|null

promoteToAdmin()

Promote the user to admin.

public promoteToAdmin() : void

recordLogin()

Record a login.

public recordLogin() : void

rememberToken()

public rememberToken() : string|null
Return values
string|null

rememberTokenExpires()

public rememberTokenExpires() : DateTimeImmutable|null
Return values
DateTimeImmutable|null

role()

public role() : string
Return values
string

setApiToken()

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
string

wordPressId()

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
throws
InvalidArgumentException

If email is invalid

validateUsername()

Validate a username.

private static validateUsername(string $username) : void
Parameters
$username : string

The username to validate

Tags
throws
InvalidArgumentException

If username is invalid


        
On this page

Search results