Refactor HttpInput::Str and rename some exceptions for consistency

This commit is contained in:
Alex Cabal 2024-01-18 11:15:59 -06:00
parent 2b5f4f55a2
commit ca3fc6dbfd
54 changed files with 163 additions and 159 deletions

View file

@ -85,7 +85,7 @@ class Artist extends PropertiesBase{
public static function Get(?int $artistId): Artist{
if($artistId === null){
throw new Exceptions\InvalidArtistException();
throw new Exceptions\ArtistNotFoundException();
}
$result = Db::Query('
@ -95,7 +95,7 @@ class Artist extends PropertiesBase{
', [$artistId], 'Artist');
if(sizeof($result) == 0){
throw new Exceptions\InvalidArtistException();
throw new Exceptions\ArtistNotFoundException();
}
return $result[0];

View file

@ -135,7 +135,7 @@ class Artwork extends PropertiesBase{
try{
$this->_Submitter = User::Get($this->SubmitterUserId);
}
catch(Exceptions\InvalidUserException){
catch(Exceptions\UserNotFoundException){
// Return null
}
}
@ -148,7 +148,7 @@ class Artwork extends PropertiesBase{
try{
$this->_Reviewer = User::Get($this->ReviewerUserId);
}
catch(Exceptions\InvalidUserException){
catch(Exceptions\UserNotFoundException){
// Return null
}
}
@ -486,7 +486,7 @@ class Artwork extends PropertiesBase{
// But we do a basic check that the string includes one _. It might not include a dash, for example anonymous_poetry
if($this->EbookWwwFilesystemPath !== null){
if(mb_stripos($this->EbookWwwFilesystemPath, '_') === false){
$error->Add(new Exceptions\InvalidEbookException('Invalid ebook. Expected file system slug like “c-s-lewis_poetry”.'));
$error->Add(new Exceptions\EbookNotFoundException('Invalid ebook. Expected file system slug like “c-s-lewis_poetry”.'));
}
}
@ -900,23 +900,23 @@ class Artwork extends PropertiesBase{
$artwork = new Artwork();
$artwork->Artist = new Artist();
$artwork->Artist->Name = HttpInput::Str(POST, 'artist-name', false);
$artwork->Artist->Name = HttpInput::Str(POST, 'artist-name');
$artwork->Artist->DeathYear = HttpInput::Int(POST, 'artist-year-of-death');
$artwork->Name = HttpInput::Str(POST, 'artwork-name', false);
$artwork->Name = HttpInput::Str(POST, 'artwork-name');
$artwork->CompletedYear = HttpInput::Int(POST, 'artwork-year');
$artwork->CompletedYearIsCirca = HttpInput::Bool(POST, 'artwork-year-is-circa', false) ?? false;
$artwork->Tags = HttpInput::Str(POST, 'artwork-tags', false) ?? [];
$artwork->Status = HttpInput::Str(POST, 'artwork-status', false) ?? ArtworkStatus::Unverified;
$artwork->EbookWwwFilesystemPath = HttpInput::Str(POST, 'artwork-ebook-www-filesystem-path', false);
$artwork->IsPublishedInUs = HttpInput::Bool(POST, 'artwork-is-published-in-us', false);
$artwork->CompletedYearIsCirca = HttpInput::Bool(POST, 'artwork-year-is-circa') ?? false;
$artwork->Tags = HttpInput::Str(POST, 'artwork-tags') ?? [];
$artwork->Status = HttpInput::Str(POST, 'artwork-status') ?? ArtworkStatus::Unverified;
$artwork->EbookWwwFilesystemPath = HttpInput::Str(POST, 'artwork-ebook-www-filesystem-path');
$artwork->IsPublishedInUs = HttpInput::Bool(POST, 'artwork-is-published-in-us') ?? false;
$artwork->PublicationYear = HttpInput::Int(POST, 'artwork-publication-year');
$artwork->PublicationYearPageUrl = HttpInput::Str(POST, 'artwork-publication-year-page-url', false);
$artwork->CopyrightPageUrl = HttpInput::Str(POST, 'artwork-copyright-page-url', false);
$artwork->ArtworkPageUrl = HttpInput::Str(POST, 'artwork-artwork-page-url', false);
$artwork->MuseumUrl = HttpInput::Str(POST, 'artwork-museum-url', false);
$artwork->Exception = HttpInput::Str(POST, 'artwork-exception', false);
$artwork->Notes = HttpInput::Str(POST, 'artwork-notes', false);
$artwork->PublicationYearPageUrl = HttpInput::Str(POST, 'artwork-publication-year-page-url');
$artwork->CopyrightPageUrl = HttpInput::Str(POST, 'artwork-copyright-page-url');
$artwork->ArtworkPageUrl = HttpInput::Str(POST, 'artwork-artwork-page-url');
$artwork->MuseumUrl = HttpInput::Str(POST, 'artwork-museum-url');
$artwork->Exception = HttpInput::Str(POST, 'artwork-exception');
$artwork->Notes = HttpInput::Str(POST, 'artwork-notes');
return $artwork;
}

View file

@ -91,20 +91,20 @@ class Ebook{
}
catch(Exception){
// We may get an exception from preg_replace if the passed repo wwwFilesystemPath contains invalid UTF-8 characters, whichis a common injection attack vector
throw new Exceptions\InvalidEbookException('Invalid repo filesystem path: ' . $this->RepoFilesystemPath);
throw new Exceptions\EbookNotFoundException('Invalid repo filesystem path: ' . $this->RepoFilesystemPath);
}
}
if(!is_dir($wwwFilesystemPath)){
throw new Exceptions\InvalidEbookException('Invalid www filesystem path: ' . $wwwFilesystemPath);
throw new Exceptions\EbookNotFoundException('Invalid www filesystem path: ' . $wwwFilesystemPath);
}
if(!is_dir($this->RepoFilesystemPath)){
throw new Exceptions\InvalidEbookException('Invalid repo filesystem path: ' . $this->RepoFilesystemPath);
throw new Exceptions\EbookNotFoundException('Invalid repo filesystem path: ' . $this->RepoFilesystemPath);
}
if(!is_file($wwwFilesystemPath . '/content.opf')){
throw new Exceptions\InvalidEbookException('Invalid content.opf file: ' . $wwwFilesystemPath . '/content.opf');
throw new Exceptions\EbookNotFoundException('Invalid content.opf file: ' . $wwwFilesystemPath . '/content.opf');
}
$this->WwwFilesystemPath = $wwwFilesystemPath;

View file

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

View file

@ -0,0 +1,5 @@
<?
namespace Exceptions;
class AuthorNotFoundException extends AppException{
}

View file

@ -0,0 +1,5 @@
<?
namespace Exceptions;
class CollectionNotFoundException extends AppException{
}

View file

@ -0,0 +1,5 @@
<?
namespace Exceptions;
class EbookNotFoundException extends AppException{
}

View file

@ -1,5 +0,0 @@
<?
namespace Exceptions;
class InvalidAuthorException extends AppException{
}

View file

@ -1,5 +0,0 @@
<?
namespace Exceptions;
class InvalidCollectionException extends AppException{
}

View file

@ -1,5 +0,0 @@
<?
namespace Exceptions;
class InvalidEbookException extends AppException{
}

View file

@ -1,5 +0,0 @@
<?
namespace Exceptions;
class InvalidSessionException extends AppException{
}

View file

@ -1,6 +1,6 @@
<?
namespace Exceptions;
class InvalidNewsletterSubscriptionException extends AppException{
class NewsletterSubscriptionNotFoundException extends AppException{
protected $message = 'We couldnt find you in our newsletter subscribers list.';
}

View file

@ -1,6 +1,6 @@
<?
namespace Exceptions;
class InvalidPollItemException extends AppException{
class PollItemNotFoundException extends AppException{
protected $message = 'We couldnt locate that poll item.';
}

View file

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

View file

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

View file

@ -0,0 +1,5 @@
<?
namespace Exceptions;
class SessionNotFoundException extends AppException{
}

View file

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

View file

@ -50,30 +50,30 @@ class HttpInput{
return preg_match('/\btext\/html\b/ius', $_SERVER['HTTP_ACCEPT'] ?? '') ? WEB : REST;
}
public static function Str(string $type, string $variable, bool $allowEmptyString = true, string $default = null): ?string{
$var = self::GetHttpVar($variable, HTTP_VAR_STR, $type, $default);
public static function Str(string $type, string $variable, $allowEmptyString = false): ?string{
$var = self::GetHttpVar($variable, HTTP_VAR_STR, $type);
if(is_array($var)){
return $default;
return null;
}
if(!$allowEmptyString && $var === ''){
if(!$allowEmptyString && $var == ''){
return null;
}
return $var;
}
public static function Int(string $type, string $variable, int $default = null): ?int{
return self::GetHttpVar($variable, HTTP_VAR_INT, $type, $default);
public static function Int(string $type, string $variable): ?int{
return self::GetHttpVar($variable, HTTP_VAR_INT, $type);
}
public static function Bool(string $type, string $variable, bool $default = null): ?bool{
return self::GetHttpVar($variable, HTTP_VAR_BOOL, $type, $default);
public static function Bool(string $type, string $variable): ?bool{
return self::GetHttpVar($variable, HTTP_VAR_BOOL, $type);
}
public static function Dec(string $type, string $variable, float $default = null): ?float{
return self::GetHttpVar($variable, HTTP_VAR_DEC, $type, $default);
public static function Dec(string $type, string $variable): ?float{
return self::GetHttpVar($variable, HTTP_VAR_DEC, $type);
}
/**
@ -81,11 +81,11 @@ class HttpInput{
* @param array<mixed> $default
* @return array<string>
*/
public static function GetArray(string $variable, array $default = null): ?array{
return self::GetHttpVar($variable, HTTP_VAR_ARRAY, GET, $default);
public static function GetArray(string $variable): ?array{
return self::GetHttpVar($variable, HTTP_VAR_ARRAY, GET);
}
private static function GetHttpVar(string $variable, int $type, string $set, mixed $default): mixed{
private static function GetHttpVar(string $variable, int $type, string $set): mixed{
$vars = [];
switch($set){
@ -110,7 +110,7 @@ class HttpInput{
}
elseif($type !== HTTP_VAR_ARRAY && is_array($vars[$variable])){
// We asked for not an array, but we got an array
return $default;
return null;
}
else{
$var = trim($vars[$variable]);
@ -126,7 +126,7 @@ class HttpInput{
return intval($var);
}
catch(Exception){
return $default;
return null;
}
}
break;
@ -143,13 +143,13 @@ class HttpInput{
return floatval($var);
}
catch(Exception){
return $default;
return null;
}
}
break;
}
}
return $default;
return null;
}
}

View file

@ -38,7 +38,7 @@ class NewsletterSubscription extends PropertiesBase{
try{
$this->User = User::GetByEmail($this->User->Email);
}
catch(Exceptions\InvalidUserException){
catch(Exceptions\UserNotFoundException){
// User doesn't exist, create the user
$this->User->Create();
}
@ -132,7 +132,11 @@ class NewsletterSubscription extends PropertiesBase{
// ORM METHODS
// ***********
public static function Get(string $uuid): NewsletterSubscription{
public static function Get(?string $uuid): NewsletterSubscription{
if($uuid === null){
throw new Exceptions\NewsletterSubscriptionNotFoundException();
}
$result = Db::Query('
SELECT ns.*
from NewsletterSubscriptions ns
@ -141,7 +145,7 @@ class NewsletterSubscription extends PropertiesBase{
', [$uuid], 'NewsletterSubscription');
if(sizeof($result) == 0){
throw new Exceptions\InvalidNewsletterSubscriptionException();
throw new Exceptions\NewsletterSubscriptionNotFoundException();
}
return $result[0];

View file

@ -39,7 +39,7 @@ class Payment extends PropertiesBase{
where UserId = ?
', [$this->User->Name, $this->User->UserId]);
}
catch(Exceptions\InvalidUserException){
catch(Exceptions\UserNotFoundException){
// User doesn't exist, create it now
$this->User->Create();
}

View file

@ -100,7 +100,7 @@ class Poll extends PropertiesBase{
public static function Get(?int $pollId): Poll{
if($pollId === null){
throw new Exceptions\InvalidPollException();
throw new Exceptions\PollNotFoundException();
}
$result = Db::Query('
@ -110,7 +110,7 @@ class Poll extends PropertiesBase{
', [$pollId], 'Poll');
if(sizeof($result) == 0){
throw new Exceptions\InvalidPollException();
throw new Exceptions\PollNotFoundException();
}
return $result[0];
@ -118,7 +118,7 @@ class Poll extends PropertiesBase{
public static function GetByUrlName(?string $urlName): Poll{
if($urlName === null){
throw new Exceptions\InvalidPollException();
throw new Exceptions\PollNotFoundException();
}
$result = Db::Query('
@ -128,7 +128,7 @@ class Poll extends PropertiesBase{
', [$urlName], 'Poll');
if(sizeof($result) == 0){
throw new Exceptions\InvalidPollException();
throw new Exceptions\PollNotFoundException();
}
return $result[0];

View file

@ -36,7 +36,7 @@ class PollItem extends PropertiesBase{
public static function Get(?int $pollItemId): PollItem{
if($pollItemId === null ){
throw new Exceptions\InvalidPollItemException();
throw new Exceptions\PollItemNotFoundException();
}
$result = Db::Query('
@ -46,7 +46,7 @@ class PollItem extends PropertiesBase{
', [$pollItemId], 'PollItem');
if(sizeof($result) == 0){
throw new Exceptions\InvalidPollItemException();
throw new Exceptions\PollItemNotFoundException();
}
return $result[0];

View file

@ -36,7 +36,7 @@ class PollVote extends PropertiesBase{
$error = new Exceptions\ValidationException();
if($this->User === null){
$error->Add(new Exceptions\InvalidUserException());
$error->Add(new Exceptions\UserNotFoundException());
}
if($this->PollItemId === null){
@ -44,11 +44,11 @@ class PollVote extends PropertiesBase{
}
else{
if($this->PollItem === null){
$error->Add(new Exceptions\InvalidPollException());
$error->Add(new Exceptions\PollNotFoundException());
}
else{
if($this->PollItem->Poll === null){
$error->Add(new Exceptions\InvalidPollException());
$error->Add(new Exceptions\PollNotFoundException());
}
else{
if(!$this->PollItem->Poll->IsActive()){
@ -67,7 +67,7 @@ class PollVote extends PropertiesBase{
$vote = PollVote::Get($this->PollItem->Poll->UrlName, $this->UserId);
$error->Add(new Exceptions\PollVoteExistsException($vote));
}
catch(Exceptions\InvalidPollVoteException){
catch(Exceptions\PollVoteNotFoundException){
// User hasn't voted yet, carry on
}
@ -87,7 +87,7 @@ class PollVote extends PropertiesBase{
$this->User = User::GetByEmail($email);
$this->UserId = $this->User->UserId;
}
catch(Exceptions\InvalidUserException){
catch(Exceptions\UserNotFoundException){
// Can't validate patron email - do nothing for now,
// this will be caught later when we validate the vote during creation.
// Save the email in the User object in case we want it later,
@ -109,7 +109,7 @@ class PollVote extends PropertiesBase{
public static function Get(?string $pollUrlName, ?int $userId): PollVote{
if($pollUrlName === null || $userId === null){
throw new Exceptions\InvalidPollVoteException();
throw new Exceptions\PollVoteNotFoundException();
}
$result = Db::Query('
@ -124,7 +124,7 @@ class PollVote extends PropertiesBase{
', [$pollUrlName, $userId], 'PollVote');
if(sizeof($result) == 0){
throw new Exceptions\InvalidPollVoteException();
throw new Exceptions\PollVoteNotFoundException();
}
return $result[0];

View file

@ -64,7 +64,7 @@ class Session extends PropertiesBase{
self::SetSessionCookie($this->SessionId);
}
catch(Exceptions\InvalidUserException){
catch(Exceptions\UserNotFoundException){
throw new InvalidLoginException();
}
}
@ -95,7 +95,7 @@ class Session extends PropertiesBase{
public static function Get(?string $sessionId): Session{
if($sessionId === null){
throw new Exceptions\InvalidSessionException();
throw new Exceptions\SessionNotFoundException();
}
$result = Db::Query('
@ -105,7 +105,7 @@ class Session extends PropertiesBase{
', [$sessionId], 'Session');
if(sizeof($result) == 0){
throw new Exceptions\InvalidSessionException();
throw new Exceptions\SessionNotFoundException();
}
return $result[0];

View file

@ -115,7 +115,7 @@ class User extends PropertiesBase{
public static function Get(?int $userId): User{
if($userId === null){
throw new Exceptions\InvalidUserException();
throw new Exceptions\UserNotFoundException();
}
$result = Db::Query('
@ -125,7 +125,7 @@ class User extends PropertiesBase{
', [$userId], 'User');
if(sizeof($result) == 0){
throw new Exceptions\InvalidUserException();
throw new Exceptions\UserNotFoundException();
}
return $result[0];
@ -133,7 +133,7 @@ class User extends PropertiesBase{
public static function GetByEmail(?string $email): User{
if($email === null){
throw new Exceptions\InvalidUserException();
throw new Exceptions\UserNotFoundException();
}
$result = Db::Query('
@ -143,7 +143,7 @@ class User extends PropertiesBase{
', [$email], 'User');
if(sizeof($result) == 0){
throw new Exceptions\InvalidUserException();
throw new Exceptions\UserNotFoundException();
}
return $result[0];
@ -154,7 +154,7 @@ class User extends PropertiesBase{
// Emails without that row may only be signed up for the newsletter and thus are not "registered" users
// The identifier is either an email or a UUID (api key)
if($identifier === null){
throw new Exceptions\InvalidUserException();
throw new Exceptions\UserNotFoundException();
}
$result = Db::Query('
@ -166,7 +166,7 @@ class User extends PropertiesBase{
', [$identifier, $identifier], 'User');
if(sizeof($result) == 0){
throw new Exceptions\InvalidUserException();
throw new Exceptions\UserNotFoundException();
}
if($result[0]->PasswordHash !== null && $password === null){
@ -175,7 +175,7 @@ class User extends PropertiesBase{
}
if($result[0]->PasswordHash !== null && !password_verify($password ?? '', $result[0]->PasswordHash)){
throw new Exceptions\InvalidUserException();
throw new Exceptions\UserNotFoundException();
}
return $result[0];