Files
KanbanGit/app/Actions/Tickets/Support/AcceptanceCriteriaChecklistMapper.php
T
Koda 456b165987
linter / quality (pull_request) Successful in 1m22s
tests / ci (8.4) (pull_request) Successful in 1m32s
Rename acceptance criteria checklist helper to mapper
2026-05-19 23:35:14 +00:00

147 lines
4.4 KiB
PHP

<?php
namespace App\Actions\Tickets\Support;
use App\Models\AcceptanceCriterion;
use App\Models\Ticket;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Support\Arr;
class AcceptanceCriteriaChecklistMapper
{
public const MAX_INDENT_LEVEL = 8;
/**
* @param iterable<AcceptanceCriterion> $criteria
* @return array<int, array{checked: bool, indent: int, text: string}>
*/
public static function fromModels(iterable $criteria): array
{
$models = ($criteria instanceof EloquentCollection ? $criteria : collect($criteria))
->sort(function (AcceptanceCriterion $left, AcceptanceCriterion $right): int {
return [
$left->parent_acceptance_criterion_id ?? 0,
$left->position,
$left->id,
] <=> [
$right->parent_acceptance_criterion_id ?? 0,
$right->position,
$right->id,
];
})
->values();
if ($models->isEmpty()) {
return [];
}
/** @var array<int|string, iterable<AcceptanceCriterion>> $grouped */
$grouped = $models->groupBy('parent_acceptance_criterion_id')->all();
$items = [];
self::flattenItems($items, $grouped, null, 0);
return $items;
}
/**
* @return array<int, array{checked: bool, indent: int, text: string}>
*/
public static function normalize(mixed $value): array
{
if (is_array($value)) {
return self::normalizeFromArray($value);
}
return [];
}
/**
* @param Ticket $ticket
* @param array<int, array{checked: bool, indent: int, text: string}> $items
*/
public static function syncTicket(Ticket $ticket, array $items): void
{
$normalized = self::normalize($items);
$ticket->acceptanceCriteria()->delete();
if ($normalized === []) {
return;
}
$parentStack = [];
$positionsByParent = [];
foreach ($normalized as $item) {
$indent = min($item['indent'], count($parentStack));
$parentId = $indent === 0 ? null : $parentStack[$indent - 1];
$positionKey = $parentId ?? 'root';
$position = $positionsByParent[$positionKey] ?? 0;
$criterion = $ticket->acceptanceCriteria()->create([
'parent_acceptance_criterion_id' => $parentId,
'position' => $position,
'text' => $item['text'],
'is_checked' => $item['checked'],
]);
$positionsByParent[$positionKey] = $position + 1;
$parentStack[$indent] = $criterion->id;
$parentStack = array_slice($parentStack, 0, $indent + 1);
}
$ticket->unsetRelation('acceptanceCriteria');
}
/**
* @param array<int, array{checked: bool, indent: int, text: string}> $items
* @param array<int|string, iterable<AcceptanceCriterion>> $grouped
*/
private static function flattenItems(array &$items, array $grouped, ?int $parentId, int $indent): void
{
foreach ($grouped[$parentId] ?? [] as $criterion) {
$items[] = [
'checked' => $criterion->is_checked,
'indent' => $indent,
'text' => $criterion->text,
];
self::flattenItems($items, $grouped, $criterion->id, $indent + 1);
}
}
/**
* @param array<int|string, mixed> $value
* @return array<int, array{checked: bool, indent: int, text: string}>
*/
private static function normalizeFromArray(array $value): array
{
$items = [];
foreach ($value as $rawItem) {
if (! is_array($rawItem)) {
continue;
}
$text = trim((string) Arr::get($rawItem, 'text', ''));
if ($text === '') {
continue;
}
$items[] = [
'checked' => filter_var(Arr::get($rawItem, 'checked', false), FILTER_VALIDATE_BOOLEAN),
'indent' => self::clampIndent((int) Arr::get($rawItem, 'indent', 0)),
'text' => $text,
];
}
return $items;
}
private static function clampIndent(int $indent): int
{
return max(0, min(self::MAX_INDENT_LEVEL, $indent));
}
}