Throw dedicated exceptions in MDNodaRepository

This commit is contained in:
Joshua Ramon Enslin 2023-10-15 18:30:18 +02:00
parent d6e3c3208b
commit 68f453f5f3
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
2 changed files with 60 additions and 10 deletions

View File

@ -65,9 +65,11 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable {
'GND' => self::gnd, 'GND' => self::gnd,
'o-gnd' => self::gnd, 'o-gnd' => self::gnd,
'O-GND' => self::gnd, 'O-GND' => self::gnd,
'pnd' => self::gnd,
'http://d-nb.info/gnd' => self::gnd, 'http://d-nb.info/gnd' => self::gnd,
'http://d-nb.info/gnd/' => self::gnd, 'http://d-nb.info/gnd/' => self::gnd,
'd-nb.info' => self::gnd, 'd-nb.info' => self::gnd,
'https://portal.dnb.de' => self::gnd,
'grobsystematik' => self::grobsystematik, 'grobsystematik' => self::grobsystematik,
'iconclass' => self::iconclass, 'iconclass' => self::iconclass,
'klbb' => self::klbb, 'klbb' => self::klbb,
@ -92,6 +94,7 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable {
'pleiades' => self::pleiades, 'pleiades' => self::pleiades,
'rkd' => self::rkd, 'rkd' => self::rkd,
'ulan' => self::ulan, 'ulan' => self::ulan,
'ULAN' => self::ulan,
'viaf' => self::viaf, 'viaf' => self::viaf,
'wikidata' => self::wikidata, 'wikidata' => self::wikidata,
'Wikidata' => self::wikidata, 'Wikidata' => self::wikidata,
@ -377,7 +380,10 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable {
else if (substr($id, 0, 1) === 'n') { else if (substr($id, 0, 1) === 'n') {
if (filter_var(trim(substr($id, 1), '0'), FILTER_VALIDATE_INT) === false) return false; if (filter_var(trim(substr($id, 1), '0'), FILTER_VALIDATE_INT) === false) return false;
} }
else throw new MDgenericInvalidInputsException("LOC IDs must start with n or nr or nb (provided: " . strip_tags($id) . ")"); else if (substr($id, 0, 2) === 'sh') {
throw new MDInvalidNodaLinkLocIdIsSh("The link to the Library of Congress authority files refers to the LOC Subject Headings. The subject headings are listed as a separate vocabulary.");
}
else throw new MDInvalidNodaLinkException("LOC IDs must start with n or nr or nb (provided: " . strip_tags($id) . ")");
return (string)$id; return (string)$id;
@ -396,11 +402,14 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable {
$id = strtr($id, [ $id = strtr($id, [
'http://id.loc.gov/authorities/names/' => '', 'http://id.loc.gov/authorities/names/' => '',
'https://id.loc.gov/authorities/names/' => '', 'https://id.loc.gov/authorities/names/' => '',
'http://id.loc.gov/authorities/subjects/' => '',
'https://id.loc.gov/authorities/subjects/' => '',
'.html' => '',
]); ]);
} }
if (substr($id, 0, 2) !== 'sh') { if (substr($id, 0, 2) !== 'sh') {
throw new MDgenericInvalidInputsException("LCSH IDs must start with sh"); throw new MDInvalidNodaLinkException("LCSH IDs must start with sh");
} }
if (filter_var(ltrim(substr($id, 2), "0"), FILTER_VALIDATE_INT) === false) { if (filter_var(ltrim(substr($id, 2), "0"), FILTER_VALIDATE_INT) === false) {
@ -443,6 +452,31 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable {
} }
/**
* Validates an NPG ID, returning a string or false.
*
* @param string $id ID to validate.
*
* @return string|false
*/
private static function validateNpgId(string $id):string|false {
if (filter_var($id, FILTER_VALIDATE_URL) !== false) {
$id = strtr($id, ['https://www.npg.org.uk/collections/search/person/' => '']);
}
if (substr($id, 0, 2) === 'mp' && is_numeric(substr($id, 2))) {
return $id;
}
if (filter_var($id, FILTER_VALIDATE_INT) === false) {
return false;
}
return $id;
}
/** /**
* Validates a wikidata ID, returning a string or false. * Validates a wikidata ID, returning a string or false.
* *
@ -498,10 +532,17 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable {
public static function validateWikipediaId(string $id):string|false { public static function validateWikipediaId(string $id):string|false {
$validation = strtr($id, [ $validation = strtr($id, [
'http://de.wikipedia.org/wiki/' => '',
'https://de.wikipedia.org/wiki/' => '', 'https://de.wikipedia.org/wiki/' => '',
'http://en.wikipedia.org/wiki/' => '',
'https://en.wikipedia.org/wiki/' => '', 'https://en.wikipedia.org/wiki/' => '',
'http://fr.wikipedia.org/wiki/' => '',
'https://fr.wikipedia.org/wiki/' => '', 'https://fr.wikipedia.org/wiki/' => '',
'http://nl.wikipedia.org/wiki/' => '',
'https://nl.wikipedia.org/wiki/' => '',
'http://sv.wikipedia.org/wiki/' => '',
'https://sv.wikipedia.org/wiki/' => '', 'https://sv.wikipedia.org/wiki/' => '',
'http://ru.wikipedia.org/wiki/' => '',
'https://ru.wikipedia.org/wiki/' => '', 'https://ru.wikipedia.org/wiki/' => '',
]); ]);
@ -522,7 +563,12 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable {
return match($this) { return match($this) {
self::allgemein => filter_var($id, FILTER_VALIDATE_URL), self::allgemein => filter_var($id, FILTER_VALIDATE_URL),
self::aat => self::validateNumericId($id, ['https://vocab.getty.edu/page/aat/']), self::aat => self::validateNumericId($id, [
'http://vocab.getty.edu/aat/',
'http://vocab.getty.edu/page/aat/',
'https://vocab.getty.edu/aat/',
'https://vocab.getty.edu/page/aat/',
]),
self::ackerbau => self::validateNumericId($id, ['https://term.museum-digital.de/ackerbau/tag/']), self::ackerbau => self::validateNumericId($id, ['https://term.museum-digital.de/ackerbau/tag/']),
self::bne => self::validateBneId($id, ['http://datos.bne.es/persona/']), self::bne => self::validateBneId($id, ['http://datos.bne.es/persona/']),
self::bnf => self::validateBnfId($id), self::bnf => self::validateBnfId($id),
@ -537,7 +583,7 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable {
self::mbl => self::validateNumericId($id, ['http://www.uni-magdeburg.de/mbl/PHP_Skripte/mbl_verwaltung/mbl_verw_anzeige_biog.php?auswahl=3&liste_biog_name=']), self::mbl => self::validateNumericId($id, ['http://www.uni-magdeburg.de/mbl/PHP_Skripte/mbl_verwaltung/mbl_verw_anzeige_biog.php?auswahl=3&liste_biog_name=']),
self::mindatorg => self::validateNumericId($id, ['https://www.mindat.org/min-', '.html']), self::mindatorg => self::validateNumericId($id, ['https://www.mindat.org/min-', '.html']),
self::moebeltypologie => self::validateNumericId($id, ['https://term.museum-digital.de/moebel/tag/']), self::moebeltypologie => self::validateNumericId($id, ['https://term.museum-digital.de/moebel/tag/']),
self::ndb_adb => self::validateGndId($id, ['https://www.deutsche-biographie.de/pnd', '.html']), self::ndb_adb => self::validateGndId($id, ['https://www.deutsche-biographie.de/pnd', '.html', '#adbcontent', '#ndbcontent']),
self::ndl => self::validateNumericId($id, [ self::ndl => self::validateNumericId($id, [
'http://id.ndl.go.jp/auth/ndlna/', 'http://id.ndl.go.jp/auth/ndlna/',
'https://id.ndl.go.jp/auth/ndlna/', 'https://id.ndl.go.jp/auth/ndlna/',
@ -545,17 +591,20 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable {
self::ndp_ikmk => self::validateNumericId($id, ['https://ikmk.smb.museum/ndp/land/']), self::ndp_ikmk => self::validateNumericId($id, ['https://ikmk.smb.museum/ndp/land/']),
self::ndp_ikmk_persons => self::validateNumericId($id, ['https://ikmk.smb.museum/ndp/person/']), self::ndp_ikmk_persons => self::validateNumericId($id, ['https://ikmk.smb.museum/ndp/person/']),
self::nomisma => str_replace('http://nomisma.org/id/', '', $id), self::nomisma => str_replace('http://nomisma.org/id/', '', $id),
self::npg => self::validateNumericId($id, [ self::npg => self::validateNpgId($id),
'https://www.npg.org.uk/collections/search/person/',
'https://www.npg.org.uk/collections/search/person/mp',
]),
self::oberbegriffsdatei => self::validateNumericId($id, ['https://term.museum-digital.de/oberbegriffsdatei/tag/']), self::oberbegriffsdatei => self::validateNumericId($id, ['https://term.museum-digital.de/oberbegriffsdatei/tag/']),
self::orcid => preg_match('/^[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}$/', $id) ? $id : false, self::orcid => preg_match('/^[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}$/', $id) ? $id : false,
self::osm => self::validateNumericId($id, ['https://www.openstreetmap.org/relation/']), self::osm => self::validateNumericId($id, ['https://www.openstreetmap.org/relation/']),
self::pim => self::validatePimId($id), self::pim => self::validatePimId($id),
self::pleiades => self::validateNumericId($id, ['https://pleiades.stoa.org/places/']), self::pleiades => self::validateNumericId($id, ['https://pleiades.stoa.org/places/']),
self::rkd => self::validateNumericId($id, ['http://rkd.nl/explore/artists/', 'https://rkd.nl/explore/artists/']), self::rkd => self::validateNumericId($id, ['http://rkd.nl/explore/artists/', 'https://rkd.nl/explore/artists/']),
self::ulan => self::validateNumericId($id, ['http://vocab.getty.edu/ulan/', 'http://vocab.getty.edu/page/ulan/', 'https://vocab.getty.edu/page/ulan/']), self::ulan => self::validateNumericId($id, [
'http://vocab.getty.edu/ulan/',
'http://vocab.getty.edu/page/ulan/',
'https://vocab.getty.edu/page/ulan/',
'http://www.getty.edu/vow/ULANFullDisplay?find=&role=&nation=&subjectid=',
'https://www.getty.edu/vow/ULANFullDisplay?find=&role=&nation=&subjectid=',
]),
self::viaf => self::validateNumericId($id, [ self::viaf => self::validateNumericId($id, [
'https://viaf.org/viaf/', 'https://viaf.org/viaf/',
'http://viaf.org/viaf/', 'http://viaf.org/viaf/',

View File

@ -35,7 +35,7 @@ final class MDNodaRepositoryTest extends TestCase {
self::assertEquals("00967046", MDNodaRepository::ndl->validateId("http://id.ndl.go.jp/auth/ndlna/00967046")); self::assertEquals("00967046", MDNodaRepository::ndl->validateId("http://id.ndl.go.jp/auth/ndlna/00967046"));
// NPG: National Portrait Gallery // NPG: National Portrait Gallery
self::assertEquals('01751', MDNodaRepository::npg->validateId("https://www.npg.org.uk/collections/search/person/mp01751")); self::assertEquals('mp01751', MDNodaRepository::npg->validateId("https://www.npg.org.uk/collections/search/person/mp01751"));
// Library of Congress // Library of Congress
self::assertEquals("n2022014604", MDNodaRepository::loc->validateId("https://id.loc.gov/authorities/names/n2022014604")); self::assertEquals("n2022014604", MDNodaRepository::loc->validateId("https://id.loc.gov/authorities/names/n2022014604"));
@ -45,6 +45,7 @@ final class MDNodaRepositoryTest extends TestCase {
self::assertEquals("sh2022014604", MDNodaRepository::lcsh->validateId("https://id.loc.gov/authorities/names/sh2022014604")); self::assertEquals("sh2022014604", MDNodaRepository::lcsh->validateId("https://id.loc.gov/authorities/names/sh2022014604"));
self::assertEquals("sh2022014604", MDNodaRepository::lcsh->validateId("http://id.loc.gov/authorities/names/sh2022014604")); self::assertEquals("sh2022014604", MDNodaRepository::lcsh->validateId("http://id.loc.gov/authorities/names/sh2022014604"));
self::assertEquals("sh2022014604", MDNodaRepository::lcsh->validateId("sh2022014604")); self::assertEquals("sh2022014604", MDNodaRepository::lcsh->validateId("sh2022014604"));
self::assertEquals("sh85081569", MDNodaRepository::lcsh->validateId("http://id.loc.gov/authorities/subjects/sh85081569.html"));
self::assertEquals("PIM71684", MDNodaRepository::pim->validateId("PIM71684")); self::assertEquals("PIM71684", MDNodaRepository::pim->validateId("PIM71684"));
self::assertEquals("PIM71684", MDNodaRepository::pim->validateId("https://opac-nevter.pim.hu/en/record/-/record/PIM71684")); self::assertEquals("PIM71684", MDNodaRepository::pim->validateId("https://opac-nevter.pim.hu/en/record/-/record/PIM71684"));