Skip to content

Commit

Permalink
refactor: sqids config
Browse files Browse the repository at this point in the history
  • Loading branch information
bensherred committed Nov 27, 2023
1 parent 737f4b8 commit a25f07d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 50 deletions.
7 changes: 4 additions & 3 deletions config/sqids.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
| Length
|--------------------------------------------------------------------------
|
| This option controls the "length" of the generated sqid excluding the
| prefix and separator. The value must be greater than 0.
| This option controls the "minimum length" of the generated sqid
| excluding the prefix and separator. This value must be greater
| than 0.
|
*/

'length' => 10,
'min_length' => 10,

/*
|--------------------------------------------------------------------------
Expand Down
77 changes: 62 additions & 15 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,83 @@

class Config
{
public static function string(string $key, string $default): string
protected static string $defaultAlphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

protected static int $defaultMinLength = 10;

protected static array $defaultBlacklist = [];

protected static string $defaultSeparator = '_';

protected static int $defaultPrefixLength = 3;

protected static string $defaultPrefixCase = 'lower';

public static function alphabet(): string
{
$alphabet = config(key: 'sqids.alphabet', default: static::$defaultAlphabet);

if (!$alphabet || !is_string(value: $alphabet)) {
return static::$defaultAlphabet;
}

return $alphabet;
}

public static function minLength(): int
{
/** @var int|null $minLength */
$minLength = config(key: 'sqids.min_length', default: static::$defaultMinLength);

if (null === $minLength) {
return static::$defaultMinLength;
}

return $minLength;
}

public static function blacklist(): array
{
$blacklist = config(key: 'sqids.blacklist', default: static::$defaultBlacklist);

if (!is_array($blacklist)) {
return static::$defaultBlacklist;
}

return $blacklist;
}

public static function separator(): string
{
$value = config(key: $key, default: $default);
$separator = config(key: 'sqids.separator', default: static::$defaultSeparator);

if (!is_string($value)) {
return $default;
if (!$separator || !is_string(value: $separator)) {
return static::$defaultSeparator;
}

return $value;
return $separator;
}

public static function integer(string $key, int $default): int
public static function prefixLength(): int
{
$value = config(key: $key, default: $default);
/** @var int|null $prefixLength */
$prefixLength = config(key: 'sqids.prefix.length', default: static::$defaultPrefixLength);

if (!is_int($value)) {
return $default;
if (null === $prefixLength) {
return static::$defaultPrefixLength;
}

return $value;
return $prefixLength;
}

public static function array(string $key, array $default = []): array
public static function prefixCase(): string
{
$value = config(key: $key, default: $default);
$prefixCase = config(key: 'sqids.prefix.case', default: static::$defaultPrefixCase);

if (!is_array($value)) {
return $default;
if (!$prefixCase || !is_string(value: $prefixCase)) {
return static::$defaultPrefixCase;
}

return $value;
return $prefixCase;
}
}
9 changes: 4 additions & 5 deletions src/HasSqids.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,19 @@ public function resolveRouteBindingQuery($query, $value, $field = null): Builder

public static function keyFromSqid(string $sqid): ?int
{
$prefixLength = Sqids::prefixLength();
$prefix = Str::beforeLast(subject: $sqid, search: Sqids::separator());
$prefixLength = Config::prefixLength();
$prefix = Str::beforeLast(subject: $sqid, search: Config::separator());
$expectedPrefix = Sqids::prefixForModel(model: __CLASS__);

if ($prefixLength && $prefix !== $expectedPrefix) {
return null;
}

$sqid = Str::afterLast(subject: $sqid, search: Sqids::separator());
$sqid = Str::afterLast(subject: $sqid, search: Config::separator());

$length = Str::length(value: $sqid);
$expectedLength = Sqids::length();

if ($length !== $expectedLength) {
if ($length < Config::minLength()) {
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Model
public static function find(string $sqid): ?EloquentModel
{
$models = static::models();
$prefix = Str::beforeLast(subject: $sqid, search: Sqids::separator());
$prefix = Str::beforeLast(subject: $sqid, search: Config::separator());

/** @var class-string<EloquentModel>|null $model */
$model = $models[$prefix] ?? null;
Expand All @@ -36,7 +36,7 @@ public static function find(string $sqid): ?EloquentModel
public static function findOrFail(string $sqid): EloquentModel
{
$models = static::models();
$prefix = Str::beforeLast(subject: $sqid, search: Sqids::separator());
$prefix = Str::beforeLast(subject: $sqid, search: Config::separator());

/** @var class-string<EloquentModel>|null $model */
$model = $models[$prefix] ?? null;
Expand Down
32 changes: 7 additions & 25 deletions 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::class);
$separator = $prefix ? static::separator() : null;
$separator = $prefix ? Config::separator() : null;
$sqid = static::encodeId(id: $id);

return "{$prefix}{$separator}{$sqid}";
Expand All @@ -25,7 +25,7 @@ public static function forModel(Model $model): string
public static function prefixForModel(string $model): ?string
{
$classBasename = class_basename(class: $model);
$prefixLength = static::prefixLength();
$prefixLength = Config::prefixLength();

if (!$prefixLength) {
return null;
Expand All @@ -35,7 +35,7 @@ public static function prefixForModel(string $model): ?string
? $classBasename
: rtrim(mb_strimwidth(string: $classBasename, start: 0, width: $prefixLength, encoding: 'UTF-8'));

return match (Config::string(key: 'sqids.prefix.case', default: 'lower')) {
return match (Config::prefixCase()) {
'upper' => Str::upper(value: $prefix),
'camel' => Str::camel(value: $prefix),
'snake' => Str::snake(value: $prefix),
Expand All @@ -46,21 +46,6 @@ public static function prefixForModel(string $model): ?string
};
}

public static function length(): int
{
return Config::integer(key: 'sqids.length', default: 10);
}

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

public static function prefixLength(): int
{
return Config::integer(key: 'sqids.prefix.length', default: 3);
}

public static function encodeId(int $id): string
{
return static::encoder()->encode(numbers: [$id]);
Expand All @@ -73,13 +58,10 @@ public static function decodeId(string $id): array

public static function encoder(): SqidsCore
{
$alphabet = Config::string(key: 'sqids.alphabet', default: '');
$minLength = static::length();
$blacklist = array_merge(
SqidsCore::DEFAULT_BLOCKLIST,
Config::array(key: 'sqids.blacklist'),
return new SqidsCore(
alphabet: Config::alphabet(),
minLength: Config::minLength(),
blocklist: Config::blacklist(),
);

return new SqidsCore(alphabet: $alphabet, minLength: $minLength, blocklist: $blacklist);
}
}

0 comments on commit a25f07d

Please sign in to comment.