Change tag type from string to enum (TagType)

This commit is contained in:
Mike Colagrosso 2024-10-03 13:39:33 -06:00 committed by Alex Cabal
parent 59eee3cc57
commit 5b1bb2a9f0
6 changed files with 26 additions and 16 deletions

View file

@ -4,7 +4,7 @@ use function Safe\preg_replace;
class ArtworkTag extends Tag{ class ArtworkTag extends Tag{
public function __construct(){ public function __construct(){
$this->Type = 'artwork'; $this->Type = TagType::Artwork;
} }
// ******* // *******
@ -45,7 +45,7 @@ class ArtworkTag extends Tag{
$error->Add(new Exceptions\InvalidArtworkTagNameException()); $error->Add(new Exceptions\InvalidArtworkTagNameException());
} }
if($this->Type != 'artwork'){ if($this->Type != TagType::Artwork){
$error->Add(new Exceptions\InvalidArtworkTagTypeException($this->Type)); $error->Add(new Exceptions\InvalidArtworkTagTypeException($this->Type));
} }
@ -76,8 +76,8 @@ class ArtworkTag extends Tag{
SELECT * SELECT *
from Tags from Tags
where Name = ? where Name = ?
and Type = "artwork" and Type = ?
', [$artworkTag->Name], ArtworkTag::class); ', [$artworkTag->Name, TagType::Artwork], ArtworkTag::class);
if(isset($result[0])){ if(isset($result[0])){
return $result[0]; return $result[0];

View file

@ -1,7 +1,7 @@
<? <?
class EbookTag extends Tag{ class EbookTag extends Tag{
public function __construct(){ public function __construct(){
$this->Type = 'ebook'; $this->Type = TagType::Ebook;
} }
// ******* // *******
@ -37,7 +37,7 @@ class EbookTag extends Tag{
$error->Add(new Exceptions\StringTooLongException('Ebook tag: '. $this->Name)); $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)); $error->Add(new Exceptions\InvalidEbookTagTypeException($this->Type));
} }
@ -69,7 +69,8 @@ class EbookTag extends Tag{
SELECT * SELECT *
from Tags from Tags
where Name = ? where Name = ?
', [$name], EbookTag::class); and Type = ?
', [$name, TagType::Ebook], EbookTag::class);
if(isset($result[0])){ if(isset($result[0])){
return $result[0]; return $result[0];

View file

@ -1,13 +1,15 @@
<? <?
namespace Exceptions; namespace Exceptions;
use \TagType;
class InvalidArtworkTagTypeException extends AppException{ class InvalidArtworkTagTypeException extends AppException{
/** @var string $message */ /** @var string $message */
protected $message = 'Type should be `artwork`.'; protected $message = 'Type should be `TagType::Artwork`.';
public function __construct(?string $tagType){ public function __construct(?TagType $tagType){
if($tagType !== null && trim($tagType) != ''){ if($tagType !== null){
$this->message .= ' Type provided: ' . $tagType; $this->message .= ' Type provided: ' . $tagType->value;
} }
parent::__construct($this->message); parent::__construct($this->message);

View file

@ -1,13 +1,15 @@
<? <?
namespace Exceptions; namespace Exceptions;
use \TagType;
class InvalidEbookTagTypeException extends AppException{ class InvalidEbookTagTypeException extends AppException{
/** @var string $message */ /** @var string $message */
protected $message = 'Type should be `ebook`.'; protected $message = 'Type should be `TagType::Ebook`.';
public function __construct(?string $tagType){ public function __construct(?TagType $tagType){
if($tagType !== null && trim($tagType) != ''){ if($tagType !== null){
$this->message .= ' Type provided: ' . $tagType; $this->message .= ' Type provided: ' . $tagType->value;
} }
parent::__construct($this->message); parent::__construct($this->message);

View file

@ -8,7 +8,7 @@ class Tag{
public int $TagId; public int $TagId;
public string $Name; public string $Name;
public string $Type; public TagType $Type;
protected ?string $_UrlName = null; protected ?string $_UrlName = null;
protected ?string $_Url = null; protected ?string $_Url = null;
} }

5
lib/TagType.php Normal file
View file

@ -0,0 +1,5 @@
<?
enum TagType: string{
case Artwork = 'artwork';
case Ebook = 'ebook';
}