tigitz /
php-spellchecker
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace PhpSpellcheck; |
||
| 6 | |||
| 7 | use PhpSpellcheck\Exception\InvalidArgumentException; |
||
| 8 | use PhpSpellcheck\MisspellingHandler\MisspellingHandlerInterface; |
||
| 9 | use PhpSpellcheck\Source\PHPString; |
||
| 10 | use PhpSpellcheck\Source\SourceInterface; |
||
| 11 | use PhpSpellcheck\Spellchecker\SpellcheckerInterface; |
||
| 12 | use PhpSpellcheck\TextProcessor\TextProcessorInterface; |
||
| 13 | use PhpSpellcheck\Utils\TextEncoding; |
||
| 14 | |||
| 15 | class MisspellingFinder |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var SpellcheckerInterface |
||
| 19 | */ |
||
| 20 | private $spellChecker; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var MisspellingHandlerInterface|null |
||
| 24 | */ |
||
| 25 | private $misspellingHandler; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var TextProcessorInterface|null |
||
| 29 | */ |
||
| 30 | private $textProcessor; |
||
| 31 | |||
| 32 | 5 | public function __construct( |
|
| 33 | SpellcheckerInterface $spellChecker, |
||
| 34 | ?MisspellingHandlerInterface $misspellingHandler = null, |
||
| 35 | ?TextProcessorInterface $textProcessor = null |
||
| 36 | ) { |
||
| 37 | 5 | $this->spellChecker = $spellChecker; |
|
| 38 | 5 | $this->misspellingHandler = $misspellingHandler; |
|
| 39 | 5 | $this->textProcessor = $textProcessor; |
|
| 40 | 5 | } |
|
| 41 | |||
| 42 | /** |
||
| 43 | * @param string|SourceInterface $source |
||
| 44 | * |
||
| 45 | * @return MisspellingInterface[] |
||
| 46 | */ |
||
| 47 | 5 | public function find( |
|
| 48 | $source, |
||
| 49 | array $languages = [], |
||
| 50 | array $context = [], |
||
| 51 | string $spellCheckEncoding = TextEncoding::UTF8 |
||
| 52 | ): iterable { |
||
| 53 | 5 | $misspellings = $this->doSpellcheck($source, $languages, $context, $spellCheckEncoding); |
|
| 54 | |||
| 55 | 4 | if ($this->misspellingHandler !== null) { |
|
| 56 | 1 | $this->misspellingHandler->handle($misspellings); |
|
| 57 | } |
||
| 58 | |||
| 59 | 4 | return $misspellings; |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 60 | } |
||
| 61 | |||
| 62 | public function setSpellchecker(SpellcheckerInterface $spellChecker): void |
||
| 63 | { |
||
| 64 | $this->spellChecker = $spellChecker; |
||
| 65 | } |
||
| 66 | |||
| 67 | 5 | public function setMisspellingHandler(MisspellingHandlerInterface $misspellingHandler): void |
|
| 68 | { |
||
| 69 | 5 | $this->misspellingHandler = $misspellingHandler; |
|
| 70 | 4 | } |
|
| 71 | |||
| 72 | /** |
||
| 73 | 5 | * @param string|SourceInterface $source |
|
| 74 | 4 | * |
|
| 75 | * @return MisspellingInterface[] |
||
| 76 | */ |
||
| 77 | 1 | private function doSpellcheck($source, array $languages, array $context, string $spellCheckEncoding): iterable |
|
| 78 | { |
||
| 79 | 1 | if (is_string($source)) { |
|
| 80 | $source = new PHPString($source); |
||
| 81 | } |
||
| 82 | 4 | ||
| 83 | if ($source instanceof SourceInterface) { |
||
| 84 | return $this->doSpellcheckFromSource($source, $languages, $context, $spellCheckEncoding); |
||
| 85 | } |
||
| 86 | |||
| 87 | $sourceVarType = is_object($source) ? get_class($source) : gettype($source); |
||
| 88 | 4 | ||
| 89 | 4 | throw new InvalidArgumentException('Source should be of type string or ' . SourceInterface::class . '. "' . $sourceVarType . '" given'); |
|
| 90 | 2 | } |
|
| 91 | |||
| 92 | /** |
||
| 93 | 4 | * @return MisspellingInterface[] |
|
| 94 | 4 | */ |
|
| 95 | 4 | private function doSpellcheckFromSource( |
|
| 96 | 4 | SourceInterface $source, |
|
| 97 | 4 | array $languages, |
|
| 98 | array $context, |
||
| 99 | string $spellCheckEncoding |
||
| 100 | 4 | ): iterable { |
|
| 101 | foreach ($source->toTexts($context) as $text) { |
||
| 102 | 4 | if ($this->textProcessor !== null) { |
|
| 103 | $text = $this->textProcessor->process($text); |
||
| 104 | } |
||
| 105 | |||
| 106 | $misspellingsCheck = $this->spellChecker->check( |
||
| 107 | $text->getContent(), |
||
| 108 | $languages, |
||
| 109 | $text->getContext(), |
||
| 110 | $spellCheckEncoding |
||
| 111 | ); |
||
| 112 | |||
| 113 | yield from $misspellingsCheck; |
||
|
0 ignored issues
–
show
|
|||
| 114 | } |
||
| 115 | } |
||
| 116 | } |
||
| 117 |