todolistPhp / classes / Tasks.class.php
Tasks.class.php
Raw
<?php

class Tasks {
/*--------properties--------*/
    // The name for the task
    private $taskName;
    // The deadline for the task
    private $deadLine;
    // list of all the tasks
    private $todoList; 

/*--------methods--------*/
    /*---Constructor----*/ 
    function __construct() {
        //controll if the json file exist 
        if(file_exists('todolist.json')){
            // read file
            $file = file_get_contents('todolist.json');
            // convert to array
            $this->todoList = json_decode($file, true);
        }
        else {
            // return empty arrya
            $this->todoList = [];
        }
    }

    /*---Setter for the task name----*/ 
    function setTaskName(string $taskName) : bool {
        // control if we have less than 5 charectures and not en empty spaces 
        if(strlen(trim($taskName)) > 4 ){
            $this->taskName = $taskName;
            return true;
        }
        return false;
    }

    /*---Setter for task deadline----*/ 
    function setTaskDeadline(string $deadLine): bool{
        if($deadLine >= strtotime("today")){
            $this->deadLine = $deadLine ;
            return true;
        }
        return false;
    }

    /*---save all the tasks to a list ----*/ 
    function saveTodo() : bool {
        // Gather info
        $data['taskName']= $this->taskName;
        $data['deadLine'] = date('Y-m-d',($this->deadLine));
        // Add to array
        array_push($this->todoList, $data);
        // convert to json 
        $jsonData= json_encode($this->todoList, JSON_PRETTY_PRINT);
        // save file 
        if(file_put_contents('todolist.json', $jsonData)){
            return true;
        }
        else {
            return false;
        }
    }

 /*---Getter for the todo list ----*/  
    function getTodoList(): array {
        return $this->todoList; 
    }

    function deleteAll() : array {
        $this->todoList = [];
        file_put_contents('todolist.json', json_encode($this->todoList, JSON_PRETTY_PRINT));
        return $this->todoList;
    }
    
}
?>