Skip to content

Commit

Permalink
feat: add support for route model binding
Browse files Browse the repository at this point in the history
  • Loading branch information
bensherred committed Nov 25, 2023
1 parent 9054974 commit a130f79
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/HasSqids.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,36 @@

namespace RedExplosion\Sqids;

use Illuminate\Database\Eloquent\Model;

trait HasSqids
{
public function getSqidAttribute(): ?string
{
return Sqids::forModel(model: $this);
}

/**
* Get the route key for the model.
*/
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);
}
}
18 changes: 18 additions & 0 deletions src/Mixins/FindBySqidMixin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace RedExplosion\Sqids\Mixins;

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

/** @mixin Builder */
class FindBySqidMixin
{
public function findBySqid(): Closure
{
return fn($id, $columns = ['*']) => $this->find(id: Sqids::decodeId(id: $id), columns: $columns);
}
}
9 changes: 8 additions & 1 deletion src/Sqids.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static function forModel(Model $model): string
$id = $model->getKey();

$prefix = static::prefixForModel(model: $model);
$separator = Config::string(key: 'sqids.separator', default: '_');
$separator = static::separator();
$sqid = static::encodeId(id: $id);

return "{$prefix}{$separator}{$sqid}";
Expand All @@ -42,13 +42,20 @@ public static function prefixForModel(Model $model): ?string
};
}

public static function separator(): string
{
return Config::string(key: 'sqids.separator', default: '_');
}

public static function encodeId(int $id): string
{
return static::encoder()->encode(numbers: [$id]);
}

public static function decodeId(string $id): int
{
$id = Str::afterLast(subject: $id, search: static::separator());

return static::encoder()->decode(id: $id)[0];
}

Expand Down
9 changes: 9 additions & 0 deletions src/SqidsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace RedExplosion\Sqids;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\ServiceProvider;
use RedExplosion\Sqids\Mixins\FindBySqidMixin;

class SqidsServiceProvider extends ServiceProvider
{
Expand All @@ -23,5 +25,12 @@ public function boot(): void
groups: 'sqids-config',
);
}

$this->bootBuilderMixins();
}

protected function bootBuilderMixins(): void
{
Builder::mixin(new FindBySqidMixin());
}
}

0 comments on commit a130f79

Please sign in to comment.