repository = $repository; return $this; } public function getRepository(): Repository { return $this->repository; } protected int $id; public function setId(int $id): ModelInterface { $this->id = $id; return $this; } public function getId(): int { return $this->id; } protected bool $new; public function setNew(): ModelInterface { $this->new = true; return $this; } public function isNew(): bool { return $this->new ?? false; } protected bool $dirty; public function setDirty(): ModelInterface { $this->dirty = true; return $this; } public function isDirty(): bool { return $this->dirty ?? $this->isNew(); } public function save(): void { if ($this->isDirty()) { $this->getRepository()->save($this); } } public function edit(array $data): void { foreach ($data as $key => $val) { $m = 'set' . ucwords($key); $this->{$m}($val); } $this->isDirty(); } public function delete(): void { $this->getRepository()->delete($this); } public function jsonSerialize(): mixed { $obj = new \ReflectionObject($this); $methods = $obj->getMethods(); $output = []; foreach ($methods as $method) { if (!str_contains($method->getName(), 'get')) { continue; } if ($method->getName() === 'getRepository') { continue; } $p = strtolower(str_replace('get', '', $method->getName())); if (!isset($this->{$p})) { continue; } $output[$p] = $this->{$method->getName()}(); } return $output; } }