web/lib/EbookSource.php
Mike Colagrosso 1076c9a77d Remove primary key EbookSourceId
There is a one to many relationship between `Ebook` and `EbookSource`, and we
don't query by `EbookSourceId`. We add a `SortOrder` column to keep the sources
in the same order as specified in `content.opf`.
2024-11-04 13:16:56 -06:00

64 lines
1.3 KiB
PHP

<?
use Safe\DateTimeImmutable;
class EbookSource{
public ?int $EbookId = null;
public EbookSourceType $Type;
public string $Url;
public ?int $SortOrder = null;
public static function FromTypeAndUrl(EbookSourceType $type, string $url): EbookSource{
$instance = new EbookSource();
$instance->Type = $type;
$instance->Url = $url;
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\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(!isset($this->SortOrder)){
$error->Add(new Exceptions\EbookSourceSortOrderRequiredException());
}
if($error->HasExceptions){
throw $error;
}
}
/**
* @throws Exceptions\ValidationException
*/
public function Create(): void{
$this->Validate();
Db::Query('
INSERT into EbookSources (EbookId, Type, Url, SortOrder)
values (?,
?,
?,
?)
', [$this->EbookId, $this->Type, $this->Url, $this->SortOrder]);
}
}