<?PHP
/**
 * Abstract class to be inherited by classes for writing consolidated vocabulary names.
 *
 * @author Joshua Ramon Enslin <joshua@museum-digital.de>
 */
declare(strict_types = 1);

/**
 * Abstract class to be inherited by classes for writing consolidated vocabulary names.
 */
abstract class NodaConsolidatedNamesAbstract {
    /**
     * This function sanitizes a string.
     *
     * @param string $inputString Input string.
     *
     * @return string
     */
    final protected static function _sanitizeInputStringStatic(string $inputString):string {

        $string = trim($inputString, "; \t" . PHP_EOL);
        $string = strtr($string, ["<" => "[", ">" => "]", "\t" => " ", '\n' => ' ',
            '<br />' => ' ', '<br/>' => ' ', '<br>' => ' ',
            "&lt;br /&gt;" => ' ', '§' => '"'
        ]);

        $string = str_replace(PHP_EOL, ' ', $string);
        while (strpos($string, "  ") !== false) {
            $string = str_replace("  ", " ", $string);
        }

        $string = strip_tags((string)$string);

        return trim(trim($string), ',| ');

    }

    /**
     * Does general cleanup for vocabulary entries.
     *
     * @param string $input Input string.
     *
     * @return string
     */
    final public static function sanitizeInputString(string $input):string {

        $output = strtr(
            self::_sanitizeInputStringStatic($input),
            [
                '<' => '(',
                '>' => ')',
                '[' => '(',
                ']' => ')',
                "unbekannt" => "",
            ],
        );

        // If the first and last character of the name are brackets, remove those.
        if (substr($output, 0, 1) === '(' && substr($output, -1) === ')') {
            $output = trim($output, '()');
        }

        return $output;

    }
}
