diff --git a/src/NodaTagRelationIdentifier.php b/src/NodaTagRelationIdentifier.php new file mode 100644 index 0000000..719bc7b --- /dev/null +++ b/src/NodaTagRelationIdentifier.php @@ -0,0 +1,54 @@ + + */ +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; + + } +} diff --git a/tests/NodaTagRelationIdentifierTest.php b/tests/NodaTagRelationIdentifierTest.php new file mode 100644 index 0000000..9470776 --- /dev/null +++ b/tests/NodaTagRelationIdentifierTest.php @@ -0,0 +1,50 @@ + + */ +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 + */ + 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); + + } +}