1<?php
2/*
3 * Yurii's Gantt Plugin
4 *
5 * Copyright (C) 2020 Yurii K.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program.  If not, see http://www.gnu.org/licenses
19 */
20
21namespace dokuwiki\plugin\yuriigantt\src\Entities;
22
23class Task implements \JsonSerializable
24{
25    const DATE_FORMAT = 'd-m-Y H:i';
26
27    /** @var int primary */
28    public $id;
29    /** @var string  */
30    public $text;
31    /** @var \DateTime|false  */
32    public $start_date;
33    /** @var int  */
34    public $duration;
35    /** @var float */
36    public $progress;
37    /** @var int */
38    public $parent;
39    /** @var bool */
40    public $open;
41    /** @var int */
42    public $order;
43    /** @var string|null */
44    public $target;
45
46
47    /**
48     * Task constructor.
49     * @param \stdClass|null $data
50     */
51    public function __construct($data)
52    {
53        if (!$data) {
54            return;
55        }
56
57        $this->id = $data->id;
58        $this->text = $data->text;
59        $this->start_date = \DateTime::createFromFormat(self::DATE_FORMAT, $data->start_date);
60        $this->duration = $data->duration;
61        $this->progress = $data->progress;
62        $this->parent = (int)$data->parent;
63        $this->open = (bool)(isset($data->open) ? $data->open : true);
64        $this->order = (int)$data->order;
65        $this->target = isset($data->target) ? $data->target : null;
66    }
67
68
69    /**
70     * {@inheritdoc}
71     */
72    public function jsonSerialize()
73    {
74        $arr = (array)$this;
75        $arr['start_date'] = $this->start_date->format(self::DATE_FORMAT);
76        $arr['progress'] = round($this->progress, 4);
77
78        return $arr;
79    }
80}
81