Skip to content

Commit

Permalink
Merge pull request #110 from RonasIT/77-implement-list-exists-validat…
Browse files Browse the repository at this point in the history
…ion-rule

77 Implement 'list_exists' validation rule
  • Loading branch information
DenTray authored Mar 29, 2024
2 parents 298301d + 50edd4f commit 7ed95ea
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 11 deletions.
41 changes: 32 additions & 9 deletions src/HelpersServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace RonasIT\Support;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
use Maatwebsite\Excel\ExcelServiceProvider;
use RonasIT\Support\Middleware\SecurityMiddleware;
use Illuminate\Support\Arr;

class HelpersServiceProvider extends ServiceProvider
{
Expand All @@ -19,6 +19,20 @@ public function boot()
$router->prependMiddlewareToGroup('web', SecurityMiddleware::class);
$router->prependMiddlewareToGroup('api', SecurityMiddleware::class);

$this->extendValidator();

app(ExcelServiceProvider::class, ['app' => app()])->boot();

$this->loadViewsFrom(__DIR__ . '/Stubs', 'ronasit');
}

public function register()
{
app(ExcelServiceProvider::class, ['app' => app()])->register();
}

protected function extendValidator()
{
Validator::extend('unique_except_of_authorized_user', function ($attribute, $value, $parameters = []) {
$table = Arr::get($parameters, 0, 'users');
$keyField = Arr::get($parameters, 1, 'id');
Expand All @@ -32,13 +46,22 @@ public function boot()
return !$result;
});

app(ExcelServiceProvider::class, ['app' => app()])->boot();
Validator::extend('list_exists', function ($attribute, $value, $parameters) {

$this->loadViewsFrom(__DIR__ . '/Stubs', 'ronasit');
}
if (count($parameters) < 1) {
return false;
}

public function register()
{
app(ExcelServiceProvider::class, ['app' => app()])->register();
$table = Arr::get($parameters, 0);
$keyField = Arr::get($parameters, 1, 'id');

if (!empty(Arr::get($parameters, 2))) {
$value = collect($value)->pluck(Arr::get($parameters, 2));
}

return DB::table($table)
->whereIn($keyField, $value)
->exists();
});
}
}
67 changes: 65 additions & 2 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace RonasIT\Support\Tests;

use Illuminate\Support\Facades\Auth;
use RonasIT\Support\Tests\Support\Traits\SqlMockTrait;
use Illuminate\Support\Facades\Validator;
use RonasIT\Support\Tests\Support\Traits\SqlMockTrait;

class ValidatorTest extends HelpersTestCase
{
Expand Down Expand Up @@ -64,4 +64,67 @@ public function testUniqueExceptOfAuthorizedUserFail()

$this->assertTrue($validator->fails());
}
}

public function testListExists()
{
$this->mockListExists(true);

$validator = Validator::make(
['ids' => [1, 2, 3]],
['ids' => 'list_exists:clients,user_id'],
);

$this->assertTrue($validator->passes());
}

public function testListExistsByArray()
{
$this->mockListExists(true);

$validator = Validator::make(
[
'ids' => [
[
'id' => 1,
'name' => 'name1',
],
[
'id' => 2,
'name' => 'name2',
],
[
'id' => 3,
'name' => 'name3',
],
]
],
[
'ids' => 'list_exists:clients,user_id,id',
],
);

$this->assertTrue($validator->passes());
}

public function testListExistsFailedValidation()
{
$this->mockListExists(false);

$validator = Validator::make(
['ids' => [1, 2, 3]],
['ids' => 'list_exists:clients,user_id'],
);

$this->assertTrue($validator->fails());
}

public function testListExistsWithoutArgs()
{
$validator = Validator::make(
['ids' => [1, 2, 3]],
['ids' => 'list_exists'],
);

$this->assertTrue($validator->fails());
}
}
13 changes: 13 additions & 0 deletions tests/support/Traits/SqlMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,19 @@ protected function mockExistsUsersExceptAuthorizedByArray(
);
}

protected function mockListExists(
bool $isExist,
string $table = 'clients',
string $keyField = 'user_id'
): void
{
$this->mockSelectExists(
"select exists(select * from `{$table}` where `{$keyField}` in (?, ?, ?)) as `exists`",
$isExist,
[1, 2, 3]
);
}

protected function mockUpdateSqlQuery(string $sql, array $bindings = [], ?int $rowCount = null): void
{
if (!empty($rowCount)) {
Expand Down

0 comments on commit 7ed95ea

Please sign in to comment.