Create your custom Spellchecker

A Spellchecker in a PHP-Spellchecker sense, is nothing more than a class that implements the Spellchecker interface.

Let's create a custom spellchecker that checks some possibly misspelled version of the library name.

<?php

$phpSpellcheckLibraryNameSpellchecker = new class implements SpellcheckerInterface {
    public function check(
        string $text,
        array $languages = [],
        array $context = []
    ): iterable {
        foreach (['php-spellchecker', 'php-spellcheckerer', 'php spellchecker'] as $misspelledCandidate) {
            $matches = [];

            if (preg_match('/\b'.$misspelledCandidate.'\b/i', $text, $matches, PREG_OFFSET_CAPTURE) !== false) {
                foreach ($matches as $match) {
                    [$word, $offset] = $match;
                    [$line, $offsetFromLine] = LineAndOffset::findFromFirstCharacterOffset($text, $offset);

                    yield new Misspelling(
                        $word,
                        $offsetFromLine,
                        $line,
                        ['PHP Spellcheck']
                    );
                }
            }
        }
    }

    public function getSupportedLanguages(): iterable
    {
        yield 'en_US';
    }
};

/** @var Misspelling[]|\Generator $misspellings */
$misspellings = $phpSpellcheckLibraryNameSpellchecker->check('The PHP-Spellcheckerer library', ['en_US']);
foreach ($misspellings as $misspelling) {
    print_r([
        $misspelling->getWord(), // 'PHP-Spellcheckerer'
        $misspelling->getLineNumber(), // '...'
        $misspelling->getOffset(), // '...'
        $misspelling->getSuggestions(), // ['PHP Spellcheck']
        $misspelling->getContext(), // []
    ]);
}