Skip to content

Commit d711843

Browse files
author
riccardodallavia
committed
ADD custom rule macros
1 parent 1a4c151 commit d711843

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ You can use them in the same way as Laravel's base rules:
126126
``` php
127127
use Maize\Encryptable\Rules\ExistsEncrypted;
128128
use Illuminate\Support\Facades\Validator;
129+
use Illuminate\Validation\Rule;
129130

130131
$data = [
131132
'email' => '[email protected]',
@@ -137,6 +138,7 @@ Validator::make($data, [
137138
'string',
138139
'email',
139140
new ExistsEncrypted('users'), // checks whether the given email exists in the database
141+
Rule::existsEncrypted('users') // alternative way to invoke the rule
140142
],
141143
]);
142144
```
@@ -155,6 +157,7 @@ Validator::make($data, [
155157
'string',
156158
'email',
157159
new UniqueEncrypted('users'), // checks whether the given email does not already exist in the database
160+
Rule::uniqueEncrypted('users') // alternative way to invoke the rule
158161
],
159162
]);
160163
```

src/EncryptableServiceProvider.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Maize\Encryptable;
44

5+
use Illuminate\Validation\Rule;
6+
use Maize\Encryptable\Rules\ExistsEncrypted;
7+
use Maize\Encryptable\Rules\UniqueEncrypted;
58
use Spatie\LaravelPackageTools\Package;
69
use Spatie\LaravelPackageTools\PackageServiceProvider;
710

@@ -13,4 +16,17 @@ public function configurePackage(Package $package): void
1316
->name('laravel-encryptable')
1417
->hasConfigFile();
1518
}
19+
20+
public function bootingPackage()
21+
{
22+
Rule::macro(
23+
'uniqueEncrypted',
24+
fn (string $table, string $column = 'NULL') => new UniqueEncrypted($table, $column)
25+
);
26+
27+
Rule::macro(
28+
'existsEncrypted',
29+
fn (string $table, string $column = 'NULL') => new ExistsEncrypted($table, $column)
30+
);
31+
}
1632
}

tests/ExistsEncryptedTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Maize\Encryptable\Tests;
44

55
use Illuminate\Support\Facades\Validator;
6+
use Illuminate\Validation\Rule;
67
use Maize\Encryptable\Rules\ExistsEncrypted;
78

89
class ExistsEncryptedTest extends TestCase
@@ -24,4 +25,16 @@ public function it_should_validate_encrypted_data_with_custom_exists_rule()
2425

2526
$this->assertFalse($validationFails);
2627
}
28+
29+
/** @test */
30+
public function it_should_have_rule_macro()
31+
{
32+
$user = $this->createUser();
33+
34+
$validationFails = Validator::make($user->toArray(), [
35+
'first_name' => Rule::existsEncrypted('users'),
36+
])->fails();
37+
38+
$this->assertFalse($validationFails);
39+
}
2740
}

tests/UniqueEncryptedTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Maize\Encryptable\Tests;
44

55
use Illuminate\Support\Facades\Validator;
6+
use Illuminate\Validation\Rule;
67
use Maize\Encryptable\Rules\UniqueEncrypted;
78

89
class UniqueEncryptedTest extends TestCase
@@ -24,4 +25,16 @@ public function it_should_validate_encrypted_data_with_custom_unique_rule()
2425

2526
$this->assertTrue($validationFails);
2627
}
28+
29+
/** @test */
30+
public function it_should_have_rule_macro()
31+
{
32+
$user = $this->createUser();
33+
34+
$validationFails = Validator::make($user->toArray(), [
35+
'first_name' => Rule::uniqueEncrypted('users'),
36+
])->fails();
37+
38+
$this->assertTrue($validationFails);
39+
}
2740
}

0 commit comments

Comments
 (0)