Skip to content

Commit

Permalink
feat: add whereSqidsIn mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
bensherred committed Nov 25, 2023
1 parent e472d26 commit cf7d9e2
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
16 changes: 0 additions & 16 deletions src/HasSqids.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,6 @@ public function getRouteKeyName(): string
return 'sqid';
}

/**
* Retrieve the model for a bound value.
*
* @param mixed $value
* @param string|null $field
* @return Model|null
*/
// public function resolveRouteBinding($value, $field = null): ?Model
// {
// if ($field !== null) {
// return parent::resolveRouteBinding(value: $value, field: $field);
// }
//
// return $this->findBySqid($value);
// }

/**
* Retrieve the model for a bound value.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Mixins/FindBySqidMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class FindBySqidMixin
{
public function findBySqid(): Closure
{
return fn($id, $columns = ['*']) => $this->find(id: Sqids::decodeId(id: $id), columns: $columns);
return fn(string $id, array $columns = ['*']) => $this->find(id: Sqids::decodeId(id: $id), columns: $columns);
}
}
2 changes: 1 addition & 1 deletion src/Mixins/WhereSqidMixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class WhereSqidMixin
{
public function whereSqid(): Closure
{
return fn($id) => $this->whereKey(id: Sqids::decodeId(id: $id));
return fn(string $id) => $this->whereKey(id: Sqids::decodeId(id: $id));
}
}
22 changes: 22 additions & 0 deletions src/Mixins/WhereSqidsInMixin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace RedExplosion\Sqids\Mixins;

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

/** @mixin Builder */
class WhereSqidsInMixin
{
public function whereSqidsIn(): Closure
{
return function (string $column, array $sqids, $boolean = 'and', $not = false) {
$values = array_map(callback: fn(string $sqid) => Sqids::decodeId(id: $sqid), array: $sqids);

return $this->whereIn(column: $column, values: $values, boolean: $boolean, not: $not);
};
}
}
2 changes: 2 additions & 0 deletions src/SqidsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\ServiceProvider;
use RedExplosion\Sqids\Mixins\FindBySqidMixin;
use RedExplosion\Sqids\Mixins\WhereSqidMixin;
use RedExplosion\Sqids\Mixins\WhereSqidsInMixin;

class SqidsServiceProvider extends ServiceProvider
{
Expand All @@ -34,5 +35,6 @@ protected function bootBuilderMixins(): void
{
Builder::mixin(new FindBySqidMixin());
Builder::mixin(new WhereSqidMixin());
Builder::mixin(new WhereSqidsInMixin());
}
}

0 comments on commit cf7d9e2

Please sign in to comment.