Type tweaks for exceptions and some validation

This commit is contained in:
Alex Cabal 2024-04-25 20:14:34 -05:00
parent 5b3f8f7b77
commit 7eaa400ae3
13 changed files with 49 additions and 37 deletions

View file

@ -84,7 +84,7 @@ class Artist extends Accessor{
$now = new DateTimeImmutable();
$thisYear = intval($now->format('Y'));
$error = new Exceptions\ValidationException();
$error = new Exceptions\InvalidArtistException();
if($this->Name === null || $this->Name == ''){
$error->Add(new Exceptions\ArtistNameRequiredException());

View file

@ -321,10 +321,10 @@ class Artwork extends Accessor{
protected function Validate(?string $imagePath = null, bool $isImageRequired = true): void{
$now = new DateTimeImmutable();
$thisYear = intval($now->format('Y'));
$error = new Exceptions\ValidationException();
$error = new Exceptions\InvalidArtworkException();
if($this->Artist === null){
$error->Add(new Exceptions\InvalidArtworkException());
$error->Add(new Exceptions\InvalidArtistException());
}
try{

View file

@ -27,7 +27,7 @@ class ArtworkTag extends Tag{
// METHODS
// *******
public function Validate(): void{
$error = new Exceptions\ValidationException();
$error = new Exceptions\InvalidArtworkTagException($this->Name);
$this->Name = mb_strtolower(trim($this->Name));
// Collapse spaces into one

View file

@ -0,0 +1,6 @@
<?
namespace Exceptions;
class InvalidArtistException extends ValidationException{
protected $message = 'Artist is invalid.';
}

View file

@ -1,6 +1,6 @@
<?
namespace Exceptions;
class InvalidArtworkException extends AppException{
class InvalidArtworkException extends ValidationException{
protected $message = 'Artwork is invalid.';
}

View file

@ -1,5 +1,12 @@
<?
namespace Exceptions;
class InvalidArtworkTagException extends AppException{
class InvalidArtworkTagException extends ValidationException{
protected $message = 'Artwork tag is invalid.';
public function __construct(?string $tagName){
if($tagName !== null && trim($tagName) != ''){
$this->message = 'Artwork tag ' . $tagName . ' is invalid.';
}
}
}

View file

@ -0,0 +1,6 @@
<?
namespace Exceptions;
class InvalidNewsletterSubscription extends ValidationException{
protected $message = 'Newsletter subscription is invalid.';
}

View file

@ -0,0 +1,6 @@
<?
namespace Exceptions;
class InvalidPollVoteException extends ValidationException{
protected $message = 'Vote is invalid.';
}

View file

@ -31,8 +31,8 @@ class NewsletterSubscription extends Accessor{
// METHODS
// *******
public function Create(): void{
$this->Validate();
public function Create(?string $expectedCaptcha = null, ?string $receivedCaptcha = null): void{
$this->Validate($expectedCaptcha, $receivedCaptcha);
// Do we need to create a user?
try{
@ -111,8 +111,8 @@ class NewsletterSubscription extends Accessor{
', [$this->UserId]);
}
public function Validate(): void{
$error = new Exceptions\ValidationException();
public function Validate(?string $expectedCaptcha = null, ?string $receivedCaptcha = null): void{
$error = new Exceptions\InvalidNewsletterSubscription();
if($this->User === null || $this->User->Email == '' || !filter_var($this->User->Email, FILTER_VALIDATE_EMAIL)){
$error->Add(new Exceptions\InvalidEmailException());
@ -122,6 +122,12 @@ class NewsletterSubscription extends Accessor{
$error->Add(new Exceptions\NewsletterRequiredException());
}
if($expectedCaptcha !== null){
if($expectedCaptcha === '' || mb_strtolower($expectedCaptcha) !== mb_strtolower($receivedCaptcha ?? '')){
$error->Add(new Exceptions\InvalidCaptchaException());
}
}
if($error->HasExceptions){
throw $error;
}

View file

@ -7,7 +7,7 @@ use Safe\DateTimeImmutable;
* @property string $Url
*/
class PollVote extends Accessor{
public int $UserId;
public ?int $UserId = null;
public DateTimeImmutable $Created;
public ?int $PollItemId = null;
protected ?User $_User = null;
@ -33,7 +33,7 @@ class PollVote extends Accessor{
// *******
protected function Validate(): void{
$error = new Exceptions\ValidationException();
$error = new Exceptions\InvalidPollVoteException();
if($this->User === null){
$error->Add(new Exceptions\UserNotFoundException());

View file

@ -38,26 +38,10 @@ try{
$subscription->IsSubscribedToNewsletter = HttpInput::Bool(POST, 'issubscribedtonewsletter') ?? false;
$subscription->IsSubscribedToSummary = HttpInput::Bool(POST, 'issubscribedtosummary') ?? false;
$captcha = HttpInput::Str(SESSION, 'captcha') ?? '';
$expectedCaptcha = HttpInput::Str(SESSION, 'captcha') ?? '';
$receivedCaptcha = HttpInput::Str(POST, 'captcha');
$exception = new Exceptions\ValidationException();
try{
$subscription->Validate();
}
catch(Exceptions\ValidationException $ex){
$exception->Add($ex);
}
if($captcha === '' || mb_strtolower($captcha) !== mb_strtolower(HttpInput::Str(POST, 'captcha') ?? '')){
$exception->Add(new Exceptions\InvalidCaptchaException());
}
if($exception->HasExceptions){
throw $exception;
}
$subscription->Create();
$subscription->Create($expectedCaptcha, $receivedCaptcha);
session_unset();
@ -99,8 +83,7 @@ catch(Exceptions\NewsletterSubscriptionExistsException){
http_response_code(409);
}
}
catch(Exceptions\AppException $ex){
// Validation failed
catch(Exceptions\InvalidNewsletterSubscription $ex){
if($requestType == WEB){
$_SESSION['subscription'] = $subscription;
$_SESSION['exception'] = $ex;

View file

@ -30,8 +30,7 @@ try{
header('Location: ' . $vote->Url);
}
}
catch(Exceptions\AppException $ex){
// Validation failed
catch(Exceptions\InvalidPollVoteException $ex){
if($requestType == WEB){
$_SESSION['vote'] = $vote;
$_SESSION['exception'] = $ex;

View file

@ -33,8 +33,7 @@ try{
header('Location: ' . $session->Url);
}
}
catch(Exceptions\AppException $ex){
// Login failed
catch(Exceptions\InvalidLoginException $ex){
if($requestType == WEB){
$_SESSION['email'] = $email;
$_SESSION['redirect'] = $redirect;