~cypheon/nextcloud-chores-app

ref: d19a6603c01e63c7545582e5ff313ef4aec0bf9f nextcloud-chores-app/lib/Db/Chore.php -rw-r--r-- 1.7 KiB
d19a6603 — Johann Rudloff Make overdue sidebar badge dynamic, switch badges to `CounterBubble` component 10 months ago
                                                                                
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
68
69
70
71
72
73
74
75
<?php
namespace OCA\Chores\Db;

use JsonSerializable;

use OCP\AppFramework\Db\Entity;

use OCA\Chores\Service\Schedule;

class Chore extends Entity implements JsonSerializable {

  protected $teamId;
  protected $name;
  protected $assigneeUid;
  protected $points;
  protected $due;
  protected $repeat;

  public function __construct() {
    $this->addType('id', 'integer');
    $this->addType('teamId', 'integer');
    $this->addType('points', 'integer');
    $this->addType('due', 'datetimetz_immutable');
  }

  public function getDue(): string {
    return $this->getDueDate()->format("c");
  }

  public function setDue(string $due) {
    $this->due = new \DateTimeImmutable($due);
    $this->markFieldUpdated("due");
  }

  public function getDueDate(): \DateTimeImmutable {
    return $this->due;
  }

  public function setDueDate(\DateTimeImmutable $dueDate) {
    $this->due = $dueDate;
    $this->markFieldUpdated("due");
  }

  public function getSchedule(): Schedule {
    return Schedule::parseSchedule($this->repeat);
  }

  public function updateFromJson(array $props) {
      $allowedProperties = [
        "name" => "setName",
        "assignee" => "setAssigneeUid",
        "points" => "setPoints",
        "due" => "setDue",
        "repeat" => "setRepeat",
      ];
      foreach ($allowedProperties as $propName => $setter) {
        if (array_key_exists($propName, $props)) {
          error_log("$setter (" . $props[$propName] . ")");
          $this->$setter($props[$propName]);
        }
      }
  }

  public function jsonSerialize() {
    return [
      'id' => $this->id,
      'name' => $this->name,
      'assignee' => $this->assigneeUid,
      'points' => $this->points,
      'due' => $this->getDue(),
      'repeat' => $this->repeat,
    ];
  }
}