Files
Lars Behrends ca2d3a6960 ...
2025-10-18 22:03:30 +02:00

136 lines
3.8 KiB
PHP

<?php
namespace App\Models;
use PDOException;
abstract class Model
{
protected \PDO $pdo;
protected string $table;
protected array $fillable = [];
protected array $hidden = [];
protected array $casts = [];
protected array $attributes = [];
public function __construct(\PDO $pdo, array $data = [])
{
$this->pdo = $pdo;
$this->attributes = $data;
}
public function __get(string $key)
{
return $this->attributes[$key] ?? null;
}
public function __set(string $key, $value)
{
$this->attributes[$key] = $value;
}
public function __isset(string $key): bool
{
return isset($this->attributes[$key]);
}
public function getAttributes(): array
{
return $this->attributes;
}
public function setAttributes(array $data)
{
$this->attributes = $data;
}
public function find(int $id): ?array
{
$stmt = $this->pdo->prepare("SELECT * FROM {$this->table} WHERE id = :id");
$stmt->execute(['id' => $id]);
return $stmt->fetch(\PDO::FETCH_ASSOC) ?: null;
}
public function findAll(array $conditions = []): array
{
$whereClause = $this->buildWhereClause($conditions);
$sql = "SELECT * FROM {$this->table} {$whereClause['sql']}";
$stmt = $this->pdo->prepare($sql);
$stmt->execute($whereClause['params']);
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
public function create(array $data): ?int
{
$filteredData = array_intersect_key($data, array_flip($this->fillable));
$columns = array_keys($filteredData);
$placeholders = array_map(fn($col) => ":$col", $columns);
$sql = "INSERT INTO {$this->table} (" . implode(', ', $columns) . ") VALUES (" . implode(', ', $placeholders) . ")";
$stmt = $this->pdo->prepare($sql);
$stmt->execute($filteredData);
return (int) $this->pdo->lastInsertId();
}
public function update(int $id, array $data): bool
{
$filteredData = array_intersect_key($data, array_flip($this->fillable));
if (empty($filteredData)) {
return false;
}
$setClause = array_map(fn($col) => "$col = :$col", array_keys($filteredData));
$sql = "UPDATE {$this->table} SET " . implode(', ', $setClause) . " WHERE id = :id";
$filteredData['id'] = $id;
$stmt = $this->pdo->prepare($sql);
return $stmt->execute($filteredData);
}
public function delete(int $id): bool
{
$stmt = $this->pdo->prepare("DELETE FROM {$this->table} WHERE id = :id");
return $stmt->execute(['id' => $id]);
}
public function count(array $conditions = []): int
{
$whereClause = $this->buildWhereClause($conditions);
$sql = "SELECT COUNT(*) FROM {$this->table} {$whereClause['sql']}";
$stmt = $this->pdo->prepare($sql);
$stmt->execute($whereClause['params']);
return (int) $stmt->fetchColumn();
}
protected function buildWhereClause(array $conditions): array
{
if (empty($conditions)) {
return ['sql' => '', 'params' => []];
}
$parts = [];
$params = [];
foreach ($conditions as $column => $value) {
if (is_array($value)) {
$parts[] = "$column IN (" . implode(', ', array_map(fn($k) => ":$column$k", array_keys($value))) . ")";
foreach ($value as $k => $v) {
$params["$column$k"] = $v;
}
} else {
$parts[] = "$column = :$column";
$params[$column] = $value;
}
}
return [
'sql' => 'WHERE ' . implode(' AND ', $parts),
'params' => $params
];
}
public function getTable(): string
{
return $this->table;
}
}