Skip to content

Commit 404d891

Browse files
authored
Validator::class (#73)
`Validator` class responsible for any validation.
1 parent e512347 commit 404d891

File tree

3 files changed

+56
-30
lines changed

3 files changed

+56
-30
lines changed

src/Plugin.php

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ class Plugin implements HandlesOriginalArguments
3434
private array $includeWords = [];
3535

3636
/**
37-
* @var string|array<string>|null
37+
* @var array<string>|null
3838
*/
39-
private $language = null;
39+
private $languages = null;
4040

4141
private bool $compact = false;
4242

@@ -54,31 +54,6 @@ public function __construct(
5454
$this->profanityLogger = new NullLogger;
5555
}
5656

57-
/**
58-
* Validates that the specified languages exist in the profanities directory.
59-
*
60-
* @param array<string>|null $languages
61-
* @return array<int, string> List of languages that don't exist
62-
*/
63-
private function validateLanguages($languages): array
64-
{
65-
if ($languages === null) {
66-
return [];
67-
}
68-
69-
$profanitiesDir = __DIR__.'/Config/profanities';
70-
$invalidLanguages = [];
71-
72-
foreach ($languages as $language) {
73-
$specificLanguage = "$profanitiesDir/$language.php";
74-
if (! file_exists($specificLanguage)) {
75-
$invalidLanguages[] = $language;
76-
}
77-
}
78-
79-
return $invalidLanguages;
80-
}
81-
8257
/**
8358
* {@inheritdoc}
8459
*/
@@ -103,7 +78,7 @@ public function handleOriginalArguments(array $arguments): void
10378

10479
if (str_starts_with($argument, '--language=')) {
10580
$languageValue = substr($argument, strlen('--language='));
106-
$this->language = explode(',', $languageValue);
81+
$this->languages = explode(',', $languageValue);
10782
unset($arguments[$key]);
10883
}
10984

@@ -124,7 +99,7 @@ public function handleOriginalArguments(array $arguments): void
12499
}
125100
}
126101

127-
$invalidLanguages = $this->validateLanguages($this->language);
102+
$invalidLanguages = Validator::validateLanguages($this->languages);
128103
if (! empty($invalidLanguages)) {
129104
$invalidLangsStr = implode(', ', $invalidLanguages);
130105
Output::errorMessage("The specified language does not exist: $invalidLangsStr");
@@ -196,7 +171,7 @@ function (Result $result) use (&$filesWithProfanity, &$totalProfanities): void {
196171
},
197172
$this->excludeWords,
198173
$this->includeWords,
199-
$this->language
174+
$this->languages
200175
);
201176

202177
$filesWithProfanityCount = count($filesWithProfanity);

src/Validator.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Pest\Profanity;
6+
7+
/**
8+
* @internal
9+
*/
10+
class Validator
11+
{
12+
/**
13+
* Validates that the specified languages exist in the profanities directory.
14+
*
15+
* @param array<string>|null $languages
16+
* @return array<int, string> List of languages that don't exist
17+
*/
18+
public static function validateLanguages(?array $languages): array
19+
{
20+
if ($languages === null) {
21+
return [];
22+
}
23+
24+
$profanitiesDir = __DIR__.'/Config/profanities';
25+
$invalidLanguages = [];
26+
27+
foreach ($languages as $language) {
28+
$specificLanguage = "$profanitiesDir/$language.php";
29+
if (! file_exists($specificLanguage)) {
30+
$invalidLanguages[] = $language;
31+
}
32+
}
33+
34+
return $invalidLanguages;
35+
}
36+
}

tests/Validator.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use Pest\Profanity\Validator;
4+
5+
test('empty', function () {
6+
expect(Validator::validateLanguages([]))->toBeEmpty();
7+
});
8+
9+
test('valid', function () {
10+
expect(Validator::validateLanguages(['ar', 'da', 'en', 'es', 'it', 'ja', 'nl', 'pt_BR']))->toBeEmpty();
11+
});
12+
13+
test('invalid', function () {
14+
expect(Validator::validateLanguages(['ar', 'en', 'invalid', 'es']))->toBe(['invalid']);
15+
});

0 commit comments

Comments
 (0)