Add class NodaTagRelationIdentifier for parsing tag relation types from
input tag names
This commit is contained in:
parent
48355a6a36
commit
6f7ad13c4e
54
src/NodaTagRelationIdentifier.php
Normal file
54
src/NodaTagRelationIdentifier.php
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
<?PHP
|
||||||
|
/**
|
||||||
|
* Identifies the type of tag relation to an object based on known suffixes.
|
||||||
|
*
|
||||||
|
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
|
||||||
|
*/
|
||||||
|
declare(strict_types = 1);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Contains static functions for identifying uncertainty or blocking
|
||||||
|
* completely uncertain inputs for actors, times, and places.
|
||||||
|
*/
|
||||||
|
final class NodaTagRelationIdentifier {
|
||||||
|
|
||||||
|
private const SUFFIXES = [
|
||||||
|
'de' => [
|
||||||
|
' (Motiv)' => MDTagRelationType::display_subject,
|
||||||
|
]
|
||||||
|
];
|
||||||
|
|
||||||
|
public readonly string $name;
|
||||||
|
public readonly MDTagRelationType|false $relation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor: Removes identifiers for well-known tag relations and determines cleaned name and relation type.
|
||||||
|
*
|
||||||
|
* @param string $lang Current language.
|
||||||
|
* @param string $input_string Input string to clean.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct(string $lang, string $input_string) {
|
||||||
|
|
||||||
|
if (empty(self::SUFFIXES[$lang])) {
|
||||||
|
$this->name = $input_string;
|
||||||
|
$this->relation = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$relation = false;
|
||||||
|
|
||||||
|
$suffixes = self::SUFFIXES[$lang];
|
||||||
|
foreach (array_keys($suffixes) as $suffix) {
|
||||||
|
if (\mb_substr($input_string, \mb_strlen($suffix) * -1) === "$suffix") {
|
||||||
|
$input_string = \mb_substr($input_string, 0, \mb_strlen($suffix) * -1);
|
||||||
|
$relation = $suffixes[$suffix];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->name = $input_string;
|
||||||
|
$this->relation = $relation;
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
50
tests/NodaTagRelationIdentifierTest.php
Normal file
50
tests/NodaTagRelationIdentifierTest.php
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
<?PHP
|
||||||
|
/**
|
||||||
|
* Tests for the identification of tag relation types to objects based on input tag names.
|
||||||
|
*
|
||||||
|
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
|
||||||
|
*/
|
||||||
|
declare(strict_types = 1);
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use PHPUnit\Framework\Attributes\CoversClass;
|
||||||
|
use PHPUnit\Framework\Attributes\Small;
|
||||||
|
use PHPUnit\Framework\Attributes\DataProvider;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This script contains tests for the uncertainty helper.
|
||||||
|
*/
|
||||||
|
#[small]
|
||||||
|
#[CoversClass(\NodaTagRelationIdentifier::class)]
|
||||||
|
final class NodaTagRelationIdentifierTest extends TestCase {
|
||||||
|
/**
|
||||||
|
* Returns input tag names with and without known suffixes signaling the relation type.
|
||||||
|
*
|
||||||
|
* @return array<array{0: string, 1: string, 2: false|MDTagRelationType}>
|
||||||
|
*/
|
||||||
|
public static function tagNameAndRelationTypeProvider():array {
|
||||||
|
|
||||||
|
return [
|
||||||
|
'Delfin' => ["Delfin", "Delfin", false],
|
||||||
|
'Delfin (Motiv)' => ["Delfin (Motiv)", "Delfin", MDTagRelationType::display_subject],
|
||||||
|
];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test to ensure times are correctly cleaned and parsed.
|
||||||
|
*
|
||||||
|
* @param string $term Term to check.
|
||||||
|
* @param string $target_output Expected output name.
|
||||||
|
* @param false|MDTagRelationType $target_type Expected identified relation type (or false for lack thereof).
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
#[DataProvider('tagNameAndRelationTypeProvider')]
|
||||||
|
public function testIdentificationWorksCorrectly(string $term, string $target_output, false|MDTagRelationType $target_type):void {
|
||||||
|
|
||||||
|
$identification = new NodaTagRelationIdentifier("de", $term);
|
||||||
|
self::assertEquals($target_output, $identification->name);
|
||||||
|
self::assertEquals($target_type, $identification->relation);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user