-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathPresetProvider.php
More file actions
67 lines (57 loc) · 1.7 KB
/
PresetProvider.php
File metadata and controls
67 lines (57 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
declare(strict_types=1);
namespace Peck\Support;
/**
* Simple helper to provide the whitelisted words for a given preset.
* The whitelisted words are used to ignore certain words when spellchecking.
*/
final readonly class PresetProvider
{
/**
* The directory where the preset stubs are stored.
*/
private const PRESET_STUBS_DIRECTORY = __DIR__.'/../../stubs/presets';
/**
* Returns the whitelisted words for the given preset.
*
* @param array<int, string>|null $presets
* @return array<int, string>
*/
public static function whitelistedWords(?array $presets = []): array
{
/** @var array<int, string> */
$words = [
...self::getWordsFromStub('base'),
];
array_map(
static function (string $preset) use (&$words): void {
$words = [
...$words,
...self::getWordsFromStub($preset),
];
},
$presets ?? [],
);
return $words;
}
/**
* Gets the words from the given stub.
*
* @return array<int, string>
*/
public static function getWordsFromStub(string $preset): array
{
if (! self::stubExists($preset)) {
return [];
}
$path = sprintf('%s/%s.stub', self::PRESET_STUBS_DIRECTORY, $preset);
return array_values(array_filter(array_map('trim', explode("\n", (string) file_get_contents($path)))));
}
/**
* Checks if the given preset exists.
*/
private static function stubExists(string $preset): bool
{
return file_exists(sprintf('%s/%s.stub', self::PRESET_STUBS_DIRECTORY, $preset));
}
}