From 28f8f11b9a6efc7b1b4a4bcd809ddffcde878c45 Mon Sep 17 00:00:00 2001 From: Jochen Date: Tue, 12 Dec 2023 22:44:11 +0100 Subject: [PATCH] [TASK] Add methods to add a task before/after a task in a task group --- src/Task/GroupTask.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/Task/GroupTask.php b/src/Task/GroupTask.php index a43a77eb2..57ac56ce5 100644 --- a/src/Task/GroupTask.php +++ b/src/Task/GroupTask.php @@ -49,4 +49,31 @@ public function setGroup(array $group): void { $this->group = $group; } + + public function addTaskBefore(string $task, string $addTask): void + { + $this->addTask($task, $addTask); + } + + public function addTaskAfter(string $task, string $addTask): void + { + $this->addTask($task, $addTask, 'after'); + } + + public function addTask(string $task, string $addTask, string $position = 'before'): void + { + $taskPosition = array_search($task, $this->group); + if(!$taskPosition) { + throw new \InvalidArgumentException("Task `$task` not found."); + } + + switch ($position) { + case 'before': + array_splice($this->group, $taskPosition, 0, [$addTask]); + break; + case 'after': + array_splice($this->group, $taskPosition + 1, 0, [$addTask]); + break; + } + } }