Skip to content

Commit 0beab93

Browse files
authored
Merge pull request #245 from wayofdev/feat/testing-helpers
feat: add laravel validation rules for database, bump php to 8.2
2 parents 44b5fba + e746905 commit 0beab93

File tree

6 files changed

+100
-8
lines changed

6 files changed

+100
-8
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
fail-fast: true
1818
matrix:
1919
os: ["ubuntu-22.04"]
20-
php: ["8.1", "8.2"]
20+
php: ["8.2"]
2121
steps:
2222
- name: 📦 Check out the codebase
2323
uses: actions/checkout@v3

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
}
1616
],
1717
"require": {
18-
"php": "^8.1",
18+
"php": "^8.2",
1919
"ext-pdo": "*",
2020
"cycle/annotated": "*",
2121
"cycle/database": "^2.4",
@@ -63,8 +63,8 @@
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": [

composer.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Bridge/Laravel/Providers/CycleServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff 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) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
}

0 commit comments

Comments
 (0)