mirror of
https://github.com/standardebooks/web.git
synced 2025-07-08 07:40:39 -04:00
Add support for filling backed enum types in the DB ORM layer, and cleanup some formatting issues
This commit is contained in:
parent
e7a9790147
commit
5b3f8f7b77
8 changed files with 57 additions and 54 deletions
|
@ -17,7 +17,7 @@ CREATE TABLE `Artworks` (
|
||||||
`ArtworkPageUrl` varchar(255) NULL,
|
`ArtworkPageUrl` varchar(255) NULL,
|
||||||
`IsPublishedInUs` tinyint(1) NOT NULL DEFAULT FALSE,
|
`IsPublishedInUs` tinyint(1) NOT NULL DEFAULT FALSE,
|
||||||
`EbookUrl` varchar(255) NULL,
|
`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,
|
`Exception` TEXT NULL DEFAULT NULL,
|
||||||
`Notes` TEXT NULL DEFAULT NULL
|
`Notes` TEXT NULL DEFAULT NULL
|
||||||
PRIMARY KEY (`ArtworkId`),
|
PRIMARY KEY (`ArtworkId`),
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
<?
|
<?
|
||||||
use Safe\DateTimeImmutable;
|
use Safe\DateTimeImmutable;
|
||||||
use function Safe\date;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @property ?int $DeathYear
|
* @property ?int $DeathYear
|
||||||
|
|
|
@ -1,14 +1,8 @@
|
||||||
<?
|
<?
|
||||||
|
|
||||||
use Exceptions\InvalidUrlException;
|
|
||||||
use Safe\DateTimeImmutable;
|
use Safe\DateTimeImmutable;
|
||||||
|
|
||||||
use function Safe\apcu_cache_info;
|
|
||||||
use function Safe\copy;
|
use function Safe\copy;
|
||||||
use function Safe\date;
|
|
||||||
use function Safe\exec;
|
use function Safe\exec;
|
||||||
use function Safe\getimagesize;
|
use function Safe\getimagesize;
|
||||||
use function Safe\ini_get;
|
|
||||||
use function Safe\parse_url;
|
use function Safe\parse_url;
|
||||||
use function Safe\preg_match;
|
use function Safe\preg_match;
|
||||||
use function Safe\preg_replace;
|
use function Safe\preg_replace;
|
||||||
|
@ -26,13 +20,10 @@ use function Safe\preg_replace;
|
||||||
* @property string $ThumbFsPath
|
* @property string $ThumbFsPath
|
||||||
* @property string $Thumb2xFsPath
|
* @property string $Thumb2xFsPath
|
||||||
* @property string $Dimensions
|
* @property string $Dimensions
|
||||||
* @property ArtworkStatus|string|null $Status
|
|
||||||
* @property Ebook $Ebook
|
* @property Ebook $Ebook
|
||||||
* @property Museum $Museum
|
* @property Museum $Museum
|
||||||
* @property User $Submitter
|
* @property User $Submitter
|
||||||
* @property User $Reviewer
|
* @property User $Reviewer
|
||||||
* @property ?ImageMimeType $MimeType
|
|
||||||
* @property ?array<ArtworkTag> $_Tags
|
|
||||||
*/
|
*/
|
||||||
class Artwork extends Accessor{
|
class Artwork extends Accessor{
|
||||||
public ?string $Name = null;
|
public ?string $Name = null;
|
||||||
|
@ -53,8 +44,11 @@ class Artwork extends Accessor{
|
||||||
public ?bool $IsPublishedInUs = null;
|
public ?bool $IsPublishedInUs = null;
|
||||||
public ?string $Exception = null;
|
public ?string $Exception = null;
|
||||||
public ?string $Notes = null;
|
public ?string $Notes = null;
|
||||||
|
public ?ImageMimeType $MimeType = null;
|
||||||
|
public ?ArtworkStatus $Status = null;
|
||||||
protected ?string $_UrlName = null;
|
protected ?string $_UrlName = null;
|
||||||
protected ?string $_Url = null;
|
protected ?string $_Url = null;
|
||||||
|
protected ?string $_EditUrl = null;
|
||||||
protected $_Tags = null;
|
protected $_Tags = null;
|
||||||
protected ?Artist $_Artist = null;
|
protected ?Artist $_Artist = null;
|
||||||
protected ?string $_ImageUrl = null;
|
protected ?string $_ImageUrl = null;
|
||||||
|
@ -65,8 +59,6 @@ class Artwork extends Accessor{
|
||||||
protected ?Museum $_Museum = null;
|
protected ?Museum $_Museum = null;
|
||||||
protected ?User $_Submitter = null;
|
protected ?User $_Submitter = null;
|
||||||
protected ?User $_Reviewer = null;
|
protected ?User $_Reviewer = null;
|
||||||
protected ?ImageMimeType $_MimeType = null;
|
|
||||||
protected ?ArtworkStatus $_Status = null;
|
|
||||||
|
|
||||||
// *******
|
// *******
|
||||||
// SETTERS
|
// 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
|
// GETTERS
|
||||||
// *******
|
// *******
|
||||||
|
@ -167,7 +135,11 @@ class Artwork extends Accessor{
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function GetEditUrl(): string{
|
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{
|
protected function GetDimensions(): string{
|
||||||
$this->_Dimensions = '';
|
$this->_Dimensions = '';
|
||||||
try{
|
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){
|
if($imageWidth && $imageHeight){
|
||||||
$this->_Dimensions = number_format($imageWidth) . ' × ' . number_format($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->CompletedYear = HttpInput::Int(POST, 'artwork-year');
|
||||||
$artwork->CompletedYearIsCirca = HttpInput::Bool(POST, 'artwork-year-is-circa') ?? false;
|
$artwork->CompletedYearIsCirca = HttpInput::Bool(POST, 'artwork-year-is-circa') ?? false;
|
||||||
$artwork->Tags = HttpInput::Str(POST, 'artwork-tags') ?? [];
|
$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->EbookUrl = HttpInput::Str(POST, 'artwork-ebook-url');
|
||||||
$artwork->IsPublishedInUs = HttpInput::Bool(POST, 'artwork-is-published-in-us') ?? false;
|
$artwork->IsPublishedInUs = HttpInput::Bool(POST, 'artwork-is-published-in-us') ?? false;
|
||||||
$artwork->PublicationYear = HttpInput::Int(POST, 'artwork-publication-year');
|
$artwork->PublicationYear = HttpInput::Int(POST, 'artwork-publication-year');
|
||||||
|
|
|
@ -219,6 +219,39 @@ class DbConnection{
|
||||||
$object->{$metadata[$i]['name']} = $row[$i] == 1 ? true : false;
|
$object->{$metadata[$i]['name']} = $row[$i] == 1 ? true : false;
|
||||||
break;
|
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:
|
default:
|
||||||
$object->{$metadata[$i]['name']} = $row[$i];
|
$object->{$metadata[$i]['name']} = $row[$i];
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
<?
|
<?
|
||||||
|
|
||||||
enum EbookFormat: string{
|
enum EbookFormat: string{
|
||||||
case Epub = 'epub';
|
case Epub = 'epub';
|
||||||
case Azw3 = 'azw3';
|
case Azw3 = 'azw3';
|
||||||
|
|
|
@ -1,16 +1,4 @@
|
||||||
<?
|
<?
|
||||||
enum EbookSourceType{
|
|
||||||
case ProjectGutenberg;
|
|
||||||
case ProjectGutenbergAustralia;
|
|
||||||
case ProjectGutenbergCanada;
|
|
||||||
case InternetArchive;
|
|
||||||
case HathiTrust;
|
|
||||||
case Wikisource;
|
|
||||||
case GoogleBooks;
|
|
||||||
case FadedPage;
|
|
||||||
case Other;
|
|
||||||
}
|
|
||||||
|
|
||||||
class EbookSource{
|
class EbookSource{
|
||||||
public EbookSourceType $Type;
|
public EbookSourceType $Type;
|
||||||
public string $Url;
|
public string $Url;
|
||||||
|
|
12
lib/EbookSourceType.php
Normal file
12
lib/EbookSourceType.php
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<?
|
||||||
|
enum EbookSourceType{
|
||||||
|
case ProjectGutenberg;
|
||||||
|
case ProjectGutenbergAustralia;
|
||||||
|
case ProjectGutenbergCanada;
|
||||||
|
case InternetArchive;
|
||||||
|
case HathiTrust;
|
||||||
|
case Wikisource;
|
||||||
|
case GoogleBooks;
|
||||||
|
case FadedPage;
|
||||||
|
case Other;
|
||||||
|
}
|
|
@ -1,5 +1,4 @@
|
||||||
<?
|
<?
|
||||||
|
|
||||||
use Exceptions\InvalidLoginException;
|
use Exceptions\InvalidLoginException;
|
||||||
use Ramsey\Uuid\Uuid;
|
use Ramsey\Uuid\Uuid;
|
||||||
use Safe\DateTimeImmutable;
|
use Safe\DateTimeImmutable;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue