Skip to content

Commit 896c140

Browse files
authored
Next version (#52)
* update: switched date rule to return the fluent interface * update: switched email rule to return the fluent interface * update: switched file rule to return the fluent interface * update: switched image rule to return the fluent interface * test: cleared mime types mock before each run * build: updated dependencies * docs: updated upgrade guide * build: remove non-stable dependencies * docs: added version compatibility table
1 parent 7dcbbf9 commit 896c140

File tree

7 files changed

+126
-62
lines changed

7 files changed

+126
-62
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ composer require sourcetoad/rule-helper-for-laravel
1212

1313
### RuleSet
1414

15-
The `RuleSet` class provides a fluent interface for defining sets of rules.
15+
The `RuleSet` class provides a fluent interface for defining sets of rules.
1616

1717
#### Basic usage
1818

@@ -108,3 +108,14 @@ Accepts multiple `RequiredIf` rules and only marks as required if all return tru
108108
#### requiredIfAny
109109

110110
Accepts multiple `RequiredIf` rules and marks as required if any return true.
111+
112+
## Version Compatibility
113+
114+
| Laravel | Rule Helper |
115+
|---------|-------------|
116+
| 12.x | 6.x |
117+
| 11.x | 5.x |
118+
| 10.x | 4.x |
119+
| 9.x | 3.x |
120+
| 9.x | 2.x |
121+
| 8.x | 1.x |

UPGRADE.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Upgrade Guide
22

3+
## v6
4+
5+
### Upgrading from v5.6 to v6.0
6+
7+
- Minimum Laravel version increased from `11.43` to `12.0`.
8+
- `Rule::date`, `Rule::email`, `Rule::file`, and `Rule::image` now return the fluent interface instead of a string. The
9+
`RuleSet` variant now accepts a modifier callback.
10+
311
## v5
412

513
### Upgrading from v5.5 to v5.6

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
"minimum-stability": "stable",
77
"require": {
88
"php": "^8.2||^8.3||^8.4",
9-
"laravel/framework": "^11.43"
9+
"laravel/framework": "^12.0"
1010
},
1111
"require-dev": {
1212
"ext-json": "*",
13-
"orchestra/testbench": "^9.4",
14-
"phpunit/phpunit": "^10.5",
15-
"laravel/pint": "1.19.0",
13+
"orchestra/testbench": "^10.0",
14+
"phpunit/phpunit": "^11.5",
15+
"laravel/pint": "1.21",
1616
"larastan/larastan": "^3.0"
1717
},
1818
"autoload": {

src/Rule.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@
1616
use Illuminate\Validation\Rule as LaravelRule;
1717
use Illuminate\Validation\Rules\ArrayRule;
1818
use Illuminate\Validation\Rules\Can;
19+
use Illuminate\Validation\Rules\Date;
1920
use Illuminate\Validation\Rules\Dimensions;
21+
use Illuminate\Validation\Rules\Email;
2022
use Illuminate\Validation\Rules\Enum;
2123
use Illuminate\Validation\Rules\ExcludeIf;
2224
use Illuminate\Validation\Rules\Exists;
25+
use Illuminate\Validation\Rules\File;
26+
use Illuminate\Validation\Rules\ImageFile;
2327
use Illuminate\Validation\Rules\In;
2428
use Illuminate\Validation\Rules\NotIn;
2529
use Illuminate\Validation\Rules\Password;
@@ -289,9 +293,9 @@ public static function currentPassword(?string $authenticationGuard = null): str
289293
*
290294
* @link https://laravel.com/docs/11.x/validation#rule-date
291295
*/
292-
public static function date(): string
296+
public static function date(): Date
293297
{
294-
return 'date';
298+
return LaravelRule::date();
295299
}
296300

297301
/**
@@ -439,24 +443,11 @@ public static function doesntStartWith(string ...$value): string
439443
/**
440444
* The field under validation must be formatted as an email address.
441445
*
442-
* By default, the *RFCValidation* validator is applied, but you can apply other validation styles as well:
443-
*
444-
* - *rfc*: {@see \Egulias\EmailValidator\Validation\RFCValidation}
445-
* - *strict*: {@see \Egulias\EmailValidator\Validation\NoRFCWarningsValidation}
446-
* - *dns*: {@see \Egulias\EmailValidator\Validation\DNSCheckValidation}
447-
* - *spoof*: {@see \Egulias\EmailValidator\Validation\Extra\SpoofCheckValidation}
448-
* - *filter*: {@see \Illuminate\Validation\Concerns\FilterEmailValidation}
449-
* - *filter_unicode*: {@see \Illuminate\Validation\Concerns\FilterEmailValidation::unicode}
450-
*
451446
* @link https://laravel.com/docs/11.x/validation#rule-email
452447
*/
453-
public static function email(string ...$validator): string
448+
public static function email(): Email
454449
{
455-
if (count($validator)) {
456-
return 'email:'.implode(',', $validator);
457-
}
458-
459-
return 'email';
450+
return LaravelRule::email();
460451
}
461452

462453
/**
@@ -587,9 +578,9 @@ public static function extensions(string ...$extension): string
587578
*
588579
* @link https://laravel.com/docs/11.x/validation#rule-file
589580
*/
590-
public static function file(): string
581+
public static function file(): File
591582
{
592-
return 'file';
583+
return LaravelRule::file();
593584
}
594585

595586
/**
@@ -642,9 +633,9 @@ public static function hexColor(): string
642633
*
643634
* @link https://laravel.com/docs/11.x/validation#rule-image
644635
*/
645-
public static function image(): string
636+
public static function image(): ImageFile
646637
{
647-
return 'image';
638+
return LaravelRule::imageFile();
648639
}
649640

650641
/**

src/RuleSet.php

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,17 @@ public function currentPassword(?string $authenticationGuard = null): self
338338
* The field under validation must be a valid, non-relative date according to the *strtotime* PHP function.
339339
*
340340
* @link https://laravel.com/docs/11.x/validation#rule-date
341+
* @param ?callable(\Illuminate\Validation\Rules\Date): (\Illuminate\Validation\Rules\Date|void) $modifier
341342
*/
342-
public function date(): self
343+
public function date(?callable $modifier = null): self
343344
{
344-
return $this->rule(Rule::date());
345+
$rule = Rule::date();
346+
347+
if ($modifier) {
348+
$rule = $this->modify($rule, $modifier);
349+
}
350+
351+
return $this->rule($rule);
345352
}
346353

347354
/**
@@ -491,20 +498,18 @@ public function doesntStartWith(string ...$value): self
491498
/**
492499
* The field under validation must be formatted as an email address.
493500
*
494-
* By default, the *RFCValidation* validator is applied, but you can apply other validation styles as well:
495-
*
496-
* - *rfc*: {@see \Egulias\EmailValidator\Validation\RFCValidation}
497-
* - *strict*: {@see \Egulias\EmailValidator\Validation\NoRFCWarningsValidation}
498-
* - *dns*: {@see \Egulias\EmailValidator\Validation\DNSCheckValidation}
499-
* - *spoof*: {@see \Egulias\EmailValidator\Validation\Extra\SpoofCheckValidation}
500-
* - *filter*: {@see \Illuminate\Validation\Concerns\FilterEmailValidation}
501-
* - *filter_unicode*: {@see \Illuminate\Validation\Concerns\FilterEmailValidation::unicode}
502-
*
503501
* @link https://laravel.com/docs/11.x/validation#rule-email
502+
* @param ?callable(\Illuminate\Validation\Rules\Email): (\Illuminate\Validation\Rules\Email|void) $modifier
504503
*/
505-
public function email(string ...$validator): self
504+
public function email(?callable $modifier = null): self
506505
{
507-
return $this->rule(Rule::email(...$validator));
506+
$rule = Rule::email();
507+
508+
if ($modifier) {
509+
$rule = $this->modify($rule, $modifier);
510+
}
511+
512+
return $this->rule($rule);
508513
}
509514

510515
/**
@@ -655,10 +660,17 @@ public function extensions(string ...$extension): self
655660
* The field under validation must be a successfully uploaded file.
656661
*
657662
* @link https://laravel.com/docs/11.x/validation#rule-file
663+
* @param ?callable(\Illuminate\Validation\Rules\File): (\Illuminate\Validation\Rules\File|void) $modifier
658664
*/
659-
public function file(): self
665+
public function file(?callable $modifier = null): self
660666
{
661-
return $this->rule(Rule::file());
667+
$rule = Rule::file();
668+
669+
if ($modifier) {
670+
$rule = $this->modify($rule, $modifier);
671+
}
672+
673+
return $this->rule($rule);
662674
}
663675

664676
/**
@@ -710,10 +722,17 @@ public function hexColor(): self
710722
* The file under validation must be an image (jpg, jpeg, png, bmp, gif, svg, or webp).
711723
*
712724
* @link https://laravel.com/docs/11.x/validation#rule-image
725+
* @param ?callable(\Illuminate\Validation\Rules\ImageFile): (\Illuminate\Validation\Rules\ImageFile|void) $modifier
713726
*/
714-
public function image(): self
727+
public function image(?callable $modifier = null): self
715728
{
716-
return $this->rule(Rule::image());
729+
$rule = Rule::image();
730+
731+
if ($modifier) {
732+
$rule = $this->modify($rule, $modifier);
733+
}
734+
735+
return $this->rule($rule);
717736
}
718737

719738
/**

tests/Unit/DefinedRuleSetsTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ protected function getPackageProviders($app): array
2828
public function testCanUseRulesDefinedOutsideOfCurrentRuleSet(): void
2929
{
3030
// Arrange
31-
resolve(DefinedRuleSets::class)->define('user.email', RuleSet::create()->email());
31+
resolve(DefinedRuleSets::class)->define('user.email', RuleSet::create()->string());
3232

3333
$validator = Validator::make([
34-
'field-a' => $this->faker->name(),
34+
'field-a' => $this->faker->randomNumber(),
3535
], [
3636
'field-a' => RuleSet::useDefined('user.email'),
3737
]);
@@ -42,7 +42,7 @@ public function testCanUseRulesDefinedOutsideOfCurrentRuleSet(): void
4242

4343
// Assert
4444
$this->assertTrue($fails, 'Failed asserting that RuleSet used defined rule.');
45-
$this->assertEquals(['field-a' => ['The field-a field must be a valid email address.']], $messages->toArray());
45+
$this->assertEquals(['field-a' => ['The field-a field must be a string.']], $messages->toArray());
4646
}
4747

4848
public function testModifyingDuringUseDoesNotModifyStoredCopy(): void
@@ -83,39 +83,39 @@ public function testThrowInvalidArgumentExceptionOnUnknownDefinition(): void
8383
public function testConcatDefinedRuleSet(): void
8484
{
8585
// Arrange
86-
RuleSet::define('user.email', RuleSet::create()->email());
86+
RuleSet::define('user.email', RuleSet::create()->string());
8787

8888
// Act
8989
$ruleSet = RuleSet::create()->required()->concatDefined('user.email');
9090

9191
// Assert
92-
$this->assertSame(['required', 'email'], $ruleSet->toArray());
92+
$this->assertSame(['required', 'string'], $ruleSet->toArray());
9393
}
9494

9595
public function testWorksWithNonBackedEnums(): void
9696
{
9797
// Arrange
98-
RuleSet::define(ExampleNonBackedEnum::Value, RuleSet::create()->email());
98+
RuleSet::define(ExampleNonBackedEnum::Value, RuleSet::create()->string());
9999

100100
// Act
101101
$ruleSet = RuleSet::useDefined(ExampleNonBackedEnum::Value);
102102

103103
// Assert
104-
$this->assertSame(['email'], $ruleSet->toArray());
104+
$this->assertSame(['string'], $ruleSet->toArray());
105105
}
106106

107107
public function testDefinedEnumsWithDuplicateValuesAreTreatedAsDifferent(): void
108108
{
109109
// Arrange
110-
RuleSet::define(ExampleStringEnum::Another, RuleSet::create()->email());
110+
RuleSet::define(ExampleStringEnum::Another, RuleSet::create()->string());
111111
RuleSet::define(ExampleStringDuplicateEnum::Another, RuleSet::create()->required());
112112

113113
// Act
114114
$ruleSetOne = RuleSet::useDefined(ExampleStringEnum::Another);
115115
$ruleSetTwo = RuleSet::useDefined(ExampleStringDuplicateEnum::Another);
116116

117117
// Assert
118-
$this->assertSame(['email'], $ruleSetOne->toArray());
118+
$this->assertSame(['string'], $ruleSetOne->toArray());
119119
$this->assertSame(['required'], $ruleSetTwo->toArray());
120120
}
121121
}

0 commit comments

Comments
 (0)