Handle nullable and non-nullable validation consistently

This commit is contained in:
Mike Colagrosso 2024-10-04 23:13:08 -06:00 committed by Alex Cabal
parent 783a41a1ba
commit 0eaf2e8e47
8 changed files with 269 additions and 111 deletions

View file

@ -48,15 +48,15 @@ class Ebook{
use Traits\Accessor; use Traits\Accessor;
public ?int $EbookId = null; public ?int $EbookId = null;
public string $Identifier;
public string $WwwFilesystemPath; public string $WwwFilesystemPath;
public string $RepoFilesystemPath; public string $RepoFilesystemPath;
public ?string $KindleCoverUrl = null; public ?string $KindleCoverUrl = null;
public string $EpubUrl; public ?string $EpubUrl = null;
public string $AdvancedEpubUrl; public ?string $AdvancedEpubUrl = null;
public string $KepubUrl; public ?string $KepubUrl = null;
public string $Azw3Url; public ?string $Azw3Url = null;
public string $Identifier; public ?string $DistCoverUrl = null;
public string $DistCoverUrl;
public ?string $Title = null; public ?string $Title = null;
public ?string $FullTitle = null; public ?string $FullTitle = null;
public ?string $AlternateTitle = null; public ?string $AlternateTitle = null;
@ -944,196 +944,305 @@ class Ebook{
$error = new Exceptions\ValidationException(); $error = new Exceptions\ValidationException();
$this->Identifier = trim($this->Identifier ?? ''); if(isset($this->Identifier)){
if($this->Identifier == ''){ $this->Identifier = trim($this->Identifier);
if($this->Identifier == ''){
$error->Add(new Exceptions\EbookIdentifierRequiredException());
}
if(strlen($this->Identifier) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook Identifier'));
}
}
else{
$error->Add(new Exceptions\EbookIdentifierRequiredException()); $error->Add(new Exceptions\EbookIdentifierRequiredException());
} }
if(strlen($this->Identifier) > EBOOKS_MAX_LONG_STRING_LENGTH){ if(isset($this->WwwFilesystemPath)){
$error->Add(new Exceptions\StringTooLongException('Ebook Identifier')); $this->WwwFilesystemPath = trim($this->WwwFilesystemPath);
if($this->WwwFilesystemPath == ''){
$error->Add(new Exceptions\EbookWwwFilesystemPathRequiredException());
}
if(strlen($this->WwwFilesystemPath) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook WwwFilesystemPath'));
}
if(!is_readable($this->WwwFilesystemPath)){
$error->Add(new Exceptions\InvalidEbookWwwFilesystemPathException($this->WwwFilesystemPath));
}
}
else{
$error->Add(new Exceptions\EbookWwwFilesystemPathRequiredException());
} }
$this->WwwFilesystemPath = trim($this->WwwFilesystemPath ?? ''); if(isset($this->RepoFilesystemPath)){
if(!is_readable($this->WwwFilesystemPath)){ $this->RepoFilesystemPath = trim($this->RepoFilesystemPath);
$error->Add(new Exceptions\InvalidEbookWwwFilesystemPathException($this->WwwFilesystemPath));
if($this->RepoFilesystemPath == ''){
$error->Add(new Exceptions\EbookRepoFilesystemPathRequiredException());
}
if(strlen($this->RepoFilesystemPath) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook RepoFilesystemPath'));
}
if(!is_readable($this->RepoFilesystemPath)){
$error->Add(new Exceptions\InvalidEbookRepoFilesystemPathException($this->RepoFilesystemPath));
}
}
else{
$error->Add(new Exceptions\EbookRepoFilesystemPathRequiredException());
} }
if(strlen($this->WwwFilesystemPath) > EBOOKS_MAX_LONG_STRING_LENGTH){ $this->KindleCoverUrl = trim($this->KindleCoverUrl ?? '');
$error->Add(new Exceptions\StringTooLongException('Ebook WwwFilesystemPath')); if($this->KindleCoverUrl == ''){
} $this->KindleCoverUrl = null;
$this->RepoFilesystemPath = trim($this->RepoFilesystemPath ?? '');
if(!is_readable($this->RepoFilesystemPath)){
$error->Add(new Exceptions\InvalidEbookRepoFilesystemPathException($this->RepoFilesystemPath));
}
if(strlen($this->RepoFilesystemPath) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook RepoFilesystemPath'));
} }
if(isset($this->KindleCoverUrl)){ if(isset($this->KindleCoverUrl)){
$this->KindleCoverUrl = trim($this->KindleCoverUrl); if(!preg_match('|/*_EBOK_portrait.jpg|ius', $this->KindleCoverUrl)){
$error->Add(new Exceptions\InvalidEbookKindleCoverUrlException('Invalid Ebook KindleCoverUrl: ' . $this->KindleCoverUrl));
}
if(strlen($this->KindleCoverUrl) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook KindleCoverUrl'));
}
} }
if(isset($this->KindleCoverUrl) && !preg_match('|/*_EBOK_portrait.jpg|ius', $this->KindleCoverUrl)){ $this->EpubUrl = trim($this->EpubUrl ?? '');
$error->Add(new Exceptions\InvalidEbookKindleCoverUrlException('Invalid Ebook KindleCoverUrl: ' . $this->KindleCoverUrl)); if($this->EpubUrl == ''){
$this->EpubUrl = null;
}
if(isset($this->KindleCoverUrl) && strlen($this->KindleCoverUrl) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook KindleCoverUrl'));
} }
if(isset($this->EpubUrl)){ if(isset($this->EpubUrl)){
$this->EpubUrl = trim($this->EpubUrl); if(!preg_match('|/*.epub|ius', $this->EpubUrl)){
$error->Add(new Exceptions\InvalidEbookEpubUrlException('Invalid Ebook EpubUrl: ' . $this->EpubUrl));
}
if(strlen($this->EpubUrl) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook EpubUrl'));
}
} }
if(isset($this->EpubUrl) && !preg_match('|/*.epub|ius', $this->EpubUrl)){ $this->AdvancedEpubUrl = trim($this->AdvancedEpubUrl ?? '');
$error->Add(new Exceptions\InvalidEbookEpubUrlException('Invalid Ebook EpubUrl: ' . $this->EpubUrl)); if($this->AdvancedEpubUrl == ''){
$this->AdvancedEpubUrl = null;
}
if(isset($this->EpubUrl) && strlen($this->EpubUrl) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook EpubUrl'));
} }
if(isset($this->AdvancedEpubUrl)){ if(isset($this->AdvancedEpubUrl)){
$this->AdvancedEpubUrl = trim($this->AdvancedEpubUrl); if(!preg_match('|/*_advanced.epub|ius', $this->AdvancedEpubUrl)){
$error->Add(new Exceptions\InvalidEbookAdvancedEpubUrlException('Invalid Ebook AdvancedEpubUrl: ' . $this->AdvancedEpubUrl));
}
if(strlen($this->AdvancedEpubUrl) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook AdvancedEpubUrl'));
}
} }
if(isset($this->AdvancedEpubUrl) && !preg_match('|/*_advanced.epub|ius', $this->AdvancedEpubUrl)){
$error->Add(new Exceptions\InvalidEbookAdvancedEpubUrlException('Invalid Ebook AdvancedEpubUrl: ' . $this->AdvancedEpubUrl));
} $this->KepubUrl = trim($this->KepubUrl ?? '');
if($this->KepubUrl == ''){
if(isset($this->AdvancedEpubUrl) && strlen($this->AdvancedEpubUrl) > EBOOKS_MAX_LONG_STRING_LENGTH){ $this->KepubUrl = null;
$error->Add(new Exceptions\StringTooLongException('Ebook AdvancedEpubUrl'));
} }
if(isset($this->KepubUrl)){ if(isset($this->KepubUrl)){
$this->KepubUrl = trim($this->KepubUrl); if(!preg_match('|/*.kepub.epub|ius', $this->KepubUrl)){
$error->Add(new Exceptions\InvalidEbookKepubUrlException('Invalid Ebook KepubUrl: ' . $this->KepubUrl));
}
if(strlen($this->KepubUrl) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook KepubUrl'));
}
} }
if(isset($this->KepubUrl) && !preg_match('|/*.kepub.epub|ius', $this->KepubUrl)){ $this->Azw3Url = trim($this->Azw3Url ?? '');
$error->Add(new Exceptions\InvalidEbookKepubUrlException('Invalid Ebook KepubUrl: ' . $this->KepubUrl)); if($this->Azw3Url == ''){
$this->Azw3Url = null;
}
if(isset($this->KepubUrl) && strlen($this->KepubUrl) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook KepubUrl'));
} }
if(isset($this->Azw3Url)){ if(isset($this->Azw3Url)){
$this->Azw3Url = trim($this->Azw3Url); if(!preg_match('|/*.azw3|ius', $this->Azw3Url)){
$error->Add(new Exceptions\InvalidEbookAzw3UrlException('Invalid Ebook Azw3Url: ' . $this->Azw3Url));
}
if(strlen($this->Azw3Url) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook Azw3Url'));
}
} }
if(isset($this->Azw3Url) && !preg_match('|/*.azw3|ius', $this->Azw3Url)){ $this->DistCoverUrl = trim($this->DistCoverUrl ?? '');
$error->Add(new Exceptions\InvalidEbookAzw3UrlException('Invalid Ebook Azw3Url: ' . $this->Azw3Url)); if($this->DistCoverUrl == ''){
$this->DistCoverUrl = null;
}
if(isset($this->Azw3Url) && strlen($this->Azw3Url) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook Azw3Url'));
} }
if(isset($this->DistCoverUrl)){ if(isset($this->DistCoverUrl)){
$this->DistCoverUrl = trim($this->DistCoverUrl); if(!preg_match('|/*cover.jpg|ius', $this->DistCoverUrl)){
$error->Add(new Exceptions\InvalidEbookDistCoverUrlException('Invalid Ebook DistCoverUrl: ' . $this->DistCoverUrl));
}
if(strlen($this->DistCoverUrl) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook DistCoverUrl'));
}
} }
if(isset($this->DistCoverUrl) && !preg_match('|/*cover.jpg|ius', $this->DistCoverUrl)){ if(isset($this->Title)){
$error->Add(new Exceptions\InvalidEbookDistCoverUrlException('Invalid Ebook DistCoverUrl: ' . $this->DistCoverUrl)); $this->Title = trim($this->Title ?? '');
if($this->Title == ''){
$error->Add(new Exceptions\EbookTitleRequiredException());
}
if(strlen($this->Title) > EBOOKS_MAX_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook Title'));
}
} }
else{
if(isset($this->DistCoverUrl) && strlen($this->DistCoverUrl) > EBOOKS_MAX_LONG_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook DistCoverUrl'));
}
$this->Title = trim($this->Title ?? '');
if($this->Title == ''){
$error->Add(new Exceptions\EbookTitleRequiredException()); $error->Add(new Exceptions\EbookTitleRequiredException());
} }
if(strlen($this->Title) > EBOOKS_MAX_STRING_LENGTH){ $this->FullTitle = trim($this->FullTitle ?? '');
$error->Add(new Exceptions\StringTooLongException('Ebook Title')); if($this->FullTitle == ''){
} $this->FullTitle = null;
if(isset($this->FullTitle)){
$this->FullTitle = trim($this->FullTitle);
} }
if(isset($this->FullTitle) && strlen($this->FullTitle) > EBOOKS_MAX_STRING_LENGTH){ if(isset($this->FullTitle) && strlen($this->FullTitle) > EBOOKS_MAX_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook FullTitle')); $error->Add(new Exceptions\StringTooLongException('Ebook FullTitle'));
} }
if(isset($this->AlternateTitle)){ $this->AlternateTitle = trim($this->AlternateTitle ?? '');
$this->AlternateTitle = trim($this->AlternateTitle); if($this->AlternateTitle == ''){
$this->AlternateTitle = null;
} }
if(isset($this->AlternateTitle) && strlen($this->AlternateTitle) > EBOOKS_MAX_STRING_LENGTH){ if(isset($this->AlternateTitle) && strlen($this->AlternateTitle) > EBOOKS_MAX_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook AlternateTitle')); $error->Add(new Exceptions\StringTooLongException('Ebook AlternateTitle'));
} }
$this->Description = trim($this->Description ?? ''); if(isset($this->Description)){
if($this->Description == ''){ $this->Description = trim($this->Description ?? '');
if($this->Description == ''){
$error->Add(new Exceptions\EbookDescriptionRequiredException());
}
}
else{
$error->Add(new Exceptions\EbookDescriptionRequiredException()); $error->Add(new Exceptions\EbookDescriptionRequiredException());
} }
$this->LongDescription = trim($this->LongDescription ?? ''); if(isset($this->LongDescription)){
if($this->LongDescription == ''){ $this->LongDescription = trim($this->LongDescription ?? '');
if($this->LongDescription == ''){
$error->Add(new Exceptions\EbookLongDescriptionRequiredException());
}
}
else{
$error->Add(new Exceptions\EbookLongDescriptionRequiredException()); $error->Add(new Exceptions\EbookLongDescriptionRequiredException());
} }
$this->Language = trim($this->Language ?? ''); if(isset($this->Language)){
if($this->Language == ''){ $this->Language = trim($this->Language ?? '');
if($this->Language == ''){
$error->Add(new Exceptions\EbookLanguageRequiredException());
}
if(strlen($this->Language) > 10){
$error->Add(new Exceptions\StringTooLongException('Ebook Language: ' . $this->Language));
}
}
else{
$error->Add(new Exceptions\EbookLanguageRequiredException()); $error->Add(new Exceptions\EbookLanguageRequiredException());
} }
if(strlen($this->Language) > 10){ if(isset($this->WordCount)){
$error->Add(new Exceptions\StringTooLongException('Ebook Language: ' . $this->Language)); if($this->WordCount <= 0){
$error->Add(new Exceptions\InvalidEbookWordCountException('Invalid Ebook WordCount: ' . $this->WordCount));
}
}
else{
$error->Add(new Exceptions\EbookWordCountRequiredException());
} }
if(isset($this->WordCount) && $this->WordCount <= 0){ if(isset($this->ReadingEase)){
$error->Add(new Exceptions\InvalidEbookWordCountException('Invalid Ebook WordCount: ' . $this->WordCount)); // In theory, Flesch reading ease can be negative, but in practice it's positive.
if($this->ReadingEase <= 0){
$error->Add(new Exceptions\InvalidEbookReadingEaseException('Invalid Ebook ReadingEase: ' . $this->ReadingEase));
}
}
else{
$error->Add(new Exceptions\EbookReadingEaseRequiredException());
} }
// In theory, Flesch reading ease can be negative, but in practice it's positive. $this->GitHubUrl = trim($this->GitHubUrl ?? '');
if(isset($this->ReadingEase) && $this->ReadingEase <= 0){ if($this->GitHubUrl == ''){
$error->Add(new Exceptions\InvalidEbookReadingEaseException('Invalid Ebook ReadingEase: ' . $this->ReadingEase)); $this->GitHubUrl = null;
} }
if(isset($this->GitHubUrl) && !preg_match('|https://github.com/standardebooks/\w+|ius', $this->GitHubUrl)){ if(isset($this->GitHubUrl)){
$error->Add(new Exceptions\InvalidEbookGitHubUrlException('Invalid Ebook GitHubUrl: ' . $this->GitHubUrl)); if(!preg_match('|https://github.com/standardebooks/\w+|ius', $this->GitHubUrl)){
$error->Add(new Exceptions\InvalidEbookGitHubUrlException('Invalid Ebook GitHubUrl: ' . $this->GitHubUrl));
}
if(strlen($this->GitHubUrl) > EBOOKS_MAX_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook GitHubUrl'));
}
} }
if(isset($this->GitHubUrl) && strlen($this->GitHubUrl) > EBOOKS_MAX_STRING_LENGTH){ $this->WikipediaUrl = trim($this->WikipediaUrl ?? '');
$error->Add(new Exceptions\StringTooLongException('Ebook GitHubUrl')); if($this->WikipediaUrl == ''){
$this->WikipediaUrl = null;
} }
if(isset($this->WikipediaUrl) && !preg_match('|https://.*wiki.*|ius', $this->WikipediaUrl)){ if(isset($this->WikipediaUrl)){
$error->Add(new Exceptions\InvalidEbookWikipediaUrlException('Invalid Ebook WikipediaUrl: ' . $this->WikipediaUrl)); if(!preg_match('|https://.*wiki.*|ius', $this->WikipediaUrl)){
$error->Add(new Exceptions\InvalidEbookWikipediaUrlException('Invalid Ebook WikipediaUrl: ' . $this->WikipediaUrl));
}
if(strlen($this->WikipediaUrl) > EBOOKS_MAX_STRING_LENGTH){
$error->Add(new Exceptions\StringTooLongException('Ebook WikipediaUrl'));
}
} }
if(isset($this->WikipediaUrl) && strlen($this->WikipediaUrl) > EBOOKS_MAX_STRING_LENGTH){ if(isset($this->EbookCreated)){
$error->Add(new Exceptions\StringTooLongException('Ebook WikipediaUrl')); if($this->EbookCreated > $now || $this->EbookCreated < EBOOK_EARLIEST_CREATION_DATE){
$error->Add(new Exceptions\InvalidEbookCreatedDatetimeException($this->EbookCreated));
}
}
else{
$error->Add(new Exceptions\EbookCreatedDatetimeRequiredException());
} }
if(!isset($this->EbookCreated) || $this->EbookCreated > $now || $this->EbookCreated < EBOOK_EARLIEST_CREATION_DATE){ if(isset($this->EbookUpdated)){
$error->Add(new Exceptions\InvalidEbookCreatedDatetimeException($this->EbookCreated)); if($this->EbookUpdated > $now || $this->EbookUpdated < EBOOK_EARLIEST_CREATION_DATE){
$error->Add(new Exceptions\InvalidEbookUpdatedDatetimeException($this->EbookUpdated));
}
}
else{
$error->Add(new Exceptions\EbookUpdatedDatetimeRequiredException());
} }
if(!isset($this->EbookUpdated) || $this->EbookUpdated > $now || $this->EbookUpdated < EBOOK_EARLIEST_CREATION_DATE){ if(isset($this->TextSinglePageByteCount)){
$error->Add(new Exceptions\InvalidEbookUpdatedDatetimeException($this->EbookUpdated)); if($this->TextSinglePageByteCount <= 0){
$error->Add(new Exceptions\InvalidEbookTextSinglePageByteCountException('Invalid Ebook TextSinglePageByteCount: ' . $this->TextSinglePageByteCount));
}
}
else{
$error->Add(new Exceptions\EbookTextSinglePageByteCountRequiredException());
} }
if(!isset($this->TextSinglePageByteCount) || $this->TextSinglePageByteCount <= 0){ if(isset($this->IndexableText)){
$error->Add(new Exceptions\InvalidEbookTextSinglePageByteCountException('Invalid Ebook TextSinglePageByteCount: ' . $this->TextSinglePageByteCount)); $this->IndexableText = trim($this->IndexableText ?? '');
}
if(!isset($this->IndexableText) || $this->IndexableText == ''){ if($this->IndexableText == ''){
$error->Add(new Exceptions\EbookIndexableTextRequiredException());
}
}
else{
$error->Add(new Exceptions\EbookIndexableTextRequiredException()); $error->Add(new Exceptions\EbookIndexableTextRequiredException());
} }

View file

@ -0,0 +1,7 @@
<?
namespace Exceptions;
class EbookCreatedDatetimeRequiredException extends AppException{
/** @var string $message */
protected $message = 'EbookCreated datetime required.';
}

View file

@ -0,0 +1,7 @@
<?
namespace Exceptions;
class EbookReadingEaseRequiredException extends AppException{
/** @var string $message */
protected $message = 'Ebook ReadingEase required.';
}

View file

@ -0,0 +1,7 @@
<?
namespace Exceptions;
class EbookRepoFilesystemPathRequiredException extends AppException{
/** @var string $message */
protected $message = 'Ebook RepoFilesystemPath required.';
}

View file

@ -0,0 +1,7 @@
<?
namespace Exceptions;
class EbookTextSinglePageByteCountRequiredException extends AppException{
/** @var string $message */
protected $message = 'Ebook TextSinglePageByteCount required.';
}

View file

@ -0,0 +1,7 @@
<?
namespace Exceptions;
class EbookUpdatedDatetimeRequiredException extends AppException{
/** @var string $message */
protected $message = 'EbookUpdated datetime required.';
}

View file

@ -0,0 +1,7 @@
<?
namespace Exceptions;
class EbookWordCountRequiredException extends AppException{
/** @var string $message */
protected $message = 'Ebook WordCount required.';
}

View file

@ -0,0 +1,7 @@
<?
namespace Exceptions;
class EbookWwwFilesystemPathRequiredException extends AppException{
/** @var string $message */
protected $message = 'Ebook WwwFilesystemPath required.';
}