55 lines
1.7 KiB
PHP
55 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
class User extends Model
|
|
{
|
|
protected static string $table = 'users';
|
|
|
|
public int $id;
|
|
public string $role;
|
|
public string $name;
|
|
public string $email;
|
|
public string $pass_hash;
|
|
public ?string $phone;
|
|
public ?string $address;
|
|
public ?int $customer_id;
|
|
public bool $is_active;
|
|
public ?string $twofa_secret;
|
|
public string $created_at;
|
|
public string $updated_at;
|
|
|
|
/**
|
|
* @param array<string, mixed> $attributes
|
|
*/
|
|
protected static function fromArray(array $attributes): static
|
|
{
|
|
$user = new static();
|
|
$user->id = (int) $attributes['id'];
|
|
$user->role = (string) $attributes['role'];
|
|
$user->name = (string) $attributes['name'];
|
|
$user->email = (string) $attributes['email'];
|
|
$user->pass_hash = (string) $attributes['pass_hash'];
|
|
$user->phone = $attributes['phone'] ?? null;
|
|
$user->address = $attributes['address'] ?? null;
|
|
$user->customer_id = isset($attributes['customer_id']) ? (int) $attributes['customer_id'] : null;
|
|
$user->is_active = (bool) $attributes['is_active'];
|
|
$user->twofa_secret = $attributes['twofa_secret'] ?? null;
|
|
$user->created_at = (string) $attributes['created_at'];
|
|
$user->updated_at = (string) $attributes['updated_at'];
|
|
return $user;
|
|
}
|
|
|
|
public static function findByEmail(string $email): ?self
|
|
{
|
|
$stmt = \App\Core\Database::connection()->prepare('SELECT * FROM users WHERE email = :email LIMIT 1');
|
|
$stmt->execute(['email' => $email]);
|
|
$row = $stmt->fetch();
|
|
if (!$row) {
|
|
return null;
|
|
}
|
|
|
|
return self::fromArray($row);
|
|
}
|
|
}
|