<?PHP
/**
 * Checks the distinctly_typed_strings table for whether it
 * contains a string.
 */
declare(strict_types = 1);

/**
 * Checks the distinctly_typed_strings table for whether it
 * contains a string.
 */
final class NodaDistinctlyTypedStrings {
    /**
     * Checks the vocabulary database whether it contains a given string.
     *
     * @param MDMysqli $mysqli_noda DB connection.
     * @param string   $lang        Language to check in.
     * @param string   $search_term Search term.
     *
     * @return 'persinst'|'zeiten'|'orte'|'tag'|''
     */
    public static function lookup(MDMysqli $mysqli_noda, string $lang, string $search_term):string {

        $result = $mysqli_noda->query_by_stmt("SELECT `type`
            FROM `distinctly_typed_strings`
            WHERE `input_string` = ?
                AND `language` = ?", "ss", $search_term, $lang);

        if (!($cur = $result->fetch_row())) {
            $result->close();
            return '';
        }
        $result->close();

        if (!in_array($cur[0], ['persinst', 'zeiten', 'orte', 'tag', ''], true)) {
            throw new Exception("Distinctly typed string of unknown type: " . $cur[0]);
        }

        return $cur[0];

    }
}
