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

@ -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;
}