Files
awo-hamburg-intranet/module/tasks/repo/TaskChangeLogRepository.php

59 lines
1.4 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
class TaskChangeLogRepository
{
public function create(
int $taskId,
string $action,
int $changedBy,
?array $beforeSnapshot,
?array $afterSnapshot,
?array $diff
): int {
return TaskCertDb::insert(
'INSERT INTO task_change_log (
task_id,
action,
changed_by,
before_json,
after_json,
diff_json
) VALUES (?, ?, ?, ?, ?, ?)',
'isisss',
[
$taskId,
$action,
$changedBy,
$this->encodeJsonOrNull($beforeSnapshot),
$this->encodeJsonOrNull($afterSnapshot),
$this->encodeJsonOrNull($diff),
]
);
}
public function listByTask(int $taskId, int $limit = 100): array
{
$limit = max(1, min(1000, $limit));
return TaskCertDb::fetchAll(
'SELECT *
FROM task_change_log
WHERE task_id = ?
ORDER BY id DESC
LIMIT ' . $limit,
'i',
[$taskId]
);
}
private function encodeJsonOrNull(?array $value): ?string
{
if ($value === null) {
return null;
}
return json_encode($value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
}