User
in package
Class User
Represents a user in the system. This class handles user data encapsulation and provides methods for CRUD operations, authentication checks, and session validation.
Table of Contents
Properties
- $bdate : string|null
- $email : string|null
- $firstname : string|null
- $id : int|null
- $is_admin : bool|int
- $lastname : string|null
- $password : string|null
- $username : string|null
Methods
- check_combination() : bool
- Verifies if a specific ID and Username combination exists in the database.
- check_username() : bool
- Checks if a username is already taken.
- deleteById() : bool
- Deletes a user record by ID.
- fill() : void
- Populates the object properties from an associative array (e.g., $_POST data).
- getAllOrdered() : array<string|int, User>
- Retrieves all users from the database.
- getUserById() : User|null
- Retrieves a user by their unique ID.
- getUserByUsername() : User|null
- Retrieves a user by their username.
- insert() : bool
- Inserts the current User object into the database as a new record.
- update() : bool
- Updates the existing user record in the database.
- updatePassword() : bool
- Updates only the user's password in the database.
- validation() : bool
- Validates user data.
- hydrate() : User
- Internal factory method to hydrate a User object from a database row.
Properties
$bdate
public
string|null
$bdate
The birth date of the user (format: Y-m-d).
public
string|null
$email
The user's email address.
$firstname
public
string|null
$firstname
The user's first name.
$id
public
int|null
$id
The unique identifier of the user (Primary Key).
$is_admin
public
bool|int
$is_admin
Indicates if the user has administrative privileges (1/true or 0/false).
$lastname
public
string|null
$lastname
The user's last name.
$password
public
string|null
$password
The hashed password string.
$username
public
string|null
$username
The unique username used for login.
Methods
check_combination()
Verifies if a specific ID and Username combination exists in the database.
public
static check_combination(int|string $id, string $username) : bool
Useful for validating session or cookie data integrity.
Parameters
- $id : int|string
-
The user ID.
- $username : string
-
The username.
Tags
Return values
bool —True if the combination matches a record, false otherwise.
check_username()
Checks if a username is already taken.
public
static check_username(string $username) : bool
Parameters
- $username : string
-
The username to check.
Tags
Return values
bool —True if the username exists, false if it is available.
deleteById()
Deletes a user record by ID.
public
static deleteById(int|string $id) : bool
Parameters
- $id : int|string
-
The ID of the user to delete.
Tags
Return values
bool —True on success, false on failure.
fill()
Populates the object properties from an associative array (e.g., $_POST data).
public
fill(array<string|int, mixed> $data) : void
Parameters
- $data : array<string|int, mixed>
-
The source data array.
getAllOrdered()
Retrieves all users from the database.
public
static getAllOrdered() : array<string|int, User>
Results are ordered by admin status (admins first) and then by username.
Tags
Return values
array<string|int, User> —An array of User objects.
getUserById()
Retrieves a user by their unique ID.
public
static getUserById(int|string $id) : User|null
Parameters
- $id : int|string
-
The user ID.
Tags
Return values
User|null —The User object if found, or null.
getUserByUsername()
Retrieves a user by their username.
public
static getUserByUsername(string $username) : User|null
Parameters
- $username : string
-
The username.
Tags
Return values
User|null —The User object if found, or null.
insert()
Inserts the current User object into the database as a new record.
public
insert() : bool
If successful, the object's ID property is updated with the new auto-increment ID.
Tags
Return values
bool —True on success, false on failure.
update()
Updates the existing user record in the database.
public
update() : bool
Requires the ID property to be set.
Tags
Return values
bool —True on success, false on failure.
updatePassword()
Updates only the user's password in the database.
public
updatePassword() : bool
Tags
Return values
bool —True on success, false on failure (or if properties are empty).
validation()
Validates user data.
public
static validation(User $user, array<string|int, mixed> $errors) : bool
Parameters
- $user : User
-
The user object to validate.
- $errors : array<string|int, mixed>
-
Reference to an array where errors will be stored.
Return values
bool —True if valid, false otherwise.
hydrate()
Internal factory method to hydrate a User object from a database row.
private
static hydrate(array<string|int, mixed> $row) : User
Parameters
- $row : array<string|int, mixed>
-
Associative array representing a database row.
Return values
User —A populated User instance.