Skip to content

Commit

Permalink
Refactor HttpInput::Str and rename some exceptions for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
acabal committed Jan 18, 2024
1 parent 2b5f4f5 commit ca3fc6d
Show file tree
Hide file tree
Showing 54 changed files with 163 additions and 159 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ Before submitting design contributions, please discuss them with the Standard Eb
- Include in-use ebook slug as a search parameter when searching for artwork by keyword.
- Artwork searching/filtering should be done in pure SQL, no after-sql filtering in PHP.
- Artwork searching/filtering should be done in pure SQL, no after-SQL filtering in PHP.
- Allow listing artwork by artist by visiting `/artworks/<artist-name>`, and link instances of artist name to that URL.
Expand Down
4 changes: 2 additions & 2 deletions lib/Artist.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function Validate(): void{

public static function Get(?int $artistId): Artist{
if($artistId === null){
throw new Exceptions\InvalidArtistException();
throw new Exceptions\ArtistNotFoundException();
}

$result = Db::Query('
Expand All @@ -95,7 +95,7 @@ public static function Get(?int $artistId): Artist{
', [$artistId], 'Artist');

if(sizeof($result) == 0){
throw new Exceptions\InvalidArtistException();
throw new Exceptions\ArtistNotFoundException();
}

return $result[0];
Expand Down
32 changes: 16 additions & 16 deletions lib/Artwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected function GetSubmitter(): ?User{
try{
$this->_Submitter = User::Get($this->SubmitterUserId);
}
catch(Exceptions\InvalidUserException){
catch(Exceptions\UserNotFoundException){
// Return null
}
}
Expand All @@ -148,7 +148,7 @@ protected function GetReviewer(): ?User{
try{
$this->_Reviewer = User::Get($this->ReviewerUserId);
}
catch(Exceptions\InvalidUserException){
catch(Exceptions\UserNotFoundException){
// Return null
}
}
Expand Down Expand Up @@ -486,7 +486,7 @@ protected function Validate(?string $imagePath = null, bool $isImageRequired = t
// 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”.'));
}
}

Expand Down Expand Up @@ -900,23 +900,23 @@ public static function FromHttpPost(): Artwork{
$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;
}
Expand Down
8 changes: 4 additions & 4 deletions lib/Ebook.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,20 @@ public function __construct(?string $wwwFilesystemPath = null){
}
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?
namespace Exceptions;

class InvalidArtistException extends AppException{
class ArtistNotFoundException extends AppException{
protected $message = 'We couldn’t locate that artist.';
}
5 changes: 5 additions & 0 deletions lib/Exceptions/AuthorNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?
namespace Exceptions;

class AuthorNotFoundException extends AppException{
}
5 changes: 5 additions & 0 deletions lib/Exceptions/CollectionNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?
namespace Exceptions;

class CollectionNotFoundException extends AppException{
}
5 changes: 5 additions & 0 deletions lib/Exceptions/EbookNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?
namespace Exceptions;

class EbookNotFoundException extends AppException{
}
5 changes: 0 additions & 5 deletions lib/Exceptions/InvalidAuthorException.php

This file was deleted.

5 changes: 0 additions & 5 deletions lib/Exceptions/InvalidCollectionException.php

This file was deleted.

5 changes: 0 additions & 5 deletions lib/Exceptions/InvalidEbookException.php

This file was deleted.

5 changes: 0 additions & 5 deletions lib/Exceptions/InvalidSessionException.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?
namespace Exceptions;

class InvalidNewsletterSubscriptionException extends AppException{
class NewsletterSubscriptionNotFoundException extends AppException{
protected $message = 'We couldn’t find you in our newsletter subscribers list.';
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?
namespace Exceptions;

class InvalidPollItemException extends AppException{
class PollItemNotFoundException extends AppException{
protected $message = 'We couldn’t locate that poll item.';
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?
namespace Exceptions;

class InvalidPollException extends AppException{
class PollNotFoundException extends AppException{
protected $message = 'We couldn’t locate that poll.';
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?
namespace Exceptions;

class InvalidPollVoteException extends AppException{
class PollVoteNotFoundException extends AppException{
protected $message = 'We couldn’t locate that vote.';
}
5 changes: 5 additions & 0 deletions lib/Exceptions/SessionNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?
namespace Exceptions;

class SessionNotFoundException extends AppException{
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?
namespace Exceptions;

class InvalidUserException extends AppException{
class UserNotFoundException extends AppException{
protected $message = 'We couldn’t locate that user.';
}
34 changes: 17 additions & 17 deletions lib/HttpInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,42 +50,42 @@ public static function RequestType(): int{
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);
}

/**
* @param string $variable
* @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){
Expand All @@ -110,7 +110,7 @@ private static function GetHttpVar(string $variable, int $type, string $set, mix
}
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]);
Expand All @@ -126,7 +126,7 @@ private static function GetHttpVar(string $variable, int $type, string $set, mix
return intval($var);
}
catch(Exception){
return $default;
return null;
}
}
break;
Expand All @@ -143,13 +143,13 @@ private static function GetHttpVar(string $variable, int $type, string $set, mix
return floatval($var);
}
catch(Exception){
return $default;
return null;
}
}
break;
}
}

return $default;
return null;
}
}
10 changes: 7 additions & 3 deletions lib/NewsletterSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function Create(): void{
try{
$this->User = User::GetByEmail($this->User->Email);
}
catch(Exceptions\InvalidUserException){
catch(Exceptions\UserNotFoundException){
// User doesn't exist, create the user
$this->User->Create();
}
Expand Down Expand Up @@ -132,7 +132,11 @@ public function Validate(): void{
// 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
Expand All @@ -141,7 +145,7 @@ public static function Get(string $uuid): NewsletterSubscription{
', [$uuid], 'NewsletterSubscription');

if(sizeof($result) == 0){
throw new Exceptions\InvalidNewsletterSubscriptionException();
throw new Exceptions\NewsletterSubscriptionNotFoundException();
}

return $result[0];
Expand Down
2 changes: 1 addition & 1 deletion lib/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function Create(): void{
where UserId = ?
', [$this->User->Name, $this->User->UserId]);
}
catch(Exceptions\InvalidUserException){
catch(Exceptions\UserNotFoundException){
// User doesn't exist, create it now
$this->User->Create();
}
Expand Down
8 changes: 4 additions & 4 deletions lib/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function IsActive(): bool{

public static function Get(?int $pollId): Poll{
if($pollId === null){
throw new Exceptions\InvalidPollException();
throw new Exceptions\PollNotFoundException();
}

$result = Db::Query('
Expand All @@ -110,15 +110,15 @@ public static function Get(?int $pollId): Poll{
', [$pollId], 'Poll');

if(sizeof($result) == 0){
throw new Exceptions\InvalidPollException();
throw new Exceptions\PollNotFoundException();
}

return $result[0];
}

public static function GetByUrlName(?string $urlName): Poll{
if($urlName === null){
throw new Exceptions\InvalidPollException();
throw new Exceptions\PollNotFoundException();
}

$result = Db::Query('
Expand All @@ -128,7 +128,7 @@ public static function GetByUrlName(?string $urlName): Poll{
', [$urlName], 'Poll');

if(sizeof($result) == 0){
throw new Exceptions\InvalidPollException();
throw new Exceptions\PollNotFoundException();
}

return $result[0];
Expand Down
4 changes: 2 additions & 2 deletions lib/PollItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected function GetVoteCount(): int{

public static function Get(?int $pollItemId): PollItem{
if($pollItemId === null ){
throw new Exceptions\InvalidPollItemException();
throw new Exceptions\PollItemNotFoundException();
}

$result = Db::Query('
Expand All @@ -46,7 +46,7 @@ public static function Get(?int $pollItemId): PollItem{
', [$pollItemId], 'PollItem');

if(sizeof($result) == 0){
throw new Exceptions\InvalidPollItemException();
throw new Exceptions\PollItemNotFoundException();
}

return $result[0];
Expand Down
Loading

0 comments on commit ca3fc6d

Please sign in to comment.