-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.php
67 lines (52 loc) · 1.17 KB
/
tasks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
function getTasks()
{
$tasks = json_decode(file_get_contents('tasks.json'), true);
return $tasks;
}
function createTask(string $title)
{
$tasks = getTasks();
$task = [
"id" => count($tasks),
"title" => $title,
"isDone" => false
];
$tasks[] = $task;
return writeJson($tasks);
}
function writeJson($updatedTasks)
{
$tasks = json_encode($updatedTasks, JSON_PRETTY_PRINT);
$filename = 'tasks.json';
return file_put_contents($filename, $tasks);
}
function getTaskById(string $id)
{
$tasks = getTasks();
for ($i = 0; $i < count($tasks); $i++)
{
$currentTask = $tasks[$i];
if ($currentTask['id'] == $id)
return $currentTask;
}
return null;
}
function toggleDone(string $id)
{
$task = getTaskById($id);
$task['isDone'] = !$task['isDone'];
$tasks = getTasks();
$tasks[$id] = array_merge($tasks[$id], $task);
return writeJson($tasks);
}
function deleteTask(string $id)
{
$tasks = getTasks();
array_splice($tasks, $id, 1);
for ($i = 0; $i < count($tasks); $i++)
{
$tasks[$i]['id'] = $i;
}
return writeJson($tasks);
}