Remove whitespaces from vocabulary links, add dedicated exception for

lcsh links that are actually loc links
This commit is contained in:
Joshua Ramon Enslin 2023-10-16 16:44:33 +02:00
parent da6e40282d
commit 1d1a690e0f
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
4 changed files with 31 additions and 2 deletions

View File

@ -0,0 +1,20 @@
<?PHP
declare(strict_types = 1);
/**
* Exception thrown in case a library of congress subject headings link refers
* to the other authority files of the Library of Congress.
*/
final class MDInvalidNodaLinkLcshIdIsGeneralLoc extends MDInvalidNodaLinkException {
/**
* Error message.
*
* @return string
*/
public function errorMessage() {
//error message
return 'Inputs are not valid.';
}
}

View File

@ -36,7 +36,7 @@ final class MDNodaLink {
public function __construct(MDNodaRepository $source, string $id) {
$this->source = $source;
if (($validatedId = $this->source->validateId(trim($id))) === false) {
if (($validatedId = $this->source->validateId(strtr(trim($id), [" " => "", "\t" => "", "\n" => ""]))) === false) {
throw new MDInvalidNodaLink("Invalid noda ID / link: \"" . $id . "\" [Repository: " . $this->source->toDbName() . "]");
}
$this->id = $validatedId;

View File

@ -351,6 +351,7 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable {
return false;
}
if (is_numeric(strtr($id, ['-' => '', 'X' => ''])) === false) {
throw new Exception($id);
return false;
}
@ -409,7 +410,10 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable {
}
if (substr($id, 0, 2) !== 'sh') {
throw new MDInvalidNodaLinkException("LCSH IDs must start with sh");
if (str_starts_with(substr($id, 0, 1), 'n')) {
throw new MDInvalidNodaLinkLcshIdIsGeneralLoc("LCSH IDs must start with sh. ID started with n. Use general LOC link instead.");
}
throw new MDInvalidNodaLinkException("LCSH IDs must start with sh. ID provided is: " . $id);
}
if (filter_var(ltrim(substr($id, 2), "0"), FILTER_VALIDATE_INT) === false) {
@ -538,6 +542,10 @@ enum MDNodaRepository implements MDValueEnumInterface, JsonSerializable {
'https://en.wikipedia.org/wiki/' => '',
'http://fr.wikipedia.org/wiki/' => '',
'https://fr.wikipedia.org/wiki/' => '',
'http://id.wikipedia.org/wiki/' => '',
'https://id.wikipedia.org/wiki/' => '',
'http://it.wikipedia.org/wiki/' => '',
'https://it.wikipedia.org/wiki/' => '',
'http://nl.wikipedia.org/wiki/' => '',
'https://nl.wikipedia.org/wiki/' => '',
'http://sv.wikipedia.org/wiki/' => '',

View File

@ -27,6 +27,7 @@ final class MDNodaRepositoryTest extends TestCase {
// GND (Germany)
self::assertEquals("102423008", MDNodaRepository::gnd->validateId("https://d-nb.info/gnd/102423008"));
self::assertEquals("102423008", MDNodaRepository::gnd->validateId("http://d-nb.info/gnd/102423008"));
self::assertEquals("102423008", MDNodaRepository::gnd->validateId("http://d-nb.info/gnd/ 102423008"));
self::assertEquals("102423008", MDNodaRepository::gnd->validateId("102423008"));
// NDL (Japan)