From 5b1bb2a9f032463c20b5461a6f99da094f40c446 Mon Sep 17 00:00:00 2001 From: Mike Colagrosso Date: Thu, 3 Oct 2024 13:39:33 -0600 Subject: [PATCH] Change tag type from string to enum (TagType) --- lib/ArtworkTag.php | 8 ++++---- lib/EbookTag.php | 7 ++++--- lib/Exceptions/InvalidArtworkTagTypeException.php | 10 ++++++---- lib/Exceptions/InvalidEbookTagTypeException.php | 10 ++++++---- lib/Tag.php | 2 +- lib/TagType.php | 5 +++++ 6 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 lib/TagType.php diff --git a/lib/ArtworkTag.php b/lib/ArtworkTag.php index 7c44121b..7b27306c 100644 --- a/lib/ArtworkTag.php +++ b/lib/ArtworkTag.php @@ -4,7 +4,7 @@ use function Safe\preg_replace; class ArtworkTag extends Tag{ public function __construct(){ - $this->Type = 'artwork'; + $this->Type = TagType::Artwork; } // ******* @@ -45,7 +45,7 @@ class ArtworkTag extends Tag{ $error->Add(new Exceptions\InvalidArtworkTagNameException()); } - if($this->Type != 'artwork'){ + if($this->Type != TagType::Artwork){ $error->Add(new Exceptions\InvalidArtworkTagTypeException($this->Type)); } @@ -76,8 +76,8 @@ class ArtworkTag extends Tag{ SELECT * from Tags where Name = ? - and Type = "artwork" - ', [$artworkTag->Name], ArtworkTag::class); + and Type = ? + ', [$artworkTag->Name, TagType::Artwork], ArtworkTag::class); if(isset($result[0])){ return $result[0]; diff --git a/lib/EbookTag.php b/lib/EbookTag.php index 1263c448..34758d8d 100644 --- a/lib/EbookTag.php +++ b/lib/EbookTag.php @@ -1,7 +1,7 @@ Type = 'ebook'; + $this->Type = TagType::Ebook; } // ******* @@ -37,7 +37,7 @@ class EbookTag extends Tag{ $error->Add(new Exceptions\StringTooLongException('Ebook tag: '. $this->Name)); } - if($this->Type != 'ebook'){ + if($this->Type != TagType::Ebook){ $error->Add(new Exceptions\InvalidEbookTagTypeException($this->Type)); } @@ -69,7 +69,8 @@ class EbookTag extends Tag{ SELECT * from Tags where Name = ? - ', [$name], EbookTag::class); + and Type = ? + ', [$name, TagType::Ebook], EbookTag::class); if(isset($result[0])){ return $result[0]; diff --git a/lib/Exceptions/InvalidArtworkTagTypeException.php b/lib/Exceptions/InvalidArtworkTagTypeException.php index 4a9ecd93..57b435fb 100644 --- a/lib/Exceptions/InvalidArtworkTagTypeException.php +++ b/lib/Exceptions/InvalidArtworkTagTypeException.php @@ -1,13 +1,15 @@ message .= ' Type provided: ' . $tagType; + public function __construct(?TagType $tagType){ + if($tagType !== null){ + $this->message .= ' Type provided: ' . $tagType->value; } parent::__construct($this->message); diff --git a/lib/Exceptions/InvalidEbookTagTypeException.php b/lib/Exceptions/InvalidEbookTagTypeException.php index 922467b8..70f9f81d 100644 --- a/lib/Exceptions/InvalidEbookTagTypeException.php +++ b/lib/Exceptions/InvalidEbookTagTypeException.php @@ -1,13 +1,15 @@ message .= ' Type provided: ' . $tagType; + public function __construct(?TagType $tagType){ + if($tagType !== null){ + $this->message .= ' Type provided: ' . $tagType->value; } parent::__construct($this->message); diff --git a/lib/Tag.php b/lib/Tag.php index e81f3286..caeba4c5 100644 --- a/lib/Tag.php +++ b/lib/Tag.php @@ -8,7 +8,7 @@ class Tag{ public int $TagId; public string $Name; - public string $Type; + public TagType $Type; protected ?string $_UrlName = null; protected ?string $_Url = null; } diff --git a/lib/TagType.php b/lib/TagType.php new file mode 100644 index 00000000..63b9ffd3 --- /dev/null +++ b/lib/TagType.php @@ -0,0 +1,5 @@ +