From 5b3f8f7b7732d98f2eecc6ac7b2fd8481af2d9e0 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Thu, 25 Apr 2024 11:53:39 -0500 Subject: [PATCH] Add support for filling backed enum types in the DB ORM layer, and cleanup some formatting issues --- config/sql/se/Artworks.sql | 2 +- lib/Artist.php | 1 - lib/Artwork.php | 49 +++++++++----------------------------- lib/DbConnection.php | 33 +++++++++++++++++++++++++ lib/EbookFormat.php | 1 - lib/EbookSource.php | 12 ---------- lib/EbookSourceType.php | 12 ++++++++++ lib/Session.php | 1 - 8 files changed, 57 insertions(+), 54 deletions(-) create mode 100644 lib/EbookSourceType.php diff --git a/config/sql/se/Artworks.sql b/config/sql/se/Artworks.sql index f2a7d96e..4262bfb8 100644 --- a/config/sql/se/Artworks.sql +++ b/config/sql/se/Artworks.sql @@ -17,7 +17,7 @@ CREATE TABLE `Artworks` ( `ArtworkPageUrl` varchar(255) NULL, `IsPublishedInUs` tinyint(1) NOT NULL DEFAULT FALSE, `EbookUrl` varchar(255) NULL, - `MimeType` varchar(64) NOT NULL, + `MimeType` enum('image/jpeg', 'image/png', 'image/bmp', 'image/tiff') NOT NULL, `Exception` TEXT NULL DEFAULT NULL, `Notes` TEXT NULL DEFAULT NULL PRIMARY KEY (`ArtworkId`), diff --git a/lib/Artist.php b/lib/Artist.php index 735f4988..844bca80 100644 --- a/lib/Artist.php +++ b/lib/Artist.php @@ -1,6 +1,5 @@ $_Tags */ class Artwork extends Accessor{ public ?string $Name = null; @@ -53,8 +44,11 @@ class Artwork extends Accessor{ public ?bool $IsPublishedInUs = null; public ?string $Exception = null; public ?string $Notes = null; + public ?ImageMimeType $MimeType = null; + public ?ArtworkStatus $Status = null; protected ?string $_UrlName = null; protected ?string $_Url = null; + protected ?string $_EditUrl = null; protected $_Tags = null; protected ?Artist $_Artist = null; protected ?string $_ImageUrl = null; @@ -65,8 +59,6 @@ class Artwork extends Accessor{ protected ?Museum $_Museum = null; protected ?User $_Submitter = null; protected ?User $_Reviewer = null; - protected ?ImageMimeType $_MimeType = null; - protected ?ArtworkStatus $_Status = null; // ******* // SETTERS @@ -92,30 +84,6 @@ class Artwork extends Accessor{ } } - protected function SetStatus(null|string|ArtworkStatus $status): void{ - if($status instanceof ArtworkStatus){ - $this->_Status = $status; - } - elseif($status === null){ - $this->_Status = null; - } - else{ - $this->_Status = ArtworkStatus::from($status); - } - } - - protected function SetMimeType(null|string|ImageMimeType $mimeType): void{ - if($mimeType instanceof ImageMimeType){ - $this->_MimeType = $mimeType; - } - elseif($mimeType === null){ - $this->_MimeType = null; - } - else{ - $this->_MimeType = ImageMimeType::tryFrom($mimeType); - } - } - // ******* // GETTERS // ******* @@ -167,7 +135,11 @@ class Artwork extends Accessor{ } protected function GetEditUrl(): string{ - return $this->Url . '/edit'; + if($this->_EditUrl === null){ + $this->_EditUrl = $this->Url . '/edit'; + } + + return $this->_EditUrl; } /** @@ -265,7 +237,8 @@ class Artwork extends Accessor{ protected function GetDimensions(): string{ $this->_Dimensions = ''; try{ - list($imageWidth, $imageHeight) = getimagesize($this->ImageFsPath); + // Safe\getimagesize() emits a warning if the file doesn't exist + list($imageWidth, $imageHeight) = @getimagesize($this->ImageFsPath); if($imageWidth && $imageHeight){ $this->_Dimensions = number_format($imageWidth) . ' × ' . number_format($imageHeight); } @@ -892,7 +865,7 @@ class Artwork extends Accessor{ $artwork->CompletedYear = HttpInput::Int(POST, 'artwork-year'); $artwork->CompletedYearIsCirca = HttpInput::Bool(POST, 'artwork-year-is-circa') ?? false; $artwork->Tags = HttpInput::Str(POST, 'artwork-tags') ?? []; - $artwork->Status = HttpInput::Str(POST, 'artwork-status') ?? ArtworkStatus::Unverified; + $artwork->Status = ArtworkStatus::tryFrom(HttpInput::Str(POST, 'artwork-status') ?? '') ?? ArtworkStatus::Unverified; $artwork->EbookUrl = HttpInput::Str(POST, 'artwork-ebook-url'); $artwork->IsPublishedInUs = HttpInput::Bool(POST, 'artwork-is-published-in-us') ?? false; $artwork->PublicationYear = HttpInput::Int(POST, 'artwork-publication-year'); diff --git a/lib/DbConnection.php b/lib/DbConnection.php index 9e12b7c7..903055b8 100644 --- a/lib/DbConnection.php +++ b/lib/DbConnection.php @@ -219,6 +219,39 @@ class DbConnection{ $object->{$metadata[$i]['name']} = $row[$i] == 1 ? true : false; break; + case 'STRING': + // We don't check the type VAR_STRING here because in MariaDB, enums are always of type STRING. + // Since this check is slow, we don't want to run it unnecessarily. + if($class == 'stdClass'){ + $object->{$metadata[$i]['name']} = $row[$i]; + } + else{ + // If the column is a string and we're filling a typed object, check if the object property is a backed enum. If so, generate it using from(). Otherwise, fill it with a string. + // Note: Using ReflectionProperty in this way is pretty slow. Maybe we'll think of a + // better way to automatically fill enum types later. + try{ + $rp = new ReflectionProperty($object, $metadata[$i]['name']); + /** @var ?ReflectionNamedType $property */ + $property = $rp->getType(); + if($property !== null){ + $type = $property->getName(); + if(is_a($type, 'BackedEnum', true)){ + $object->{$metadata[$i]['name']} = $type::from($row[$i]); + } + else{ + $object->{$metadata[$i]['name']} = $row[$i]; + } + } + else{ + $object->{$metadata[$i]['name']} = $row[$i]; + } + } + catch(\Exception){ + $object->{$metadata[$i]['name']} = $row[$i]; + } + } + break; + default: $object->{$metadata[$i]['name']} = $row[$i]; break; diff --git a/lib/EbookFormat.php b/lib/EbookFormat.php index fef29f90..b50b0ef6 100644 --- a/lib/EbookFormat.php +++ b/lib/EbookFormat.php @@ -1,5 +1,4 @@