1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace PhpSpellcheck\Source; |
||
6 | |||
7 | use PhpSpellcheck\Exception\RuntimeException; |
||
8 | use PhpSpellcheck\Text; |
||
9 | use PhpSpellcheck\TextInterface; |
||
10 | |||
11 | class PHPString implements SourceInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | 4 | private $string; |
|
17 | |||
18 | 4 | public function __construct(string $string) |
|
19 | 4 | { |
|
20 | $this->string = $string; |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * @param array $context |
||
25 | * |
||
26 | 4 | * @return TextInterface[] |
|
27 | */ |
||
28 | 4 | public function toTexts(array $context): iterable |
|
29 | 4 | { |
|
30 | $encoding = mb_detect_encoding($this->string, null, true); |
||
31 | |||
32 | if ($encoding === false) { |
||
33 | throw new RuntimeException( |
||
34 | \Safe\sprintf( |
||
35 | 'Coulnd\'t detect enconding of string:' . PHP_EOL . '%s', |
||
36 | $this->string |
||
37 | ) |
||
38 | ); |
||
39 | } |
||
40 | |||
41 | yield new Text($this->string, $encoding, $context); |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
42 | } |
||
43 | } |
||
44 |