Use setter function when setting artwork tags

This commit is contained in:
Alex Cabal 2024-01-09 11:28:38 -06:00
parent 8a82524de7
commit 716fc8bea9
2 changed files with 23 additions and 21 deletions

View file

@ -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<ArtworkTag> */
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<mixed> $uploadedFile
* @throws \Exceptions\ValidationException

View file

@ -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();