diff options
| author | winter | 2024-12-17 20:03:55 +0000 |
|---|---|---|
| committer | winter | 2024-12-17 20:03:55 +0000 |
| commit | 7cb4ef6c6b82bbca3300154ae89a7bb05c1d91cd (patch) | |
| tree | 24328c094598835913152affca844d208f10bd59 /Digitigrade/Model/Note.php | |
| parent | 1243bf45f04d5292c4f147b9a46c878d19cf2ba5 (diff) | |
add methods to create models easierly
Diffstat (limited to 'Digitigrade/Model/Note.php')
| -rw-r--r-- | Digitigrade/Model/Note.php | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/Digitigrade/Model/Note.php b/Digitigrade/Model/Note.php index 1e867fa..e80d972 100644 --- a/Digitigrade/Model/Note.php +++ b/Digitigrade/Model/Note.php @@ -29,6 +29,43 @@ class Note extends PushableModel { public array $attachments = []; public array $extensions = []; + /** + * Creates and saves a new note with the specified fields pre-set + * @param Actor $author who wrote the note + * @param string $plainContent text of the note, without any markup + * @param string $language ISO 639-3 3-letter language code + * @param ?string $summary summary line or content warning + * @param NotePrivacyScope $scope who is allowed to view this note + * @param bool $indexable whether this note should be findable e.g. in search results + * @return self + */ + public static function create( + Actor $author, + string $plainContent, + string $language, + ?string $summary = null, + NotePrivacyScope $scope = NotePrivacyScope::PUBLIC , + bool $indexable = true + ): self { + $note = new self(); + // can't set the note uri properly until it's been saved and we have an id + $note->uri = "UNKNOWN"; + $note->created = new \DateTimeImmutable(); + $note->author = $author; + $note->summary = $summary; + $note->plainContent = $plainContent; + $note->language = $language; + $note->privacy = new NotePrivacy(); + $note->privacy->scope = $scope; + $note->privacy->indexable = $indexable; + + $note->save(); + $note->uri = path_to_uri("/post/$note->id"); + $note->save(); + + return $note; + } + protected function getUpdateWhereClause(\PDO $db): ?string { if (self::findWhere('uri = ?', [$this->uri]) != null) return 'uri = ' . $db->quote($this->uri); |
