Rename the constructor Ebook::__construct() to static Ebook::FromFilesystem()

Also added `GetFoo()` methods for all the derived properties like `GetUrl()`, `GetHasDownloads()`, etc. Removed that logic from the constructor so that it's reusable in `Ebook::FromFilesystem()` and `Ebook::GetByIdentifier()`
This commit is contained in:
Mike Colagrosso 2024-05-20 22:33:16 -06:00 committed by Alex Cabal
parent f605a4df60
commit 2098b265a8
9 changed files with 656 additions and 279 deletions

View file

@ -1,19 +1,33 @@
<? <?
use function Safe\preg_replace; use function Safe\preg_replace;
/**
* @property string $Url
*/
class Collection{ class Collection{
use Traits\Accessor;
public string $Name; public string $Name;
public string $UrlName; public string $UrlName;
public string $Url;
public ?int $SequenceNumber = null; public ?int $SequenceNumber = null;
public ?string $Type = null; public ?string $Type = null;
protected ?string $_Url = null;
public function __construct(string $name){ protected function GetUrl(): ?string{
$this->Name = $name; if($this->_Url === null){
$this->UrlName = Formatter::MakeUrlSafe($this->Name);
$this->Url = '/collections/' . $this->UrlName; $this->Url = '/collections/' . $this->UrlName;
} }
return $this->_Url;
}
public static function FromName(string $name): Collection{
$instance = new Collection();
$instance->Name = $name;
$instance->UrlName = Formatter::MakeUrlSafe($instance->Name);
return $instance;
}
public function GetSortedName(): string{ public function GetSortedName(): string{
return preg_replace('/^(the|and|a|)\s/ius', '', $this->Name); return preg_replace('/^(the|and|a|)\s/ius', '', $this->Name);
} }

View file

@ -8,13 +8,15 @@ class Contributor{
public ?string $FullName = null; public ?string $FullName = null;
public ?string $NacoafUrl = null; public ?string $NacoafUrl = null;
public function __construct(string $name, string $sortName = null, string $fullName = null, string $wikipediaUrl = null, string $marcRole = null, string $nacoafUrl = null){ public static function FromProperties(string $name, string $sortName = null, string $fullName = null, string $wikipediaUrl = null, string $marcRole = null, string $nacoafUrl = null): Contributor{
$this->Name = str_replace('\'', '', $name); $instance = new Contributor();
$this->UrlName = Formatter::MakeUrlSafe($name); $instance->Name = str_replace('\'', '', $name);
$this->SortName = $sortName; $instance->UrlName = Formatter::MakeUrlSafe($name);
$this->FullName = $fullName; $instance->SortName = $sortName;
$this->WikipediaUrl = $wikipediaUrl; $instance->FullName = $fullName;
$this->MarcRole = $marcRole; $instance->WikipediaUrl = $wikipediaUrl;
$this->NacoafUrl = $nacoafUrl; $instance->MarcRole = $marcRole;
$instance->NacoafUrl = $nacoafUrl;
return $instance;
} }
} }

File diff suppressed because it is too large Load diff

View file

@ -3,8 +3,10 @@ class EbookSource{
public EbookSourceType $Type; public EbookSourceType $Type;
public string $Url; public string $Url;
public function __construct(EbookSourceType $type, string $url){ public static function FromTypeAndUrl(EbookSourceType $type, string $url): EbookSource{
$this->Type = $type; $instance = new EbookSource();
$this->Url = $url; $instance->Type = $type;
$instance->Url = $url;
return $instance;
} }
} }

View file

@ -9,14 +9,16 @@ class GitCommit{
/** /**
* @throws Exceptions\InvalidGitCommitException * @throws Exceptions\InvalidGitCommitException
*/ */
public function __construct(string $unixTimestamp, string $hash, string $message){ public static function FromLog(string $unixTimestamp, string $hash, string $message): GitCommit{
$instance = new GitCommit();
try{ try{
$this->Created = new DateTimeImmutable('@' . $unixTimestamp); $instance->Created = new DateTimeImmutable('@' . $unixTimestamp);
} }
catch(\Exception){ catch(\Exception){
throw new Exceptions\InvalidGitCommitException('Invalid timestamp for Git commit.'); throw new Exceptions\InvalidGitCommitException('Invalid timestamp for Git commit.');
} }
$this->Message = $message; $instance->Message = $message;
$this->Hash = $hash; $instance->Hash = $hash;
return $instance;
} }
} }

View file

@ -433,7 +433,7 @@ class Library{
try{ try{
$ebookWwwFilesystemPath = preg_replace('|/content\.opf|ius', '', $path); $ebookWwwFilesystemPath = preg_replace('|/content\.opf|ius', '', $path);
$ebooks[] = new Ebook($ebookWwwFilesystemPath); $ebooks[] = Ebook::FromFilesystem($ebookWwwFilesystemPath);
} }
catch(\Exception){ catch(\Exception){
// An error in a book isn't fatal; just carry on. // An error in a book isn't fatal; just carry on.
@ -704,7 +704,7 @@ class Library{
try{ try{
$ebookWwwFilesystemPath = preg_replace('|/content\.opf|ius', '', $filename); $ebookWwwFilesystemPath = preg_replace('|/content\.opf|ius', '', $filename);
$ebook = new Ebook($ebookWwwFilesystemPath); $ebook = Ebook::FromFilesystem($ebookWwwFilesystemPath);
$ebooks[$ebookWwwFilesystemPath] = $ebook; $ebooks[$ebookWwwFilesystemPath] = $ebook;

View file

@ -1,6 +1,7 @@
<? <?
class LocSubject extends Tag{ class LocSubject{
public int $LocSubjectId; public int $LocSubjectId;
public string $Name;
/** /**
* @throws Exceptions\ValidationException * @throws Exceptions\ValidationException

View file

@ -20,7 +20,7 @@ try{
$ebook = apcu_fetch('ebook-' . $wwwFilesystemPath); $ebook = apcu_fetch('ebook-' . $wwwFilesystemPath);
} }
catch(Safe\Exceptions\ApcuException){ catch(Safe\Exceptions\ApcuException){
$ebook = new Ebook($wwwFilesystemPath); $ebook = Ebook::FromFilesystem($wwwFilesystemPath);
} }
switch($format){ switch($format){

View file

@ -7,7 +7,7 @@ use function Safe\preg_replace;
use function Safe\apcu_fetch; use function Safe\apcu_fetch;
use function Safe\shuffle; use function Safe\shuffle;
$ebook = new Ebook(); $ebook = null;
$transcriptionSources = []; $transcriptionSources = [];
$scanSources = []; $scanSources = [];
$otherSources = []; $otherSources = [];
@ -45,7 +45,7 @@ try{
$ebook = apcu_fetch('ebook-' . $wwwFilesystemPath); $ebook = apcu_fetch('ebook-' . $wwwFilesystemPath);
} }
catch(Safe\Exceptions\ApcuException){ catch(Safe\Exceptions\ApcuException){
$ebook = new Ebook($wwwFilesystemPath); $ebook = Ebook::FromFilesystem($wwwFilesystemPath);
} }
// Divide our sources into transcriptions and scans // Divide our sources into transcriptions and scans