This repository was archived by the owner on Jan 7, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 8 files changed +188
-8
lines changed Expand file tree Collapse file tree 8 files changed +188
-8
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,25 @@ class Post extends Model
76
76
}
77
77
```
78
78
79
+ ### Validation
80
+
81
+ Should you wish to use the efficient UUID column as part of your validation strategy, you may use the ` EfficientUuidExists ` rule as normal.
82
+
83
+ ``` php
84
+ use Dyrynda\Database\Rules\EfficientUuidExists;
85
+
86
+ public function update(Request $request, User $user)
87
+ {
88
+ $request->validate([
89
+ // Using the default column name
90
+ 'uuid' => [new EfficientUuidExists(Post::class)],
91
+
92
+ // Using a custom column name
93
+ 'custom_uuid' => [new EfficientUuidExists(Post::class, 'custom_uuid')],
94
+ ]);
95
+ }
96
+ ```
97
+
79
98
## Support
80
99
81
100
If you are having general issues with this package, feel free to contact me on [ Twitter] ( https://twitter.com/michaeldyrynda ) .
Original file line number Diff line number Diff line change 12
12
],
13
13
"require" : {
14
14
"php" : " ^7.3|^8.0" ,
15
+ "dyrynda/laravel-model-uuid" : " ^6.5" ,
15
16
"illuminate/container" : " ^8.12" ,
16
17
"illuminate/contracts" : " ^8.12" ,
17
18
"illuminate/database" : " ^8.12" ,
18
19
"ramsey/uuid" : " ^4.1"
19
20
},
20
21
"require-dev" : {
21
- "phpunit/phpunit " : " ^9.3 " ,
22
+ "laravel/legacy-factories " : " ^1.1 " ,
22
23
"mockery/mockery" : " ^1.4.2" ,
23
- "orchestra/testbench" : " ^6.0"
24
+ "orchestra/testbench" : " ^6.0" ,
25
+ "phpunit/phpunit" : " ^9.3"
24
26
},
25
27
"autoload" : {
26
28
"psr-4" : {
Original file line number Diff line number Diff line change 22
22
<directory >./tests/</directory >
23
23
</testsuite >
24
24
</testsuites >
25
+ <php >
26
+ <env name =" DB_CONNECTION" value =" testing" />
27
+ </php >
25
28
</phpunit >
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Dyrynda \Database \Rules ;
4
+
5
+ use Ramsey \Uuid \Uuid ;
6
+ use Illuminate \Contracts \Validation \Rule ;
7
+
8
+ class EfficientUuidExists implements Rule
9
+ {
10
+ /** @var \Dyrynda\Database\GeneratesUuid */
11
+ protected $ model ;
12
+
13
+ /** @var string */
14
+ protected $ column ;
15
+
16
+ public function __construct (string $ model , string $ column = 'uuid ' )
17
+ {
18
+ $ this ->model = new $ model ;
19
+
20
+ $ this ->column = $ column ;
21
+ }
22
+
23
+ public function passes ($ attribute , $ value ): bool
24
+ {
25
+ if (Uuid::isValid ($ value )) {
26
+ $ binaryUuid = Uuid::fromString (strtolower ($ value ))->getBytes ();
27
+
28
+ return $ this ->model ->where ($ this ->column , $ binaryUuid )->exists ();
29
+ }
30
+
31
+ return false ;
32
+ }
33
+
34
+ public function message (): string
35
+ {
36
+ return trans ('validation.exists ' );
37
+ }
38
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Tests ;
4
+
5
+ use Dyrynda \Database \Rules \EfficientUuidExists ;
6
+ use Ramsey \Uuid \Uuid ;
7
+ use Tests \Fixtures \EfficientUuidPost ;
8
+
9
+ class EfficientUuidExistsRuleTest extends TestCase
10
+ {
11
+ /** @test */
12
+ public function it_passes_valid_existing_uuid ()
13
+ {
14
+ /** @var \Tests\Fixtures\EfficientUuidPost $post */
15
+ $ post = factory (EfficientUuidPost::class)->create ();
16
+
17
+ $ rule = new EfficientUuidExists (EfficientUuidPost::class);
18
+
19
+ $ this ->assertTrue ($ rule ->passes ('uuid ' , $ post ->uuid ));
20
+ }
21
+
22
+ /** @test */
23
+ public function it_fails_on_non_existing_uuid ()
24
+ {
25
+ $ uuid = Uuid::uuid4 ();
26
+
27
+ $ rule = new EfficientUuidExists (EfficientUuidPost::class);
28
+
29
+ $ this ->assertFalse ($ rule ->passes ('post_id ' , $ uuid ));
30
+ }
31
+
32
+ /** @test */
33
+ public function it_fails_on_any_non_uuid_invalid_strings ()
34
+ {
35
+ $ uuid = "1235123564354633 " ;
36
+
37
+ $ rule = new EfficientUuidExists (EfficientUuidPost::class, 'uuid ' );
38
+
39
+ $ this ->assertFalse ($ rule ->passes ('post_id ' , $ uuid ));
40
+ }
41
+
42
+ /** @test */
43
+ public function it_works_with_custom_uuid_column_name ()
44
+ {
45
+ /** @var \Tests\Fixtures\EfficientUuidPost $post */
46
+ $ post = factory (EfficientUuidPost::class)->create ();
47
+
48
+ $ rule = new EfficientUuidExists (EfficientUuidPost::class, 'custom_uuid ' );
49
+
50
+ $ this ->assertTrue ($ rule ->passes ('custom_uuid ' , $ post ->custom_uuid ));
51
+ }
52
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Tests \Fixtures ;
4
+
5
+ use Illuminate \Database \Eloquent \Model ;
6
+ use Dyrynda \Database \Casts \EfficientUuid ;
7
+ use Dyrynda \Database \Support \GeneratesUuid ;
8
+
9
+ class EfficientUuidPost extends Model
10
+ {
11
+ use GeneratesUuid;
12
+
13
+ protected $ table = 'posts ' ;
14
+
15
+ public $ timestamps = false ;
16
+
17
+ protected $ casts = [
18
+ 'uuid ' => EfficientUuid::class,
19
+ 'custom_uuid ' => EfficientUuid::class,
20
+ ];
21
+
22
+ public function uuidColumn (): string
23
+ {
24
+ return 'uuid ' ;
25
+ }
26
+
27
+ public function uuidColumns (): array
28
+ {
29
+ return ['uuid ' , 'custom_uuid ' ];
30
+ }
31
+ }
Original file line number Diff line number Diff line change 2
2
3
3
namespace Tests ;
4
4
5
+ use Illuminate \Support \Facades \Schema ;
6
+ use Illuminate \Database \Schema \Blueprint ;
5
7
use Dyrynda \Database \LaravelEfficientUuidServiceProvider ;
6
8
7
9
class TestCase extends \Orchestra \Testbench \TestCase
8
10
{
9
- protected function getPackageProviders ($ app )
10
- {
11
- return [
12
- LaravelEfficientUuidServiceProvider::class,
13
- ];
14
- }
11
+ protected function setUp (): void
12
+ {
13
+ parent ::setUp ();
14
+
15
+ $ this ->withFactories (realpath (__DIR__ . '/database/factories ' ));
16
+
17
+ $ this ->setupDatabase ($ this ->app );
18
+ }
19
+
20
+ protected function getPackageProviders ($ app )
21
+ {
22
+ return [
23
+ LaravelEfficientUuidServiceProvider::class,
24
+ ];
25
+ }
26
+
27
+ protected function setupDatabase ($ app )
28
+ {
29
+ Schema::dropAllTables ();
30
+
31
+ $ app ['db ' ]->connection ()->getSchemaBuilder ()->create ('posts ' , function (Blueprint $ table ) {
32
+ $ table ->increments ('id ' );
33
+ $ table ->efficientUuid ('uuid ' );
34
+ $ table ->efficientUuid ('custom_uuid ' );
35
+ $ table ->string ('title ' );
36
+ });
37
+ }
15
38
}
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /** @var \Illuminate\Database\Eloquent\Factory $factory */
4
+
5
+ use Faker \Generator as Faker ;
6
+ use Tests \Fixtures \EfficientUuidPost ;
7
+
8
+ $ factory ->define (EfficientUuidPost::class, function (Faker $ faker ) {
9
+ return [
10
+ 'title ' => $ faker ->sentence ,
11
+ ];
12
+ });
You can’t perform that action at this time.
0 commit comments