Make collection type (series, set) an enum

This commit is contained in:
Mike Colagrosso 2024-08-23 14:19:10 -06:00 committed by Alex Cabal
parent d3b7f5015a
commit b792dec9e5
4 changed files with 10 additions and 4 deletions

View file

@ -2,7 +2,7 @@ CREATE TABLE `Collections` (
`CollectionId` int(10) unsigned NOT NULL AUTO_INCREMENT,
`Name` varchar(255) NOT NULL,
`UrlName` varchar(255) NOT NULL,
`Type` varchar(255) NULL,
`Type` enum('series', 'set') NULL,
PRIMARY KEY (`CollectionId`),
UNIQUE KEY `idxUnique` (`UrlName`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

View file

@ -10,7 +10,7 @@ class Collection{
public int $CollectionId;
public string $Name;
public string $UrlName;
public ?string $Type = null;
public ?CollectionType $Type = null;
protected ?string $_Url = null;
protected function GetUrl(): ?string{
@ -59,7 +59,7 @@ class Collection{
$error->Add(new Exceptions\StringTooLongException('Collection name: '. $this->Name));
}
if($this->Type !== null && ($this->Type != 'series' && $this->Type != 'set')){
if($this->Type !== null && ($this->Type != CollectionType::Series && $this->Type != CollectionType::Set)){
$error->Add(new Exceptions\InvalidCollectionTypeException($this->Type));
}

6
lib/CollectionType.php Normal file
View file

@ -0,0 +1,6 @@
<?
enum CollectionType: string{
case Series = 'series';
case Set = 'set';
case Unknown = 'unknown';
}

View file

@ -762,7 +762,7 @@ class Ebook{
$cm->SequenceNumber = (int)$s;
}
foreach($xml->xpath('/package/metadata/meta[@refines="#' . $id . '"][@property="collection-type"]') ?: [] as $s){
$cm->Collection->Type = (string)$s;
$cm->Collection->Type = CollectionType::tryFrom((string)$s) ?? CollectionType::Unknown;
}
$collectionMemberships[] = $cm;
}