language = new Language; } /** * @return array{0: string, 1: float}|null */ public function detect(string $text): ?array { if (trim($text) === '') { return null; } $languages = $this->language->detect($text)->bestResults()->close(); if ($languages === []) { return null; } // bestResults() keeps every candidate within 0.025 of the top score. // array_key_first picks the highest-ranked one (arsort'd by the library). $code = array_key_first($languages); return [$code, $languages[$code]]; } }