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;
/**
* @property string $Url
*/
class Collection{
use Traits\Accessor;
public string $Name;
public string $UrlName;
public string $Url;
public ?int $SequenceNumber = null;
public ?string $Type = null;
protected ?string $_Url = null;
public function __construct(string $name){
$this->Name = $name;
$this->UrlName = Formatter::MakeUrlSafe($this->Name);
protected function GetUrl(): ?string{
if($this->_Url === null){
$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{
return preg_replace('/^(the|and|a|)\s/ius', '', $this->Name);
}

View file

@ -8,13 +8,15 @@ class Contributor{
public ?string $FullName = 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){
$this->Name = str_replace('\'', '', $name);
$this->UrlName = Formatter::MakeUrlSafe($name);
$this->SortName = $sortName;
$this->FullName = $fullName;
$this->WikipediaUrl = $wikipediaUrl;
$this->MarcRole = $marcRole;
$this->NacoafUrl = $nacoafUrl;
public static function FromProperties(string $name, string $sortName = null, string $fullName = null, string $wikipediaUrl = null, string $marcRole = null, string $nacoafUrl = null): Contributor{
$instance = new Contributor();
$instance->Name = str_replace('\'', '', $name);
$instance->UrlName = Formatter::MakeUrlSafe($name);
$instance->SortName = $sortName;
$instance->FullName = $fullName;
$instance->WikipediaUrl = $wikipediaUrl;
$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 string $Url;
public function __construct(EbookSourceType $type, string $url){
$this->Type = $type;
$this->Url = $url;
public static function FromTypeAndUrl(EbookSourceType $type, string $url): EbookSource{
$instance = new EbookSource();
$instance->Type = $type;
$instance->Url = $url;
return $instance;
}
}

View file

@ -9,14 +9,16 @@ class GitCommit{
/**
* @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{
$this->Created = new DateTimeImmutable('@' . $unixTimestamp);
$instance->Created = new DateTimeImmutable('@' . $unixTimestamp);
}
catch(\Exception){
throw new Exceptions\InvalidGitCommitException('Invalid timestamp for Git commit.');
}
$this->Message = $message;
$this->Hash = $hash;
$instance->Message = $message;
$instance->Hash = $hash;
return $instance;
}
}

View file

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

View file

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

View file

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

View file

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