diff --git a/lib/Collection.php b/lib/Collection.php index 3416e158..9c5e51cc 100644 --- a/lib/Collection.php +++ b/lib/Collection.php @@ -55,13 +55,34 @@ class Collection{ public function Validate(): void{ $error = new Exceptions\ValidationException(); - $this->Name = trim($this->Name ?? ''); - if($this->Name == ''){ + if(isset($this->Name)){ + $this->Name = trim($this->Name); + + if($this->Name == ''){ + $error->Add(new Exceptions\CollectionNameRequiredException()); + } + + if(strlen($this->Name) > EBOOKS_MAX_STRING_LENGTH){ + $error->Add(new Exceptions\StringTooLongException('Collection name: '. $this->Name)); + } + } + else{ $error->Add(new Exceptions\CollectionNameRequiredException()); } - if(strlen($this->Name) > EBOOKS_MAX_STRING_LENGTH){ - $error->Add(new Exceptions\StringTooLongException('Collection name: '. $this->Name)); + if(isset($this->UrlName)){ + $this->UrlName = trim($this->UrlName); + + if($this->UrlName == ''){ + $error->Add(new Exceptions\CollectionUrlNameRequiredException()); + } + + if(strlen($this->UrlName) > EBOOKS_MAX_STRING_LENGTH){ + $error->Add(new Exceptions\StringTooLongException('Collection UrlName: '. $this->UrlName)); + } + } + else{ + $error->Add(new Exceptions\CollectionUrlNameRequiredException()); } if($this->Type !== null && ($this->Type != CollectionType::Series && $this->Type != CollectionType::Set)){ diff --git a/lib/Contributor.php b/lib/Contributor.php index 2982293b..244f73d7 100644 --- a/lib/Contributor.php +++ b/lib/Contributor.php @@ -1,4 +1,9 @@ EbookId)){ + $error->Add(new Exceptions\ContributorEbookIdRequiredException()); + } + + if(isset($this->Name)){ + $this->Name = trim($this->Name); + + if($this->Name == ''){ + $error->Add(new Exceptions\ContributorNameRequiredException()); + } + } + else{ + $error->Add(new Exceptions\ContributorNameRequiredException()); + } + + if(isset($this->UrlName)){ + $this->UrlName = trim($this->UrlName); + + if($this->UrlName == ''){ + $error->Add(new Exceptions\ContributorUrlNameRequiredException()); + } + } + else{ + $error->Add(new Exceptions\ContributorUrlNameRequiredException()); + } + + $this->SortName = trim($this->SortName ?? ''); + if($this->SortName == ''){ + $this->SortName = null; + } + + $this->FullName = trim($this->FullName ?? ''); + if($this->FullName == ''){ + $this->FullName = null; + } + + $this->WikipediaUrl = trim($this->WikipediaUrl ?? ''); + if($this->WikipediaUrl == ''){ + $this->WikipediaUrl = null; + } + + if(isset($this->WikipediaUrl)){ + if(!preg_match('|https://.*wiki.*|ius', $this->WikipediaUrl)){ + $error->Add(new Exceptions\InvalidContributorWikipediaUrlException('Invalid Contributor WikipediaUrl: ' . $this->WikipediaUrl)); + } + + if(strlen($this->WikipediaUrl) > EBOOKS_MAX_STRING_LENGTH){ + $error->Add(new Exceptions\StringTooLongException('Contributor WikipediaUrl')); + } + } + + $this->MarcRole = trim($this->MarcRole ?? ''); + if($this->MarcRole == ''){ + $this->MarcRole = null; + } + + $this->NacoafUrl = trim($this->NacoafUrl ?? ''); + if($this->NacoafUrl == ''){ + $this->NacoafUrl = null; + } + + if(isset($this->NacoafUrl)){ + if(!preg_match('|https?://id\.loc\.gov/.*|ius', $this->NacoafUrl)){ + $error->Add(new Exceptions\InvalidContributorNacoafUrlException('Invalid Contributor NacoafUrl: ' . $this->NacoafUrl)); + } + + if(strlen($this->NacoafUrl) > EBOOKS_MAX_STRING_LENGTH){ + $error->Add(new Exceptions\StringTooLongException('Contributor NacoafUrl')); + } + } + + if(!isset($this->SortOrder)){ + $error->Add(new Exceptions\ContributorSortOrderRequiredException()); + } + + if($error->HasExceptions){ + throw $error; + } + } + + /** + * @throws Exceptions\ValidationException + */ public function Create(): void{ + $this->Validate(); Db::Query(' INSERT into Contributors (EbookId, Name, UrlName, SortName, WikipediaUrl, MarcRole, FullName, NacoafUrl, SortOrder) diff --git a/lib/Ebook.php b/lib/Ebook.php index a72e80e3..3450159b 100644 --- a/lib/Ebook.php +++ b/lib/Ebook.php @@ -1750,6 +1750,9 @@ class Ebook{ ); } + /** + * @throws Exceptions\ValidationException + */ private function InsertGitCommits(): void{ foreach($this->GitCommits as $commit){ $commit->EbookId = $this->EbookId; @@ -1765,6 +1768,9 @@ class Ebook{ ); } + /** + * @throws Exceptions\ValidationException + */ private function InsertSources(): void{ foreach($this->Sources as $source){ $source->EbookId = $this->EbookId; @@ -1780,6 +1786,9 @@ class Ebook{ ); } + /** + * @throws Exceptions\ValidationException + */ private function InsertContributors(): void{ $allContributors = array_merge($this->Authors, $this->Illustrators, $this->Translators, $this->Contributors); foreach($allContributors as $sortOrder => $contributor){ diff --git a/lib/EbookSource.php b/lib/EbookSource.php index 4ab4dcb4..fcbc098e 100644 --- a/lib/EbookSource.php +++ b/lib/EbookSource.php @@ -1,4 +1,7 @@ EbookId)){ + $error->Add(new Exceptions\EbookSourceEbookIdRequiredException()); + } + + if(isset($this->Url)){ + $this->Url = trim($this->Url); + + if($this->Url == ''){ + $error->Add(new Exceptions\EbookSourceUrlRequiredException()); + } + } + else{ + $error->Add(new Exceptions\EbookSourceUrlRequiredException()); + } + + if($error->HasExceptions){ + throw $error; + } + } + + /** + * @throws Exceptions\ValidationException + */ public function Create(): void{ + $this->Validate(); Db::Query(' INSERT into EbookSources (EbookId, Type, Url) values (?, diff --git a/lib/EbookTag.php b/lib/EbookTag.php index 69c214ed..a38854fa 100644 --- a/lib/EbookTag.php +++ b/lib/EbookTag.php @@ -33,13 +33,19 @@ class EbookTag extends Tag{ public function Validate(): void{ $error = new Exceptions\ValidationException(); - $this->Name = trim($this->Name ?? ''); - if($this->Name == ''){ - $error->Add(new Exceptions\EbookTagNameRequiredException()); - } + if(isset($this->Name)){ + $this->Name = trim($this->Name); - if(strlen($this->Name) > EBOOKS_MAX_STRING_LENGTH){ - $error->Add(new Exceptions\StringTooLongException('Ebook tag: '. $this->Name)); + if($this->Name == ''){ + $error->Add(new Exceptions\EbookTagNameRequiredException()); + } + + if(strlen($this->Name) > EBOOKS_MAX_STRING_LENGTH){ + $error->Add(new Exceptions\StringTooLongException('Ebook tag: '. $this->Name)); + } + } + else{ + $error->Add(new Exceptions\EbookTagNameRequiredException()); } if($this->Type != TagType::Ebook){ diff --git a/lib/Exceptions/CollectionUrlNameRequiredException.php b/lib/Exceptions/CollectionUrlNameRequiredException.php new file mode 100644 index 00000000..17f45410 --- /dev/null +++ b/lib/Exceptions/CollectionUrlNameRequiredException.php @@ -0,0 +1,7 @@ +message = 'Invalid GitCommit Created datetime. ' . $createdDatetime->format('Y-m-d') . ' is not between ' . EBOOK_EARLIEST_CREATION_DATE->format('Y-m-d') . ' and ' . $now->format('Y-m-d') . '.'; + } +} diff --git a/lib/GitCommit.php b/lib/GitCommit.php index 3df150df..a2bc22d3 100644 --- a/lib/GitCommit.php +++ b/lib/GitCommit.php @@ -24,7 +24,60 @@ class GitCommit{ return $instance; } + /** + * @throws Exceptions\ValidationException + */ + public function Validate(): void{ + /** @throws void */ + $now = new DateTimeImmutable(); + + $error = new Exceptions\ValidationException(); + + if(!isset($this->EbookId)){ + $error->Add(new Exceptions\GitCommitEbookIdRequiredException()); + } + + if(isset($this->Created)){ + if($this->Created > $now || $this->Created < EBOOK_EARLIEST_CREATION_DATE){ + $error->Add(new Exceptions\InvalidGitCommitCreatedDatetimeException($this->Created)); + } + } + else{ + $error->Add(new Exceptions\GitCommitCreatedDatetimeRequiredException()); + } + + if(isset($this->Message)){ + $this->Message = trim($this->Message); + + if($this->Message == ''){ + $error->Add(new Exceptions\GitCommitMessageRequiredException()); + } + } + else{ + $error->Add(new Exceptions\GitCommitMessageRequiredException()); + } + + if(isset($this->Hash)){ + $this->Hash = trim($this->Hash); + + if($this->Hash == ''){ + $error->Add(new Exceptions\GitCommitHashRequiredException()); + } + } + else{ + $error->Add(new Exceptions\GitCommitHashRequiredException()); + } + + if($error->HasExceptions){ + throw $error; + } + } + + /** + * @throws Exceptions\ValidationException + */ public function Create(): void{ + $this->Validate(); Db::Query(' INSERT into GitCommits (EbookId, Created, Message, Hash) values (?, diff --git a/lib/LocSubject.php b/lib/LocSubject.php index 935dd35c..ce161e18 100644 --- a/lib/LocSubject.php +++ b/lib/LocSubject.php @@ -9,13 +9,19 @@ class LocSubject{ public function Validate(): void{ $error = new Exceptions\ValidationException(); - $this->Name = trim($this->Name ?? ''); - if($this->Name == ''){ - $error->Add(new Exceptions\LocSubjectNameRequiredException()); - } + if(isset($this->Name)){ + $this->Name = trim($this->Name); - if(strlen($this->Name) > EBOOKS_MAX_STRING_LENGTH){ - $error->Add(new Exceptions\StringTooLongException('LoC subject: '. $this->Name)); + if($this->Name == ''){ + $error->Add(new Exceptions\LocSubjectNameRequiredException()); + } + + if(strlen($this->Name) > EBOOKS_MAX_STRING_LENGTH){ + $error->Add(new Exceptions\StringTooLongException('LoC subject: '. $this->Name)); + } + } + else{ + $error->Add(new Exceptions\LocSubjectNameRequiredException()); } if($error->HasExceptions){