Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
fail-fast: true
matrix:
os: ["ubuntu-22.04"]
php: ["8.1", "8.2"]
php: ["8.2"]
steps:
- name: 📦 Check out the codebase
uses: actions/checkout@v3
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"php": "^8.1",
"php": "^8.2",
"ext-pdo": "*",
"cycle/annotated": "*",
"cycle/database": "^2.4",
Expand Down Expand Up @@ -63,8 +63,8 @@
"scripts": {
"cs:fix": "php vendor/bin/php-cs-fixer fix -v",
"cs:diff": "php vendor/bin/php-cs-fixer fix --dry-run -v --diff",
"test": "php vendor/bin/pest",
"test:cc": "php vendor/bin/pest --coverage",
"test": "php vendor/bin/pest --colors=always",
"test:cc": "php vendor/bin/pest --colors=always --coverage",
"stan": "php vendor/bin/phpstan analyse --memory-limit=256M",
"stan:ci": "php vendor/bin/phpstan analyse --error-format=github",
"post-autoload-dump": [
Expand Down
4 changes: 2 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Bridge/Laravel/Providers/CycleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ public function register(): void
Registrators\RegisterConfigs::class,
Registrators\RegisterClassesInterface::class,
Registrators\RegisterAnnotated::class,
Registrators\RegisterORM::class,
Registrators\RegisterDatabase::class,
Registrators\RegisterMigrations::class,
Registrators\RegisterSchema::class,
Registrators\RegisterORM::class,
Registrators\RegisterMigrations::class,
];

foreach ($registrators as $registrator) {
Expand Down
46 changes: 46 additions & 0 deletions src/Bridge/Laravel/Rules/Exists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace WayOfDev\Cycle\Bridge\Laravel\Rules;

use Closure;
use Cycle\Database\DatabaseInterface;
use Cycle\Database\Query\SelectQuery;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Translation\PotentiallyTranslatedString;

readonly class Exists implements ValidationRule
{
public function __construct(
private DatabaseInterface $database,
private string $table,
private string $column = 'id'
) {
}

/**
* Run the validation rule.
*
* @param Closure(string): PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
/** @var SelectQuery $table */
$table = $this->database->table($this->table);

$count = $table->where([$this->column => $value])->count();

if (0 === $count) {
$fail($this->message());
}
}

/**
* Get the validation error message.
*/
public function message(): PotentiallyTranslatedString|string
{
return trans('validation.exists');
}
}
46 changes: 46 additions & 0 deletions src/Bridge/Laravel/Rules/Unique.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace WayOfDev\Cycle\Bridge\Laravel\Rules;

use Closure;
use Cycle\Database\DatabaseInterface;
use Cycle\Database\Query\SelectQuery;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Translation\PotentiallyTranslatedString;

readonly class Unique implements ValidationRule
{
public function __construct(
private DatabaseInterface $database,
private string $table,
private string $column = 'id'
) {
}

/**
* Run the validation rule.
*
* @param Closure(string): PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
/** @var SelectQuery $table */
$table = $this->database->table($this->table);

$count = $table->where([$this->column => $value])->count();

if (0 < $count) {
$fail($this->message());
}
}

/**
* Get the validation error message.
*/
public function message(): PotentiallyTranslatedString|string
{
return trans('validation.unique');
}
}