2
0
Web/auth/User.php
2025-09-10 21:57:44 -05:00

83 lines
1.8 KiB
PHP

<?php
/**
* User model represents an authenticated user
*/
class User
{
public int|string $id;
public string $username;
public string $email;
public string $password;
public ?string $rememberToken;
public array $attributes = [];
public ?string $createdAt;
public ?string $updatedAt;
public function __construct(array $data = [])
{
foreach ($data as $key => $value) {
if (property_exists($this, $key)) {
$this->$key = $value;
} else {
$this->attributes[$key] = $value;
}
}
}
/**
* Get user identifier (email or username)
*/
public function getIdentifier(): string
{
return $this->email ?: $this->username;
}
/**
* Get user ID
*/
public function getId(): int|string
{
return $this->id;
}
/**
* Get custom attribute
*/
public function getAttribute(string $key): mixed
{
return $this->attributes[$key] ?? null;
}
/**
* Set custom attribute
*/
public function setAttribute(string $key, mixed $value): void
{
$this->attributes[$key] = $value;
}
/**
* Convert to array
*/
public function toArray(): array
{
return [
'id' => $this->id,
'username' => $this->username,
'email' => $this->email,
'created_at' => $this->createdAt,
'updated_at' => $this->updatedAt,
] + $this->attributes;
}
/**
* Convert to safe array (without sensitive data)
*/
public function toSafeArray(): array
{
$data = $this->toArray();
unset($data['password'], $data['remember_token']);
return $data;
}
}