Add TuxiNet branding and document templates
This commit is contained in:
38
app/Models/Model.php
Normal file
38
app/Models/Model.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Core\Database;
|
||||
use PDO;
|
||||
|
||||
abstract class Model
|
||||
{
|
||||
protected static string $table;
|
||||
protected static string $primaryKey = 'id';
|
||||
|
||||
/**
|
||||
* @return array<int, static>
|
||||
*/
|
||||
public static function all(): array
|
||||
{
|
||||
$stmt = Database::connection()->query('SELECT * FROM ' . static::$table);
|
||||
$rows = $stmt->fetchAll();
|
||||
return array_map(fn($row) => static::fromArray($row), $rows);
|
||||
}
|
||||
|
||||
public static function find(int $id): ?static
|
||||
{
|
||||
$stmt = Database::connection()->prepare('SELECT * FROM ' . static::$table . ' WHERE ' . static::$primaryKey . ' = :id LIMIT 1');
|
||||
$stmt->execute(['id' => $id]);
|
||||
$row = $stmt->fetch();
|
||||
if (!$row) {
|
||||
return null;
|
||||
}
|
||||
return static::fromArray($row);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $attributes
|
||||
*/
|
||||
abstract protected static function fromArray(array $attributes): static;
|
||||
}
|
||||
54
app/Models/User.php
Normal file
54
app/Models/User.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user