Use shorthand assignment for basic getters

This commit is contained in:
Alex Cabal 2025-02-27 16:03:26 -06:00
parent 99b5fd66f2
commit 7f5ffb4aea
21 changed files with 142 additions and 326 deletions

View file

@ -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){

View file

@ -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<ArtworkTag>
*/
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('Couldnt find an ebook with that URL.'));
}
}
else{
$this->EbookUrl = null;
}
// Check for existing `Artwork` objects with the same URL but different `ArtworkID`s.
try{

View file

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

View file

@ -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<Ebook>
*/
protected function GetEbooks(): array{
if(!isset($this->_Ebooks)){
$this->_Ebooks = Ebook::GetAllByCollection($this->CollectionId);
}
return $this->_Ebooks;
return $this->_Ebooks ??= Ebook::GetAllByCollection($this->CollectionId);
}

View file

@ -73,7 +73,6 @@ class Contributor{
$error->Add(new Exceptions\ContributorEbookIdRequiredException());
}
$this->Name = trim($this->Name ?? '');
if($this->Name == ''){
$error->Add(new Exceptions\ContributorNameRequiredException());

View file

@ -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{

View file

@ -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<Project>
*/
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<GitCommit>
*/
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<EbookTag>
*/
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<LocSubject>
*/
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<CollectionMembership>
*/
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<EbookSource>
*/
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{

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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{

View file

@ -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<PollItem>
*/
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;
}
/**

View file

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

View file

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

View file

@ -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<ProjectReminder>
*/
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);
}

View file

@ -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{

View file

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

View file

@ -4,8 +4,8 @@ use function Safe\ob_start;
class Template{
/**
* @param array<mixed> $arguments
*/
* @param array<mixed> $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<mixed> $arguments
*/
* @param array<mixed> $arguments
*/
public static function __callStatic(string $function, array $arguments): string{
if(isset($arguments[0]) && is_array($arguments[0])){
return self::Get($function, $arguments[0]);

View file

@ -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<Payment>
*/
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{