<?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,
];
}
}