mirror of
https://github.com/standardebooks/web.git
synced 2025-07-15 19:06:49 -04:00
Add cover art database
Co-authored-by: Job Curtis <job.curtis@gmail.com> Co-authored-by: Alex Cabal <alex@standardebooks.org>
This commit is contained in:
parent
74f747df76
commit
6a5c05511a
92 changed files with 3174 additions and 146 deletions
153
lib/Artist.php
Normal file
153
lib/Artist.php
Normal file
|
@ -0,0 +1,153 @@
|
|||
<?
|
||||
use Safe\DateTime;
|
||||
use function Safe\date;
|
||||
|
||||
/**
|
||||
* @property string $UrlName
|
||||
* @property array<string> $AlternateSpellings
|
||||
*/
|
||||
class Artist extends PropertiesBase{
|
||||
public $ArtistId;
|
||||
public $Name;
|
||||
public $DeathYear;
|
||||
public $Created;
|
||||
public $Updated;
|
||||
protected $_UrlName;
|
||||
protected $_AlternateSpellings;
|
||||
|
||||
// *******
|
||||
// GETTERS
|
||||
// *******
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
protected function GetUrlName(): string{
|
||||
if($this->Name === null || $this->Name == ''){
|
||||
return '';
|
||||
}
|
||||
|
||||
if($this->_UrlName === null){
|
||||
$this->_UrlName = Formatter::MakeUrlSafe($this->Name);
|
||||
}
|
||||
|
||||
return $this->_UrlName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string>
|
||||
*/
|
||||
protected function GetAlternateSpellings(): array{
|
||||
if($this->_AlternateSpellings === null){
|
||||
$this->_AlternateSpellings = [];
|
||||
|
||||
$result = Db::Query('
|
||||
SELECT *
|
||||
from ArtistAlternateSpellings
|
||||
where ArtistId = ?
|
||||
', [$this->ArtistId], 'stdClass');
|
||||
|
||||
foreach($result as $row){
|
||||
$this->_AlternateSpellings[] = $row->AlternateSpelling;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->_AlternateSpellings;
|
||||
}
|
||||
|
||||
// *******
|
||||
// METHODS
|
||||
// *******
|
||||
|
||||
public function Validate(): void{
|
||||
$error = new Exceptions\ValidationException();
|
||||
|
||||
if($this->Name === null || $this->Name == ''){
|
||||
$error->Add(new Exceptions\ArtistNameRequiredException());
|
||||
}
|
||||
|
||||
if($this->Name !== null && strlen($this->Name) > COVER_ARTWORK_MAX_STRING_LENGTH){
|
||||
$error->Add(new Exceptions\StringTooLongException('Artist Name'));
|
||||
}
|
||||
|
||||
if($this->DeathYear !== null && ($this->DeathYear <= 0 || $this->DeathYear > intval(date('Y')))){
|
||||
$error->Add(new Exceptions\InvalidDeathYearException());
|
||||
}
|
||||
|
||||
if($error->HasExceptions){
|
||||
throw $error;
|
||||
}
|
||||
}
|
||||
// ***********
|
||||
// ORM METHODS
|
||||
// ***********
|
||||
|
||||
public static function Get(?int $artistId): Artist{
|
||||
if($artistId === null){
|
||||
throw new Exceptions\InvalidArtistException();
|
||||
}
|
||||
|
||||
$result = Db::Query('
|
||||
SELECT *
|
||||
from Artists
|
||||
where ArtistId = ?
|
||||
', [$artistId], 'Artist');
|
||||
|
||||
if(sizeof($result) == 0){
|
||||
throw new Exceptions\InvalidArtistException();
|
||||
}
|
||||
|
||||
return $result[0];
|
||||
}
|
||||
|
||||
public function Create(): void{
|
||||
$this->Validate();
|
||||
Db::Query('
|
||||
INSERT into Artists (Name, UrlName, DeathYear)
|
||||
values (?,
|
||||
?,
|
||||
?)
|
||||
', [$this->Name, $this->UrlName, $this->DeathYear]);
|
||||
|
||||
$this->ArtistId = Db::GetLastInsertedId();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exceptions\ValidationException
|
||||
*/
|
||||
public static function GetOrCreate(Artist $artist): Artist{
|
||||
$result = Db::Query('
|
||||
SELECT *
|
||||
from Artists
|
||||
where UrlName = ?
|
||||
', [$artist->UrlName], 'Artist');
|
||||
|
||||
if(isset($result[0])){
|
||||
return $result[0];
|
||||
}
|
||||
else{
|
||||
$artist->Create();
|
||||
return $artist;
|
||||
}
|
||||
}
|
||||
|
||||
public static function FindMatch(string $artistName): ?Artist{
|
||||
$result = Db::Query('
|
||||
SELECT a.*
|
||||
from Artists a left join ArtistAlternateSpellings alt using (ArtistId)
|
||||
where a.Name = ? or alt.AlternateSpelling = ?
|
||||
order by a.DeathYear desc
|
||||
limit 1;
|
||||
', [$artistName, $artistName], 'Artist');
|
||||
|
||||
return $result[0] ?? null;
|
||||
}
|
||||
|
||||
public function Delete(): void{
|
||||
Db::Query('
|
||||
DELETE
|
||||
from Artists
|
||||
where ArtistId = ?
|
||||
', [$this->ArtistId]);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue