File tree Expand file tree Collapse file tree 4 files changed +97
-5
lines changed Expand file tree Collapse file tree 4 files changed +97
-5
lines changed Original file line number Diff line number Diff line change 1515 }
1616 ],
1717 "require" : {
18- "php" : " ^8.1 " ,
18+ "php" : " ^8.2 " ,
1919 "ext-pdo" : " *" ,
2020 "cycle/annotated" : " *" ,
2121 "cycle/database" : " ^2.4" ,
6363 "scripts" : {
6464 "cs:fix" : " php vendor/bin/php-cs-fixer fix -v" ,
6565 "cs:diff" : " php vendor/bin/php-cs-fixer fix --dry-run -v --diff" ,
66- "test" : " php vendor/bin/pest" ,
67- "test:cc" : " php vendor/bin/pest --coverage" ,
66+ "test" : " php vendor/bin/pest --colors=always " ,
67+ "test:cc" : " php vendor/bin/pest --colors=always -- coverage" ,
6868 "stan" : " php vendor/bin/phpstan analyse --memory-limit=256M" ,
6969 "stan:ci" : " php vendor/bin/phpstan analyse --error-format=github" ,
7070 "post-autoload-dump" : [
Original file line number Diff line number Diff line change @@ -53,10 +53,10 @@ public function register(): void
5353 Registrators \RegisterConfigs::class,
5454 Registrators \RegisterClassesInterface::class,
5555 Registrators \RegisterAnnotated::class,
56- Registrators \RegisterORM::class,
5756 Registrators \RegisterDatabase::class,
58- Registrators \RegisterMigrations::class,
5957 Registrators \RegisterSchema::class,
58+ Registrators \RegisterORM::class,
59+ Registrators \RegisterMigrations::class,
6060 ];
6161
6262 foreach ($ registrators as $ registrator ) {
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace WayOfDev \Cycle \Bridge \Laravel \Rules ;
6+
7+ use Closure ;
8+ use Cycle \Database \DatabaseInterface ;
9+ use Cycle \Database \Query \SelectQuery ;
10+ use Illuminate \Contracts \Validation \ValidationRule ;
11+ use Illuminate \Translation \PotentiallyTranslatedString ;
12+
13+ readonly class Exists implements ValidationRule
14+ {
15+ public function __construct (
16+ private DatabaseInterface $ database ,
17+ private string $ table ,
18+ private string $ column = 'id '
19+ ) {
20+ }
21+
22+ /**
23+ * Run the validation rule.
24+ *
25+ * @param Closure(string): PotentiallyTranslatedString $fail
26+ */
27+ public function validate (string $ attribute , mixed $ value , Closure $ fail ): void
28+ {
29+ /** @var SelectQuery $table */
30+ $ table = $ this ->database ->table ($ this ->table );
31+
32+ $ count = $ table ->where ([$ this ->column => $ value ])->count ();
33+
34+ if (0 === $ count ) {
35+ $ fail ($ this ->message ());
36+ }
37+ }
38+
39+ /**
40+ * Get the validation error message.
41+ */
42+ public function message (): PotentiallyTranslatedString |string
43+ {
44+ return trans ('validation.exists ' );
45+ }
46+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ declare (strict_types=1 );
4+
5+ namespace WayOfDev \Cycle \Bridge \Laravel \Rules ;
6+
7+ use Closure ;
8+ use Cycle \Database \DatabaseInterface ;
9+ use Cycle \Database \Query \SelectQuery ;
10+ use Illuminate \Contracts \Validation \ValidationRule ;
11+ use Illuminate \Translation \PotentiallyTranslatedString ;
12+
13+ readonly class Unique implements ValidationRule
14+ {
15+ public function __construct (
16+ private DatabaseInterface $ database ,
17+ private string $ table ,
18+ private string $ column = 'id '
19+ ) {
20+ }
21+
22+ /**
23+ * Run the validation rule.
24+ *
25+ * @param Closure(string): PotentiallyTranslatedString $fail
26+ */
27+ public function validate (string $ attribute , mixed $ value , Closure $ fail ): void
28+ {
29+ /** @var SelectQuery $table */
30+ $ table = $ this ->database ->table ($ this ->table );
31+
32+ $ count = $ table ->where ([$ this ->column => $ value ])->count ();
33+
34+ if (0 < $ count ) {
35+ $ fail ($ this ->message ());
36+ }
37+ }
38+
39+ /**
40+ * Get the validation error message.
41+ */
42+ public function message (): PotentiallyTranslatedString |string
43+ {
44+ return trans ('validation.unique ' );
45+ }
46+ }
You can’t perform that action at this time.
0 commit comments