Skip to content

Commit

Permalink
Support Custom Alphabets
Browse files Browse the repository at this point in the history
  • Loading branch information
yondifon committed Aug 9, 2023
1 parent 3c423dc commit f85cc33
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
20 changes: 12 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,29 +51,33 @@ To create a new migration, use the artisan command `make:nanoid-migration`. All
```php
<?php

/**
* @var array|int
*/
/** @var array|int */
protected $nanoidLength = 10;
// id will be of length 10
// specifying to array. e.g [10, 20] will generate id of length 10 to 20

// or
public function nanoidLength(): array|int
{
// [10,20]
return 10;
}

/**
* @var string
*/
/** @var string */
protected $nanoidPrefix = 'pl_'; // id will look: pl_2k1MzOO2shfwow ...

// or
public function nanoidPrefix(): string
{
return 'pay_'; // pay_2MII83829sl2d
}

/** @var string */
protected nanoidAlphabet = 'ABC';
// or
public function nanoidAlphabet(): stirng {
return 'ABC'; // pay_ACBACB
}


```

Check the upgrade guide if you're [upgrading](UPGRADE.MD) from 0.x
Expand Down
21 changes: 20 additions & 1 deletion src/HasNanoids.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ protected function generateNanoid(): string

protected function newNanoId(): string
{
return (new Client())->generateId($this->getNanoidLength(), Client::MODE_DYNAMIC);
$client = new Client;

if ($alphabet = $this->getNanoIdAlphabet()) {
return $client->formattedId($alphabet, $this->getNanoidLength());
}

return $client->generateId($this->getNanoidLength(), Client::MODE_DYNAMIC);
}

protected function getNanoIdPrefix(): string
Expand Down Expand Up @@ -61,6 +67,19 @@ protected function getNanoidLength(): ?int
return $nanoIdLength;
}

protected function getNanoIdAlphabet(): ?string
{
if (property_exists($this, 'nanoidAlphabet')) {
return $this->nanoidAlphabet;
}

if (method_exists($this, 'nanoidAlphabet')) {
return $this->nanoidAlphabet();
}

return null;
}

/**
* Get the auto-incrementing key type.
*
Expand Down
25 changes: 25 additions & 0 deletions tests/HasNanoidsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@
expect(Str::length($model->getKey()))->toBe(3);
});

it('creates nanoid with alphabet before saving', function () {
$model = BasicModelWithAlphabet::create();

expect((int) $model->getKey())->toBeInt();
});

it('creates nanoid with alphabet and length before saving', function () {
$model = BasicModelWithAlphabetAndLength::create();

expect((int) $model->getKey())->toBeInt();
expect(Str::length($model->getKey()))->toBe(3);
});

abstract class ModelTest extends Model
{
use HasNanoids;
Expand Down Expand Up @@ -111,3 +124,15 @@ public function nanoidLength(): int
return 3;
}
}

class BasicModelWithAlphabet extends ModelTest
{
protected $nanoidAlphabet = '1234567890';
}

class BasicModelWithAlphabetAndLength extends ModelTest
{
protected $nanoidAlphabet = '1234567890';

protected $nanoidLength = 3;
}

0 comments on commit f85cc33

Please sign in to comment.