From 716fc8bea94d7dd482c7ab55bf2e322bbcea5482 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Tue, 9 Jan 2024 11:28:38 -0600 Subject: [PATCH] Use setter function when setting artwork tags --- lib/Artwork.php | 36 +++++++++++++++++++++--------------- www/artworks/post.php | 8 ++------ 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/lib/Artwork.php b/lib/Artwork.php index c872fb9a..b2f4ad58 100644 --- a/lib/Artwork.php +++ b/lib/Artwork.php @@ -56,6 +56,27 @@ class Artwork extends PropertiesBase{ protected ?User $_Reviewer = null; protected ?ImageMimeType $_MimeType = null; + // ******* + // SETTERS + // ******* + + protected function SetTags(string|array|null $tags): void{ + if($tags === null || is_array($tags)){ + $this->_Tags = $tags; + } + elseif(is_string($tags)){ + $tags = array_map('trim', explode(',', $tags)); + $tags = array_values(array_filter($tags)); + $tags = array_unique($tags); + + $this->_Tags = array_map(function ($str){ + $tag = new ArtworkTag(); + $tag->Name = $str; + return $tag; + }, $tags); + } + } + // ******* // GETTERS // ******* @@ -382,21 +403,6 @@ class Artwork extends PropertiesBase{ } } - /** @return array */ - public static function ParseTags(?string $tags): array{ - if(!$tags) return []; - - $tags = array_map('trim', explode(',', $tags)); - $tags = array_values(array_filter($tags)); - $tags = array_unique($tags); - - return array_map(function ($str){ - $tag = new ArtworkTag(); - $tag->Name = $str; - return $tag; - }, $tags); - } - /** * @param array $uploadedFile * @throws \Exceptions\ValidationException diff --git a/www/artworks/post.php b/www/artworks/post.php index e8021fae..9b7d521b 100644 --- a/www/artworks/post.php +++ b/www/artworks/post.php @@ -30,7 +30,7 @@ try{ $artwork->Name = HttpInput::Str(POST, 'artwork-name', false); $artwork->CompletedYear = HttpInput::Int(POST, 'artwork-year'); $artwork->CompletedYearIsCirca = HttpInput::Bool(POST, 'artwork-year-is-circa', false); - $artwork->Tags = Artwork::ParseTags(HttpInput::Str(POST, 'artwork-tags', false)); + $artwork->Tags = HttpInput::Str(POST, 'artwork-tags', false); $artwork->Status = HttpInput::Str(POST, 'artwork-status', false, COVER_ARTWORK_STATUS_UNVERIFIED); $artwork->EbookWwwFilesystemPath = HttpInput::Str(POST, 'artwork-ebook-www-filesystem-path', false); $artwork->SubmitterUserId = $GLOBALS['User']->UserId ?? null; @@ -76,6 +76,7 @@ try{ $artwork->Name = HttpInput::Str(POST, 'artwork-name', false) ?? $artwork->Name; $artwork->CompletedYear = HttpInput::Int(POST, 'artwork-year') ?? $artwork->CompletedYear; $artwork->CompletedYearIsCirca = HttpInput::Bool(POST, 'artwork-year-is-circa', false) ?? $artwork->CompletedYearIsCirca; + $artwork->Tags = HttpInput::Str(POST, 'artwork-tags', false) ?? $artwork->Tags; $artwork->Status = HttpInput::Str(POST, 'artwork-status', false) ?? $artwork->Status; $artwork->EbookWwwFilesystemPath = HttpInput::Str(POST, 'artwork-ebook-www-filesystem-path', false) ?? $artwork->EbookWwwFilesystemPath; $artwork->IsPublishedInUs = HttpInput::Bool(POST, 'artwork-is-published-in-us', false) ?? $artwork->IsPublishedInUs; @@ -86,11 +87,6 @@ try{ $artwork->MuseumUrl = HttpInput::Str(POST, 'artwork-museum-url', false) ?? $artwork->MuseumUrl; $artwork->Exception = HttpInput::Str(POST, 'artwork-exception', false) ?? $artwork->Exception; - $tagsString = HttpInput::Str(POST, 'artwork-tags', false); - if($tagsString !== null){ - $artwork->Tags = Artwork::ParseTags($tagsString); - } - $artwork->ReviewerUserId = $GLOBALS['User']->UserId; $artwork->Save();