diff --git a/lib/Artist.php b/lib/Artist.php index 444a2f93..2a97d2ca 100644 --- a/lib/Artist.php +++ b/lib/Artist.php @@ -41,11 +41,7 @@ class Artist{ } protected function GetUrl(): string{ - if(!isset($this->_Url)){ - $this->_Url = '/artworks/' . $this->UrlName; - } - - return $this->_Url; + return $this->_Url ??= '/artworks/' . $this->UrlName; } /** @@ -82,7 +78,9 @@ class Artist{ $error = new Exceptions\InvalidArtistException(); - if(!isset($this->Name) || $this->Name == ''){ + $this->Name = trim($this->Name ?? ''); + + if($this->Name == ''){ $error->Add(new Exceptions\ArtistNameRequiredException()); } elseif(strlen($this->Name) > ARTWORK_MAX_STRING_LENGTH){ diff --git a/lib/Artwork.php b/lib/Artwork.php index 93cbd93b..e3cbc11f 100644 --- a/lib/Artwork.php +++ b/lib/Artwork.php @@ -146,35 +146,23 @@ class Artwork{ } protected function GetUrl(): string{ - if(!isset($this->_Url)){ - $this->_Url = '/artworks/' . $this->Artist->UrlName . '/' . $this->UrlName; - } - - return $this->_Url; + return $this->_Url ??= '/artworks/' . $this->Artist->UrlName . '/' . $this->UrlName; } protected function GetEditUrl(): string{ - if(!isset($this->_EditUrl)){ - $this->_EditUrl = $this->Url . '/edit'; - } - - return $this->_EditUrl; + return $this->_EditUrl ??= $this->Url . '/edit'; } /** * @return array */ protected function GetTags(): array{ - if(!isset($this->_Tags)){ - $this->_Tags = Db::Query(' + return $this->_Tags ??= Db::Query(' SELECT t.* from Tags t inner join ArtworkTags at using (TagId) where ArtworkId = ? ', [$this->ArtworkId], ArtworkTag::class); - } - - return $this->_Tags; } /** @@ -359,28 +347,29 @@ class Artwork{ } } - if($this->Exception !== null && trim($this->Exception) == ''){ + $this->Exception = trim($this->Exception ?? ''); + + if($this->Exception == ''){ $this->Exception = null; } - if($this->Notes !== null && trim($this->Notes) == ''){ + $this->Notes = trim($this->Notes ?? ''); + + if($this->Notes == ''){ $this->Notes = null; } - if(isset($this->Name)){ - if($this->Name == ''){ - $error->Add(new Exceptions\ArtworkNameRequiredException()); - } + $this->Name = trim($this->Name ?? ''); - if(strlen($this->Name) > ARTWORK_MAX_STRING_LENGTH){ - $error->Add(new Exceptions\StringTooLongException('Artwork Name')); - } - } - else{ + if($this->Name == ''){ $error->Add(new Exceptions\ArtworkNameRequiredException()); } - if($this->CompletedYear !== null && ($this->CompletedYear <= 0 || $this->CompletedYear > $thisYear)){ + if(strlen($this->Name) > ARTWORK_MAX_STRING_LENGTH){ + $error->Add(new Exceptions\StringTooLongException('Artwork Name')); + } + + if(isset($this->CompletedYear) && ($this->CompletedYear <= 0 || $this->CompletedYear > $thisYear)){ $error->Add(new Exceptions\InvalidCompletedYearException()); } @@ -388,7 +377,7 @@ class Artwork{ $this->CompletedYearIsCirca = false; } - if($this->PublicationYear !== null && ($this->PublicationYear <= 0 || $this->PublicationYear > $thisYear)){ + if(isset($this->PublicationYear) && ($this->PublicationYear <= 0 || $this->PublicationYear > $thisYear)){ $error->Add(new Exceptions\InvalidPublicationYearException()); } @@ -396,29 +385,28 @@ class Artwork{ $error->Add(new Exceptions\InvalidArtworkStatusException()); } - if(isset($this->Tags)){ - if(count($this->Tags) == 0){ - $error->Add(new Exceptions\TagsRequiredException()); - } + $this->Tags ??= []; - if(count($this->Tags) > ARTWORK_MAX_TAGS){ - $error->Add(new Exceptions\TooManyTagsException()); - } - - foreach($this->Tags as $tag){ - try{ - $tag->Validate(); - } - catch(Exceptions\ValidationException $ex){ - $error->Add($ex); - } - } - } - else{ + if(sizeof($this->Tags) == 0){ $error->Add(new Exceptions\TagsRequiredException()); } - if($this->MuseumUrl !== null){ + if(sizeof($this->Tags) > ARTWORK_MAX_TAGS){ + $error->Add(new Exceptions\TooManyTagsException()); + } + + foreach($this->Tags as $tag){ + try{ + $tag->Validate(); + } + catch(Exceptions\ValidationException $ex){ + $error->Add($ex); + } + } + + $this->MuseumUrl = trim($this->MuseumUrl ?? ''); + + if($this->MuseumUrl != ''){ if(strlen($this->MuseumUrl) > ARTWORK_MAX_STRING_LENGTH){ $error->Add(new Exceptions\StringTooLongException('Link to an approved museum page')); } @@ -431,13 +419,18 @@ class Artwork{ $error->Add($ex); } } + else{ + $this->MuseumUrl = null; + } - if($this->PublicationYearPageUrl !== null){ + $this->PublicationYearPageUrl = trim($this->PublicationYearPageUrl ?? ''); + + if($this->PublicationYearPageUrl != ''){ if(strlen($this->PublicationYearPageUrl) > ARTWORK_MAX_STRING_LENGTH){ $error->Add(new Exceptions\StringTooLongException('Link to page with year of publication')); } - if($this->PublicationYearPageUrl == '' || filter_var($this->PublicationYearPageUrl, FILTER_VALIDATE_URL) === false){ + if(filter_var($this->PublicationYearPageUrl, FILTER_VALIDATE_URL) === false){ $error->Add(new Exceptions\InvalidPublicationYearPageUrlException()); } else{ @@ -449,13 +442,18 @@ class Artwork{ } } } + else{ + $this->PublicationYearPageUrl = null; + } - if($this->CopyrightPageUrl !== null){ + $this->CopyrightPageUrl = trim($this->CopyrightPageUrl ?? ''); + + if($this->CopyrightPageUrl != ''){ if(strlen($this->CopyrightPageUrl) > ARTWORK_MAX_STRING_LENGTH){ $error->Add(new Exceptions\StringTooLongException('Link to page with copyright details')); } - if($this->CopyrightPageUrl == '' || filter_var($this->CopyrightPageUrl, FILTER_VALIDATE_URL) === false){ + if(filter_var($this->CopyrightPageUrl, FILTER_VALIDATE_URL) === false){ $error->Add(new Exceptions\InvalidCopyrightPageUrlException()); } else{ @@ -467,13 +465,18 @@ class Artwork{ } } } + else{ + $this->CopyrightPageUrl = null; + } - if($this->ArtworkPageUrl !== null){ + $this->ArtworkPageUrl = trim($this->ArtworkPageUrl ?? ''); + + if($this->ArtworkPageUrl != ''){ if(strlen($this->ArtworkPageUrl) > ARTWORK_MAX_STRING_LENGTH){ $error->Add(new Exceptions\StringTooLongException('Link to page with artwork')); } - if($this->ArtworkPageUrl == '' || filter_var($this->ArtworkPageUrl, FILTER_VALIDATE_URL) === false){ + if(filter_var($this->ArtworkPageUrl, FILTER_VALIDATE_URL) === false){ $error->Add(new Exceptions\InvalidArtworkPageUrlException()); } else{ @@ -485,18 +488,20 @@ class Artwork{ } } } + else{ + $this->ArtworkPageUrl = null; + } - $hasMuseumProof = $this->MuseumUrl !== null && $this->MuseumUrl != ''; - $hasBookProof = $this->PublicationYear !== null - && ($this->PublicationYearPageUrl !== null && $this->PublicationYearPageUrl != '') - && ($this->ArtworkPageUrl !== null && $this->ArtworkPageUrl != ''); + $hasMuseumProof = $this->MuseumUrl !== null; + $hasBookProof = $this->PublicationYear !== null && $this->PublicationYearPageUrl !== null && $this->ArtworkPageUrl !== null; if(!$hasMuseumProof && !$hasBookProof && $this->Exception === null){ $error->Add(new Exceptions\MissingPdProofException()); } - // Check the ebook URL. - if($this->EbookUrl !== null){ + $this->EbookUrl = trim($this->EbookUrl ?? ''); + + if($this->EbookUrl != ''){ try{ Ebook::GetByIdentifier('url:' . $this->EbookUrl); @@ -507,6 +512,9 @@ class Artwork{ $error->Add(new Exceptions\EbookNotFoundException('Couldn’t find an ebook with that URL.')); } } + else{ + $this->EbookUrl = null; + } // Check for existing `Artwork` objects with the same URL but different `ArtworkID`s. try{ diff --git a/lib/ArtworkTag.php b/lib/ArtworkTag.php index 0e3ae556..46d59ad7 100644 --- a/lib/ArtworkTag.php +++ b/lib/ArtworkTag.php @@ -13,11 +13,7 @@ class ArtworkTag extends Tag{ // ******* protected function GetUrl(): string{ - if(!isset($this->_Url)){ - $this->_Url = '/artworks?query=' . Formatter::MakeUrlSafe($this->Name); - } - - return $this->_Url; + return $this->_Url ??= '/artworks?query=' . Formatter::MakeUrlSafe($this->Name); } diff --git a/lib/Collection.php b/lib/Collection.php index d50e26be..f22d532a 100644 --- a/lib/Collection.php +++ b/lib/Collection.php @@ -24,22 +24,14 @@ class Collection{ // ******* protected function GetUrl(): string{ - if(!isset($this->_Url)){ - $this->_Url = '/collections/' . $this->UrlName; - } - - return $this->_Url; + return $this->_Url ??= '/collections/' . $this->UrlName; } /** * @return array */ protected function GetEbooks(): array{ - if(!isset($this->_Ebooks)){ - $this->_Ebooks = Ebook::GetAllByCollection($this->CollectionId); - } - - return $this->_Ebooks; + return $this->_Ebooks ??= Ebook::GetAllByCollection($this->CollectionId); } diff --git a/lib/Contributor.php b/lib/Contributor.php index c8d93914..21bbc483 100644 --- a/lib/Contributor.php +++ b/lib/Contributor.php @@ -73,7 +73,6 @@ class Contributor{ $error->Add(new Exceptions\ContributorEbookIdRequiredException()); } - $this->Name = trim($this->Name ?? ''); if($this->Name == ''){ $error->Add(new Exceptions\ContributorNameRequiredException()); diff --git a/lib/DonationDrive.php b/lib/DonationDrive.php index c6b4667a..228ad755 100644 --- a/lib/DonationDrive.php +++ b/lib/DonationDrive.php @@ -24,33 +24,30 @@ class DonationDrive{ // ******* protected function GetDonationCount(): int{ - if(!isset($this->_DonationCount)){ - $this->_DonationCount = Db::QueryInt(' - SELECT sum(cnt) - from + return $this->_DonationCount ??= Db::QueryInt(' + SELECT sum(cnt) + from + ( ( + # Anonymous patrons, i.e. from AOGF + select count(*) cnt from Payments + where + UserId is null + and ( - # Anonymous patrons, i.e. from AOGF - select count(*) cnt from Payments - where - UserId is null - and - ( - (IsRecurring = true and Amount >= 10 and Created >= ?) - or - (IsRecurring = false and Amount >= 100 and Created >= ?) - ) + (IsRecurring = true and Amount >= 10 and Created >= ?) + or + (IsRecurring = false and Amount >= 100 and Created >= ?) ) - union all - ( - # All non-anonymous patrons - select count(*) as cnt from Patrons - where Created >= ? - ) - ) x - ', [$this->Start, $this->Start, $this->Start]); - } - return $this->_DonationCount; + ) + union all + ( + # All non-anonymous patrons + select count(*) as cnt from Patrons + where Created >= ? + ) + ) x + ', [$this->Start, $this->Start, $this->Start]); } protected function GetTargetDonationCount(): int{ diff --git a/lib/Ebook.php b/lib/Ebook.php index 1a269bcf..62ec7306 100644 --- a/lib/Ebook.php +++ b/lib/Ebook.php @@ -143,8 +143,7 @@ final class Ebook{ // ******* protected function GetArtwork(): ?Artwork{ - if(!isset($this->_Artwork)){ - $this->_Artwork = Db::Query(' + return $this->_Artwork ??= Db::Query(' SELECT * from @@ -152,17 +151,13 @@ final class Ebook{ where EbookUrl = ? ', [preg_replace('/^url:/iu', '', $this->Identifier)], Artwork::class)[0] ?? null; - } - - return $this->_Artwork; } /** * @return array */ protected function GetProjects(): array{ - if(!isset($this->_Projects)){ - $this->_Projects = Db::MultiTableSelect(' + return $this->_Projects ??= Db::MultiTableSelect(' SELECT * from Projects inner join Ebooks @@ -170,9 +165,6 @@ final class Ebook{ where Ebooks.EbookId = ? order by Projects.Created desc ', [$this->EbookId], Project::class); - } - - return $this->_Projects; } protected function GetProjectInProgress(): ?Project{ @@ -222,82 +214,62 @@ final class Ebook{ * @return array */ protected function GetGitCommits(): array{ - if(!isset($this->_GitCommits)){ - $this->_GitCommits = Db::Query(' + return $this->_GitCommits ??= Db::Query(' SELECT * from GitCommits where EbookId = ? order by Created desc ', [$this->EbookId], GitCommit::class); - } - - return $this->_GitCommits; } /** * @return array */ protected function GetTags(): array{ - if(!isset($this->_Tags)){ - $this->_Tags = Db::Query(' + return $this->_Tags ??= Db::Query(' SELECT t.* from Tags t inner join EbookTags et using (TagId) where EbookId = ? order by SortOrder asc ', [$this->EbookId], EbookTag::class); - } - - return $this->_Tags; } /** * @return array */ protected function GetLocSubjects(): array{ - if(!isset($this->_LocSubjects)){ - $this->_LocSubjects = Db::Query(' + return $this->_LocSubjects ??= Db::Query(' SELECT l.* from LocSubjects l inner join EbookLocSubjects el using (LocSubjectId) where EbookId = ? order by SortOrder asc ', [$this->EbookId], LocSubject::class); - } - - return $this->_LocSubjects; } /** * @return array */ protected function GetCollectionMemberships(): array{ - if(!isset($this->_CollectionMemberships)){ - $this->_CollectionMemberships = Db::Query(' + return $this->_CollectionMemberships ??= Db::Query(' SELECT * from CollectionEbooks where EbookId = ? order by SortOrder asc ', [$this->EbookId], CollectionMembership::class); - } - - return $this->_CollectionMemberships; } /** * @return array */ protected function GetSources(): array{ - if(!isset($this->_Sources)){ - $this->_Sources = Db::Query(' + return $this->_Sources ??= Db::Query(' SELECT * from EbookSources where EbookId = ? order by SortOrder asc ', [$this->EbookId], EbookSource::class); - } - - return $this->_Sources; } /** @@ -418,51 +390,27 @@ final class Ebook{ } protected function GetUrl(): string{ - if(!isset($this->_Url)){ - $this->_Url = str_replace(EBOOKS_IDENTIFIER_ROOT, '', $this->Identifier); - } - - return $this->_Url; + return $this->_Url ??= str_replace(EBOOKS_IDENTIFIER_ROOT, '', $this->Identifier); } protected function GetEditUrl(): string{ - if(!isset($this->_EditUrl)){ - $this->_EditUrl = $this->Url . '/edit'; - } - - return $this->_EditUrl; + return $this->_EditUrl ??= $this->Url . '/edit'; } protected function GetDeleteUrl(): string{ - if(!isset($this->_DeleteUrl)){ - $this->_DeleteUrl = $this->Url . '/delete'; - } - - return $this->_DeleteUrl; + return $this->_DeleteUrl ??= $this->Url . '/delete'; } protected function GetHasDownloads(): bool{ - if(!isset($this->_HasDownloads)){ - $this->_HasDownloads = $this->EpubUrl || $this->AdvancedEpubUrl || $this->KepubUrl || $this->Azw3Url; - } - - return $this->_HasDownloads; + return $this->_HasDownloads ??= $this->EpubUrl || $this->AdvancedEpubUrl || $this->KepubUrl || $this->Azw3Url; } protected function GetUrlSafeIdentifier(): string{ - if(!isset($this->_UrlSafeIdentifier)){ - $this->_UrlSafeIdentifier = str_replace(['url:https://standardebooks.org/ebooks/', '/'], ['', '_'], $this->Identifier); - } - - return $this->_UrlSafeIdentifier; + return $this->_UrlSafeIdentifier ??= str_replace(['url:https://standardebooks.org/ebooks/', '/'], ['', '_'], $this->Identifier); } protected function GetHeroImageUrl(): string{ - if(!isset($this->_HeroImageUrl)){ - $this->_HeroImageUrl = '/images/covers/' . $this->UrlSafeIdentifier . '-' . substr(sha1($this->Updated->format(Enums\DateTimeFormat::UnixTimestamp->value)), 0, 8) . '-hero.jpg'; - } - - return $this->_HeroImageUrl; + return $this->_HeroImageUrl ??= '/images/covers/' . $this->UrlSafeIdentifier . '-' . substr(sha1($this->Updated->format(Enums\DateTimeFormat::UnixTimestamp->value)), 0, 8) . '-hero.jpg'; } protected function GetHeroImageAvifUrl(): string{ @@ -479,11 +427,7 @@ final class Ebook{ } protected function GetHeroImage2xUrl(): string{ - if(!isset($this->_HeroImage2xUrl)){ - $this->_HeroImage2xUrl = '/images/covers/' . $this->UrlSafeIdentifier . '-' . substr(sha1($this->Updated->format(Enums\DateTimeFormat::UnixTimestamp->value)), 0, 8) . '-hero@2x.jpg'; - } - - return $this->_HeroImage2xUrl; + return $this->_HeroImage2xUrl ??= '/images/covers/' . $this->UrlSafeIdentifier . '-' . substr(sha1($this->Updated->format(Enums\DateTimeFormat::UnixTimestamp->value)), 0, 8) . '-hero@2x.jpg'; } protected function GetHeroImage2xAvifUrl(): string{ @@ -500,11 +444,7 @@ final class Ebook{ } protected function GetCoverImageUrl(): string{ - if(!isset($this->_CoverImageUrl)){ - $this->_CoverImageUrl = '/images/covers/' . $this->UrlSafeIdentifier . '-' . substr(sha1($this->Updated->format(Enums\DateTimeFormat::UnixTimestamp->value)), 0, 8) . '-cover.jpg'; - } - - return $this->_CoverImageUrl; + return $this->_CoverImageUrl ??= '/images/covers/' . $this->UrlSafeIdentifier . '-' . substr(sha1($this->Updated->format(Enums\DateTimeFormat::UnixTimestamp->value)), 0, 8) . '-cover.jpg'; } protected function GetCoverImageAvifUrl(): string{ @@ -521,11 +461,7 @@ final class Ebook{ } protected function GetCoverImage2xUrl(): string{ - if(!isset($this->_CoverImage2xUrl)){ - $this->_CoverImage2xUrl = '/images/covers/' . $this->UrlSafeIdentifier . '-' . substr(sha1($this->Updated->format(Enums\DateTimeFormat::UnixTimestamp->value)), 0, 8) . '-cover@2x.jpg'; - } - - return $this->_CoverImage2xUrl; + return $this->_CoverImage2xUrl ??= '/images/covers/' . $this->UrlSafeIdentifier . '-' . substr(sha1($this->Updated->format(Enums\DateTimeFormat::UnixTimestamp->value)), 0, 8) . '-cover@2x.jpg'; } protected function GetCoverImage2xAvifUrl(): string{ @@ -601,27 +537,15 @@ final class Ebook{ } protected function GetAuthorsHtml(): string{ - if(!isset($this->_AuthorsHtml)){ - $this->_AuthorsHtml = Ebook::GenerateContributorList($this->Authors, true); - } - - return $this->_AuthorsHtml; + return $this->_AuthorsHtml ??= Ebook::GenerateContributorList($this->Authors, true); } protected function GetAuthorsUrl(): string{ - if(!isset($this->_AuthorsUrl)){ - $this->_AuthorsUrl = preg_replace('|url:https://standardebooks.org/ebooks/([^/]+)/.*|ius', '/ebooks/\1', $this->Identifier); - } - - return $this->_AuthorsUrl; + return $this->_AuthorsUrl ??= preg_replace('|url:https://standardebooks.org/ebooks/([^/]+)/.*|ius', '/ebooks/\1', $this->Identifier); } protected function GetAuthorsString(): string{ - if(!isset($this->_AuthorsString)){ - $this->_AuthorsString = strip_tags(Ebook::GenerateContributorList($this->Authors, false)); - } - - return $this->_AuthorsString; + return $this->_AuthorsString ??= strip_tags(Ebook::GenerateContributorList($this->Authors, false)); } protected function GetContributorsHtml(): string{ @@ -673,19 +597,11 @@ final class Ebook{ } protected function GetTextUrl(): string{ - if(!isset($this->_TextUrl)){ - $this->_TextUrl = $this->Url . '/text'; - } - - return $this->_TextUrl; + return $this->_TextUrl ??= $this->Url . '/text'; } protected function GetTextSinglePageUrl(): string{ - if(!isset($this->_TextSinglePageUrl)){ - $this->_TextSinglePageUrl = $this->Url . '/text/single-page'; - } - - return $this->_TextSinglePageUrl; + return $this->_TextSinglePageUrl ??= $this->Url . '/text/single-page'; } protected function GetTextSinglePageSizeFormatted(): string{ diff --git a/lib/EbookTag.php b/lib/EbookTag.php index 138a3184..4f907281 100644 --- a/lib/EbookTag.php +++ b/lib/EbookTag.php @@ -10,11 +10,7 @@ class EbookTag extends Tag{ // ******* protected function GetUrl(): string{ - if(!isset($this->_Url)){ - $this->_Url = '/subjects/' . $this->UrlName; - } - - return $this->_Url; + return $this->_Url ??= '/subjects/' . $this->UrlName; } diff --git a/lib/NewsletterSubscription.php b/lib/NewsletterSubscription.php index 10d1f16b..9b4f9041 100644 --- a/lib/NewsletterSubscription.php +++ b/lib/NewsletterSubscription.php @@ -23,11 +23,7 @@ class NewsletterSubscription{ // ******* protected function GetUrl(): string{ - if(!isset($this->_Url)){ - $this->_Url = '/newsletter/subscriptions/' . $this->User->Uuid; - } - - return $this->_Url; + return $this->_Url ??= '/newsletter/subscriptions/' . $this->User->Uuid; } diff --git a/lib/OpdsAcquisitionFeed.php b/lib/OpdsAcquisitionFeed.php index 9c775b5e..fc38ba65 100644 --- a/lib/OpdsAcquisitionFeed.php +++ b/lib/OpdsAcquisitionFeed.php @@ -13,10 +13,6 @@ class OpdsAcquisitionFeed extends OpdsFeed{ // ******* protected function GetXmlString(): string{ - if(!isset($this->_XmlString)){ - $this->_XmlString = $this->CleanXmlString(Template::OpdsAcquisitionFeed(['id' => $this->Id, 'url' => $this->Url, 'title' => $this->Title, 'parentUrl' => $this->Parent ? $this->Parent->Url : null, 'updated' => $this->Updated, 'isCrawlable' => $this->IsCrawlable, 'subtitle' => $this->Subtitle, 'entries' => $this->Entries])); - } - - return $this->_XmlString; + return $this->_XmlString ??= $this->CleanXmlString(Template::OpdsAcquisitionFeed(['id' => $this->Id, 'url' => $this->Url, 'title' => $this->Title, 'parentUrl' => $this->Parent ? $this->Parent->Url : null, 'updated' => $this->Updated, 'isCrawlable' => $this->IsCrawlable, 'subtitle' => $this->Subtitle, 'entries' => $this->Entries])); } } diff --git a/lib/OpdsNavigationFeed.php b/lib/OpdsNavigationFeed.php index f6a0c4da..5287e2fb 100644 --- a/lib/OpdsNavigationFeed.php +++ b/lib/OpdsNavigationFeed.php @@ -38,10 +38,6 @@ class OpdsNavigationFeed extends OpdsFeed{ // ******* protected function GetXmlString(): string{ - if(!isset($this->_XmlString)){ - $this->_XmlString = $this->CleanXmlString(Template::OpdsNavigationFeed(['id' => $this->Id, 'url' => $this->Url, 'title' => $this->Title, 'parentUrl' => $this->Parent ? $this->Parent->Url : null, 'updated' => $this->Updated, 'subtitle' => $this->Subtitle, 'entries' => $this->Entries])); - } - - return $this->_XmlString; + return $this->_XmlString ??= $this->CleanXmlString(Template::OpdsNavigationFeed(['id' => $this->Id, 'url' => $this->Url, 'title' => $this->Title, 'parentUrl' => $this->Parent ? $this->Parent->Url : null, 'updated' => $this->Updated, 'subtitle' => $this->Subtitle, 'entries' => $this->Entries])); } } diff --git a/lib/Patron.php b/lib/Patron.php index d540aa4e..4eaa31f8 100644 --- a/lib/Patron.php +++ b/lib/Patron.php @@ -26,17 +26,13 @@ class Patron{ // ******* protected function GetLastPayment(): ?Payment{ - if(!isset($this->_LastPayment)){ - $this->_LastPayment = Db::Query(' + return $this->_LastPayment ??= Db::Query(' SELECT * from Payments where UserId = ? order by Created desc limit 1 ', [$this->UserId], Payment::class)[0] ?? null; - } - - return $this->_LastPayment; } diff --git a/lib/Payment.php b/lib/Payment.php index 911bef49..c6d59145 100644 --- a/lib/Payment.php +++ b/lib/Payment.php @@ -30,11 +30,7 @@ class Payment{ * @throws Exceptions\UserNotFoundException */ protected function GetUser(): ?User{ - if(!isset($this->_User) && $this->UserId !== null){ - $this->_User = User::Get($this->UserId); - } - - return $this->_User; + return $this->_User ??= User::Get($this->UserId); } protected function GetProcessorUrl(): string{ diff --git a/lib/Poll.php b/lib/Poll.php index 771be151..e572d6d0 100644 --- a/lib/Poll.php +++ b/lib/Poll.php @@ -31,40 +31,28 @@ class Poll{ // ******* protected function GetUrl(): string{ - if(!isset($this->_Url)){ - $this->_Url = '/polls/' . $this->UrlName; - } - - return $this->_Url; + return $this->_Url ??= '/polls/' . $this->UrlName; } protected function GetVoteCount(): int{ - if(!isset($this->_VoteCount)){ - $this->_VoteCount = Db::QueryInt(' + return $this->_VoteCount ??= Db::QueryInt(' SELECT count(*) from PollVotes pv inner join PollItems pi using (PollItemId) where pi.PollId = ? ', [$this->PollId]); - } - - return $this->_VoteCount; } /** * @return array */ protected function GetPollItems(): array{ - if(!isset($this->_PollItems)){ - $this->_PollItems = Db::Query(' + return $this->_PollItems ??= Db::Query(' SELECT * from PollItems where PollId = ? order by SortOrder asc ', [$this->PollId], PollItem::class); - } - - return $this->_PollItems; } /** diff --git a/lib/PollItem.php b/lib/PollItem.php index 35ec487d..5f852107 100644 --- a/lib/PollItem.php +++ b/lib/PollItem.php @@ -20,16 +20,12 @@ class PollItem{ // ******* protected function GetVoteCount(): int{ - if(!isset($this->_VoteCount)){ - $this->_VoteCount = Db::QueryInt(' + return $this->_VoteCount ??= Db::QueryInt(' SELECT count(*) from PollVotes pv inner join PollItems pi using (PollItemId) where pi.PollItemId = ? ', [$this->PollItemId]); - } - - return $this->_VoteCount; } diff --git a/lib/PollVote.php b/lib/PollVote.php index 39da3c0c..a5cdb42b 100644 --- a/lib/PollVote.php +++ b/lib/PollVote.php @@ -24,11 +24,7 @@ class PollVote{ // ******* protected function GetUrl(): string{ - if(!isset($this->_Url)){ - $this->_Url = $this->PollItem->Poll->Url . '/votes/' . $this->UserId; - } - - return $this->_Url; + return $this->_Url ??= $this->PollItem->Poll->Url . '/votes/' . $this->UserId; } diff --git a/lib/Project.php b/lib/Project.php index 5270b327..8af0719d 100644 --- a/lib/Project.php +++ b/lib/Project.php @@ -115,19 +115,11 @@ final class Project{ } protected function GetUrl(): string{ - if(!isset($this->_Url)){ - $this->_Url = '/projects/' . $this->ProjectId; - } - - return $this->_Url; + return $this->_Url ??= '/projects/' . $this->ProjectId; } protected function GetEditUrl(): string{ - if(!isset($this->_EditUrl)){ - $this->_EditUrl = $this->Url . '/edit'; - } - - return $this->_EditUrl; + return $this->_EditUrl ??= $this->Url . '/edit'; } protected function GetLastActivityTimestamp(): DateTimeImmutable{ @@ -150,33 +142,21 @@ final class Project{ * @throws Exceptions\UserNotFoundException If the `User` can't be found. */ protected function GetManager(): User{ - if(!isset($this->_Manager)){ - $this->_Manager = User::Get($this->ManagerUserId); - } - - return $this->_Manager; + return $this->_Manager ??= User::Get($this->ManagerUserId); } /** * @throws Exceptions\UserNotFoundException If the `User` can't be found. */ protected function GetReviewer(): User{ - if(!isset($this->_Reviewer)){ - $this->_Reviewer = User::Get($this->ReviewerUserId); - } - - return $this->_Reviewer; + return $this->_Reviewer ??= User::Get($this->ReviewerUserId); } /** * @return array */ protected function GetReminders(): array{ - if(!isset($this->_Reminders)){ - $this->_Reminders = Db::Query('SELECT * from ProjectReminders where ProjectId = ? order by Created asc', [$this->ProjectId], ProjectReminder::class); - } - - return $this->_Reminders; + return $this->_Reminders ??= Db::Query('SELECT * from ProjectReminders where ProjectId = ? order by Created asc', [$this->ProjectId], ProjectReminder::class); } diff --git a/lib/RssFeed.php b/lib/RssFeed.php index c0199b02..fd4607a4 100644 --- a/lib/RssFeed.php +++ b/lib/RssFeed.php @@ -21,13 +21,7 @@ class RssFeed extends Feed{ // ******* protected function GetXmlString(): string{ - if(!isset($this->_XmlString)){ - $feed = Template::RssFeed(['url' => $this->Url, 'description' => $this->Description, 'title' => $this->Title, 'entries' => $this->Entries, 'updated' => NOW]); - - $this->_XmlString = $this->CleanXmlString($feed); - } - - return $this->_XmlString; + return $this->_XmlString ??= $this->CleanXmlString(Template::RssFeed(['url' => $this->Url, 'description' => $this->Description, 'title' => $this->Title, 'entries' => $this->Entries, 'updated' => NOW])); } public function SaveIfChanged(): bool{ diff --git a/lib/Session.php b/lib/Session.php index 320be09c..006b240c 100644 --- a/lib/Session.php +++ b/lib/Session.php @@ -24,11 +24,7 @@ class Session{ // ******* protected function GetUrl(): string{ - if(!isset($this->_Url)){ - $this->_Url = '/sessions/' . $this->SessionId; - } - - return $this->_Url; + return $this->_Url ??= '/sessions/' . $this->SessionId; } diff --git a/lib/Template.php b/lib/Template.php index b3ef7960..5f80998d 100644 --- a/lib/Template.php +++ b/lib/Template.php @@ -4,8 +4,8 @@ use function Safe\ob_start; class Template{ /** - * @param array $arguments - */ + * @param array $arguments + */ protected static function Get(string $templateName, array $arguments = []): string{ // Expand the passed variables to make them available to the included template. // We use these funny names so that we can use 'name' and 'value' as template variables if we want to. @@ -22,8 +22,8 @@ class Template{ } /** - * @param array $arguments - */ + * @param array $arguments + */ public static function __callStatic(string $function, array $arguments): string{ if(isset($arguments[0]) && is_array($arguments[0])){ return self::Get($function, $arguments[0]); diff --git a/lib/User.php b/lib/User.php index 1cd9cd84..18e196ea 100644 --- a/lib/User.php +++ b/lib/User.php @@ -116,49 +116,33 @@ class User{ } protected function GetUrl(): string{ - if(!isset($this->_Url)){ - $this->_Url = '/users/' . $this->UserId; - } - - return $this->_Url; + return $this->_Url ??= '/users/' . $this->UserId; } protected function GetEditUrl(): string{ - if(!isset($this->_EditUrl)){ - $this->_EditUrl = $this->Url . '/edit'; - } - - return $this->_EditUrl; + return $this->_EditUrl ??= $this->Url . '/edit'; } /** * @return array */ protected function GetPayments(): array{ - if(!isset($this->_Payments)){ - $this->_Payments = Db::Query(' + return $this->_Payments ??= Db::Query(' SELECT * from Payments where UserId = ? order by Created desc ', [$this->UserId], Payment::class); - } - - return $this->_Payments; } protected function GetLastPayment(): ?Payment{ - if(!isset($this->_LastPayment)){ - $this->_LastPayment = Db::Query(' + return $this->_LastPayment ??= Db::Query(' SELECT * from Payments where UserId = ? order by Created desc limit 1 ', [$this->UserId], Payment::class)[0] ?? null; - } - - return $this->_LastPayment; } protected function GetBenefits(): Benefits{