Add type hints to Artwork class members

This commit is contained in:
Alex Cabal 2024-01-08 14:06:25 -06:00
parent 5b6a334dd0
commit 8e37543fa3
5 changed files with 64 additions and 47 deletions

View file

@ -23,38 +23,39 @@ use function Safe\sprintf;
* @property User $Submitter * @property User $Submitter
* @property User $Reviewer * @property User $Reviewer
* @property ?ImageMimeType $MimeType * @property ?ImageMimeType $MimeType
* @property array<ArtworkTag> $_Tags
*/ */
class Artwork extends PropertiesBase{ class Artwork extends PropertiesBase{
public $Name; public ?string $Name;
public $ArtworkId; public ?int $ArtworkId;
public $ArtistId; public ?int $ArtistId;
public $CompletedYear; public ?int $CompletedYear;
public $CompletedYearIsCirca; public ?bool $CompletedYearIsCirca;
public $Created; public ?DateTime $Created;
public $Updated; public ?DateTime $Updated;
public $Status; public ?string $Status;
public $EbookWwwFilesystemPath; public ?string $EbookWwwFilesystemPath;
public $SubmitterUserId; public ?int $SubmitterUserId;
public $ReviewerUserId; public ?int $ReviewerUserId;
public $MuseumUrl; public ?string $MuseumUrl;
public $PublicationYear; public ?int $PublicationYear;
public $PublicationYearPageUrl; public ?string $PublicationYearPageUrl;
public $CopyrightPageUrl; public ?string $CopyrightPageUrl;
public $ArtworkPageUrl; public ?string $ArtworkPageUrl;
public $IsPublishedInUs; public ?bool $IsPublishedInUs;
public $Exception; public ?string $Exception;
protected $_UrlName; protected ?string $_UrlName = null;
protected $_Url; protected ?string $_Url = null;
protected $_AdminUrl;
protected $_Tags = null; protected $_Tags = null;
protected $_Artist = null; protected ?Artist $_Artist = null;
protected $_ImageUrl = null; protected ?string $_ImageUrl = null;
protected $_ThumbUrl = null; protected ?string $_ThumbUrl = null;
protected $_Thumb2xUrl = null; protected ?string $_Thumb2xUrl = null;
protected $_Dimensions = null; protected ?string $_Dimensions = null;
protected $_Ebook = null; protected ?Ebook $_Ebook = null;
protected $_Museum = null; protected ?Museum $_Museum = null;
protected $_Submitter = null; protected ?User $_Submitter = null;
protected ?User $_Reviewer = null;
protected ?ImageMimeType $_MimeType = null; protected ?ImageMimeType $_MimeType = null;
// ******* // *******
@ -97,14 +98,6 @@ class Artwork extends PropertiesBase{
return $this->_Url; return $this->_Url;
} }
protected function GetAdminUrl(): string{
if($this->_AdminUrl === null){
$this->_AdminUrl = '/admin/artworks/' . $this->ArtworkId;
}
return $this->_AdminUrl;
}
/** /**
* @return array<ArtworkTag> * @return array<ArtworkTag>
*/ */
@ -123,7 +116,12 @@ class Artwork extends PropertiesBase{
public function GetMuseum(): ?Museum{ public function GetMuseum(): ?Museum{
if($this->_Museum === null){ if($this->_Museum === null){
$this->_Museum = Museum::GetByUrl($this->MuseumUrl); try{
$this->_Museum = Museum::GetByUrl($this->MuseumUrl);
}
catch(Exceptions\MuseumNotFoundException){
// Pass
}
} }
return $this->_Museum; return $this->_Museum;
@ -178,16 +176,15 @@ class Artwork extends PropertiesBase{
} }
protected function GetDimensions(): string{ protected function GetDimensions(): string{
$this->_Dimensions = '';
try{ try{
list($imageWidth, $imageHeight) = getimagesize(WEB_ROOT . $this->ImageUrl); list($imageWidth, $imageHeight) = getimagesize(WEB_ROOT . $this->ImageUrl);
if($imageWidth && $imageHeight){ if($imageWidth && $imageHeight){
$this->_Dimensions .= $imageWidth . ' × ' . $imageHeight; $this->_Dimensions = $imageWidth . ' × ' . $imageHeight;
} }
} }
catch(Exception){ catch(Exception){
// Image doesn't exist // Image doesn't exist, return blank strin
$this->_Dimensions = '';
} }
return $this->_Dimensions; return $this->_Dimensions;
@ -580,7 +577,11 @@ class Artwork extends PropertiesBase{
/** /**
* @throws \Exceptions\InvalidArtworkException * @throws \Exceptions\InvalidArtworkException
*/ */
public static function GetByUrl(string $artistUrlName, string $artworkUrlName): Artwork{ public static function GetByUrl(?string $artistUrlName, ?string $artworkUrlName): Artwork{
if($artistUrlName === null || $artworkUrlName === null){
throw new Exceptions\ArtworkNotFoundException();
}
$result = Db::Query(' $result = Db::Query('
SELECT Artworks.* SELECT Artworks.*
from Artworks from Artworks

View file

@ -0,0 +1,6 @@
<?
namespace Exceptions;
class MuseumNotFoundException extends AppException{
protected $message = 'We couldnt locate that museum.';
}

View file

@ -5,7 +5,11 @@ class Museum extends PropertiesBase{
public $Name; public $Name;
public $Domain; public $Domain;
public static function GetByUrl(string $url): ?Museum{ public static function GetByUrl(?string $url): Museum{
if($url === null){
throw new Exceptions\MuseumNotFoundException();
}
$result = Db::Query(' $result = Db::Query('
SELECT * SELECT *
from Museums from Museums
@ -13,6 +17,10 @@ class Museum extends PropertiesBase{
limit 1; limit 1;
', [$url], 'Museum'); ', [$url], 'Museum');
return $result[0] ?? null; if($result[0] === null){
throw new Exceptions\MuseumNotFoundException();
}
return $result[0];
} }
} }

View file

@ -88,7 +88,7 @@ if($perPage !== COVER_ARTWORK_PER_PAGE){
</span> </span>
</label> </label>
<label class="search">Keywords <label class="search">Keywords
<input type="search" name="query" value="<?= Formatter::ToPlainText($query ?? '') ?>"/> <input type="search" name="query" value="<?= Formatter::ToPlainText($query) ?>"/>
</label> </label>
<label> <label>
<span>Sort</span> <span>Sort</span>

View file

@ -115,7 +115,9 @@ catch(Exceptions\ArtworkNotFoundException){
Template::Emit404(); Template::Emit404();
} }
catch(Exceptions\AppException $exception){ catch(Exceptions\AppException $exception){
$_SESSION['artwork'] = $artwork ?? null; $artwork = $artwork ?? null;
$_SESSION['artwork'] = $artwork;
$_SESSION['exception'] = $exception; $_SESSION['exception'] = $exception;
http_response_code(303); http_response_code(303);