|
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 null|MisspellingHandlerInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $misspellingHandler; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var null|TextProcessorInterface |
|
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
|
|
|
) { |
|
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; |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string|SourceInterface $source |
|
64
|
|
|
* |
|
65
|
|
|
* @return MisspellingInterface[] |
|
66
|
|
|
*/ |
|
67
|
5 |
|
private function doSpellcheck($source, array $languages, array $context, string $spellCheckEncoding): iterable |
|
68
|
|
|
{ |
|
69
|
5 |
|
if (is_string($source)) { |
|
70
|
4 |
|
$source = new PHPString($source); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
5 |
|
if ($source instanceof SourceInterface) { |
|
|
|
|
|
|
74
|
4 |
|
return $this->doSpellcheckFromSource($source, $languages, $context, $spellCheckEncoding); |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
$sourceVarType = is_object($source) ? get_class($source) : gettype($source); |
|
78
|
|
|
|
|
79
|
1 |
|
throw new InvalidArgumentException('Source should be of type string or ' . SourceInterface::class . '. "' . $sourceVarType . '" given'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
4 |
|
/** |
|
83
|
|
|
* @return MisspellingInterface[] |
|
84
|
|
|
*/ |
|
85
|
|
|
private function doSpellcheckFromSource( |
|
86
|
|
|
SourceInterface $source, |
|
87
|
|
|
array $languages, |
|
88
|
4 |
|
array $context, |
|
89
|
4 |
|
string $spellCheckEncoding |
|
90
|
2 |
|
): iterable { |
|
91
|
|
|
foreach ($source->toTexts($context) as $text) { |
|
92
|
|
|
if ($this->textProcessor !== null) { |
|
93
|
4 |
|
$text = $this->textProcessor->process($text); |
|
94
|
4 |
|
} |
|
95
|
4 |
|
|
|
96
|
4 |
|
$misspellingsCheck = $this->spellChecker->check( |
|
97
|
4 |
|
$text->getContent(), |
|
98
|
|
|
$languages, |
|
99
|
|
|
$text->getContext(), |
|
100
|
4 |
|
$spellCheckEncoding |
|
101
|
|
|
); |
|
102
|
4 |
|
|
|
103
|
|
|
yield from $misspellingsCheck; |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function setSpellchecker(SpellcheckerInterface $spellChecker): void |
|
108
|
|
|
{ |
|
109
|
|
|
$this->spellChecker = $spellChecker; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function setMisspellingHandler(MisspellingHandlerInterface $misspellingHandler): void |
|
113
|
|
|
{ |
|
114
|
|
|
$this->misspellingHandler = $misspellingHandler; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|