Skip to content

Commit 58969dd

Browse files
committed
Allow more flexibility for rule input
1 parent 03ddfdc commit 58969dd

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ use Axlon\PostalCodeValidation\Rules\PostalCode;
6969

7070
$request->validate([
7171
'postal_code' => [
72-
PostalCode::of(['NL', 'BE']),
72+
PostalCode::of('NL', 'BE'),
7373
],
7474
]);
7575
```

src/Rules/PostalCode.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ final class PostalCode implements ValidationRule, DataAwareRule
3838
*/
3939
public function __construct(
4040
public readonly array $parameters,
41-
public RegexCollectionContract $regexes = new RegexCollection(),
41+
private readonly RegexCollectionContract $regexes = new RegexCollection(),
4242
) {
4343
}
4444

@@ -57,16 +57,15 @@ private static function isCountryCode(mixed $value): bool
5757
/**
5858
* Create a new validation rule.
5959
*
60-
* @param array<string>|string $countries
60+
* @param array<string>|string ...$parameters
6161
* @return self
6262
*/
63-
public static function of(array|string $countries): self
63+
public static function of(array|string ...$parameters): self
6464
{
65-
if (is_string($countries)) {
66-
$countries = [$countries];
67-
}
65+
/** @var array<string> $parameters */
66+
$parameters = Arr::flatten($parameters, depth: 1);
6867

69-
return new self($countries);
68+
return new self($parameters);
7069
}
7170

7271
/**

tests/Rules/PostalCodeTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ public function testItCreatesInstanceFromArray(): void
4242
self::assertSame(['AA', 'BB'], $rule->parameters);
4343
}
4444

45-
public function testItCreatesInstanceFromString(): void
45+
public function testItCreatesInstanceFromStrings(): void
4646
{
47-
$rule = PostalCode::of('AA');
47+
$rule = PostalCode::of('AA', 'BB');
4848

49-
self::assertSame(['AA'], $rule->parameters);
49+
self::assertSame(['AA', 'BB'], $rule->parameters);
5050
}
5151

5252
public function testItFailsWhenCountryCodeFieldIsEmpty(): void

0 commit comments

Comments
 (0)