|
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\SourceInterface; |
|
10
|
|
|
use PhpSpellcheck\Spellchecker\SpellcheckerInterface; |
|
11
|
|
|
use PhpSpellcheck\TextProcessor\TextProcessorInterface; |
|
12
|
|
|
|
|
13
|
|
|
class MisspellingFinder |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var SpellcheckerInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $spellChecker; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var MisspellingHandlerInterface|null |
|
22
|
|
|
*/ |
|
23
|
|
|
private $misspellingHandler; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var TextProcessorInterface|null |
|
27
|
|
|
*/ |
|
28
|
|
|
private $textProcessor; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct( |
|
31
|
|
|
SpellcheckerInterface $spellChecker, |
|
32
|
5 |
|
?MisspellingHandlerInterface $misspellingHandler = null, |
|
33
|
|
|
?TextProcessorInterface $textProcessor = null |
|
34
|
|
|
) { |
|
35
|
|
|
$this->spellChecker = $spellChecker; |
|
36
|
|
|
$this->misspellingHandler = $misspellingHandler; |
|
37
|
5 |
|
$this->textProcessor = $textProcessor; |
|
38
|
5 |
|
} |
|
39
|
5 |
|
|
|
40
|
5 |
|
/** |
|
41
|
|
|
* @param iterable<TextInterface>|SourceInterface|string|TextInterface $source |
|
42
|
|
|
* @param array<mixed> $context |
|
43
|
|
|
* @param array<string> $languages |
|
44
|
|
|
* |
|
45
|
|
|
* @return MisspellingInterface[] |
|
46
|
|
|
*/ |
|
47
|
5 |
|
public function find( |
|
48
|
|
|
$source, |
|
49
|
|
|
array $languages = [], |
|
50
|
|
|
array $context = [] |
|
51
|
|
|
): iterable { |
|
52
|
|
|
if (\is_string($source)) { |
|
|
|
|
|
|
53
|
5 |
|
$texts = [new Text($source, $context)]; |
|
54
|
|
|
} elseif ($source instanceof TextInterface) { |
|
55
|
4 |
|
$texts = [$source]; |
|
56
|
1 |
|
} elseif (\is_array($source)) { |
|
57
|
|
|
$texts = $source; |
|
58
|
|
|
} elseif ($source instanceof SourceInterface) { |
|
59
|
4 |
|
$texts = $source->toTexts($context); |
|
60
|
|
|
} else { |
|
61
|
|
|
$sourceVarType = \is_object($source) ? \get_class($source) : \gettype($source); |
|
62
|
|
|
$allowedTypes = implode(' or ', ['"string"', '"' . SourceInterface::class . '"', '"iterable<' . TextInterface::class . '>"', '"' . TextInterface::class . '"']); |
|
63
|
|
|
|
|
64
|
|
|
throw new InvalidArgumentException('Source should be of type ' . $allowedTypes . ', "' . $sourceVarType . '" given'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
5 |
|
$misspellings = $this->doSpellCheckTexts($texts, $languages); |
|
68
|
|
|
|
|
69
|
5 |
|
if ($this->misspellingHandler !== null) { |
|
70
|
4 |
|
$this->misspellingHandler->handle($misspellings); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
5 |
|
return $misspellings; |
|
|
|
|
|
|
74
|
4 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function setSpellchecker(SpellcheckerInterface $spellChecker): void |
|
77
|
1 |
|
{ |
|
78
|
|
|
$this->spellChecker = $spellChecker; |
|
79
|
1 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function setMisspellingHandler(MisspellingHandlerInterface $misspellingHandler): void |
|
82
|
4 |
|
{ |
|
83
|
|
|
$this->misspellingHandler = $misspellingHandler; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param TextInterface[] $texts |
|
88
|
4 |
|
* @param string[] $languages |
|
89
|
4 |
|
* |
|
90
|
2 |
|
* @return iterable<MisspellingInterface> |
|
91
|
|
|
*/ |
|
92
|
|
|
private function doSpellCheckTexts( |
|
93
|
4 |
|
iterable $texts, |
|
94
|
4 |
|
array $languages |
|
95
|
4 |
|
): iterable { |
|
96
|
4 |
|
foreach ($texts as $text) { |
|
97
|
4 |
|
if ($this->textProcessor !== null) { |
|
98
|
|
|
$text = $this->textProcessor->process($text); |
|
99
|
|
|
} |
|
100
|
4 |
|
|
|
101
|
|
|
yield from $this->spellChecker->check( |
|
102
|
4 |
|
$text->getContent(), |
|
103
|
|
|
$languages, |
|
104
|
|
|
$text->getContext() |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|