Skip to content

Commit

Permalink
feat: add whereSqidNotIn mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
bensherred committed Nov 25, 2023
1 parent 7634796 commit 301cec7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@

use Closure;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/** @mixin Builder */
class WhereSqidsInMixin
class WhereSqidInMixin
{
public function whereSqidsIn(): Closure
public function whereSqidIn(): Closure
{
return function (string $column, array $sqids, $boolean = 'and', $not = false) {
return function (array $sqids, $boolean = 'and', $not = false) {
/** @var Model $model */
$model = $this->getModel();

$column = $model->qualifyColumn(column: $model->getKeyName());

/** @phpstan-ignore-next-line */
$values = array_map(callback: fn(string $sqid) => $this->getModel()->keyFromSqid(sqid: $sqid), array: $sqids);

Expand Down
20 changes: 20 additions & 0 deletions src/Mixins/WhereSqidNotInMixin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace RedExplosion\Sqids\Mixins;

use Closure;
use Illuminate\Database\Eloquent\Builder;

/** @mixin Builder */
class WhereSqidNotInMixin
{
public function whereSqidNotIn(): Closure
{
return function (array $sqids, $boolean = 'and') {
/** @phpstan-ignore-next-line */
return $this->whereSqidIn(sqids: $sqids, boolean: $boolean, not: true);
};
}
}
6 changes: 4 additions & 2 deletions src/SqidsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
use RedExplosion\Sqids\Mixins\FindBySqidMixin;
use RedExplosion\Sqids\Mixins\FindOrFailBySqidMixin;
use RedExplosion\Sqids\Mixins\WhereSqidMixin;
use RedExplosion\Sqids\Mixins\WhereSqidsInMixin;
use RedExplosion\Sqids\Mixins\WhereSqidInMixin;
use RedExplosion\Sqids\Mixins\WhereSqidNotInMixin;

class SqidsServiceProvider extends ServiceProvider
{
Expand All @@ -36,7 +37,8 @@ protected function bootBuilderMixins(): void
{
Builder::mixin(new FindBySqidMixin());
Builder::mixin(new FindOrFailBySqidMixin());
Builder::mixin(new WhereSqidInMixin());
Builder::mixin(new WhereSqidMixin());
Builder::mixin(new WhereSqidsInMixin());
Builder::mixin(new WhereSqidNotInMixin());
}
}

0 comments on commit 301cec7

Please sign in to comment.