Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#77: refactor list_exists rule #158

Merged
merged 7 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 11 additions & 4 deletions src/HelpersServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ protected function extendValidator(): void
});

Validator::extend('list_exists', function ($attribute, $value, $parameters) {

if (count($parameters) < 1) {
return false;
}
Expand All @@ -76,12 +75,20 @@ protected function extendValidator(): void
$keyField = Arr::get($parameters, 1, 'id');

if (!empty(Arr::get($parameters, 2))) {
$value = collect($value)->pluck(Arr::get($parameters, 2));
$value = collect($value)
->pluck(Arr::get($parameters, 2))
->toArray();
aizlee marked this conversation as resolved.
Show resolved Hide resolved
}

return DB::table($table)
$value = array_unique($value);

$existingValue = DB::table($table)
aizlee marked this conversation as resolved.
Show resolved Hide resolved
->whereIn($keyField, $value)
->exists();
->distinct()
->pluck($keyField)
->toArray();
aizlee marked this conversation as resolved.
Show resolved Hide resolved

return count($existingValue) === count($value);
});
}

Expand Down
6 changes: 3 additions & 3 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function testUniqueExceptOfAuthorizedUserFail()

public function testListExists()
{
$this->mockListExists(true);
$this->mockListExists([1, 2, 3]);

$validator = Validator::make(
['ids' => [1, 2, 3]],
Expand All @@ -79,7 +79,7 @@ public function testListExists()

public function testListExistsByArray()
{
$this->mockListExists(true);
$this->mockListExists([1, 2, 3]);

$validator = Validator::make(
[
Expand Down Expand Up @@ -108,7 +108,7 @@ public function testListExistsByArray()

public function testListExistsFailedValidation()
{
$this->mockListExists(false);
$this->mockListExists([1, 2]);

$validator = Validator::make(
['ids' => [1, 2, 3]],
Expand Down
14 changes: 9 additions & 5 deletions tests/support/Traits/SqlMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,14 +432,18 @@ protected function mockExistsUsersExceptAuthorizedByArray(
}

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

$this->mockSelect(
query: "select distinct \"{$keyField}\" from \"{$table}\" where \"{$keyField}\" in (?, ?, ?)",
result: $formattedResult,
bindings: [1, 2, 3],
DenTray marked this conversation as resolved.
Show resolved Hide resolved
);
}

Expand Down
Loading