diff --git a/UPGRADE.md b/UPGRADE.md new file mode 100644 index 0000000..34c3f51 --- /dev/null +++ b/UPGRADE.md @@ -0,0 +1,31 @@ +# Upgrade Guide + +## 0.x to 1.x + +Previously we had model within that package you had to extend but now you don't, you just need to use our trait like you would with `HasUlid` & `HasUuid` in Laravel + +```diff +{$model->getKeyName()} = $model->generateNanoid(); - }); - } ++ use HasNanoids; } ``` -Take note of the `$incrementing` and `$keyType` properties. Also make sure within your `boot` method you call the `parent::boot` method and then add the `creating` event listener. -Also make sure your id column is set to `string` type. +Your migration file should like. -```php +```diff // migration file public function up() { Schema::create('test_models', function (Blueprint $table) { - $table->string('id')->primary(); +- $table->id(); ++ $table->string('id')->primary(); // $table->timestamps(); }); @@ -113,35 +52,32 @@ To create a new migration, use the artisan command `make:nanoid-migration`. All {$model->getKeyName()} = $model->generateNanoid(); - }); - } -} diff --git a/src/Console/Commands/NanoidModelMakeCommand.php b/src/Console/Commands/NanoidModelMakeCommand.php index e6a239c..5619428 100644 --- a/src/Console/Commands/NanoidModelMakeCommand.php +++ b/src/Console/Commands/NanoidModelMakeCommand.php @@ -33,27 +33,13 @@ class NanoidModelMakeCommand extends ModelMakeCommand */ protected function getStub(): string { - return realpath(__DIR__.'/../../Eloquent/stubs/nanoid.model.stub'); - } - - /** - * Get the console command options. - */ - protected function getOptions(): array - { - // Remove the pivot option from the parent class. - return array_filter( - parent::getOptions(), - fn (array $option): bool => $option[0] !== 'pivot' - ); + return realpath(__DIR__.'/stubs/nanoid.model.stub'); } /** * Create a migration file for the model. - * - * @return void */ - protected function createMigration() + protected function createMigration(): void { $table = Str::snake(Str::pluralStudly(class_basename($this->argument('name')))); @@ -62,19 +48,4 @@ protected function createMigration() '--create' => $table, ]); } - - /** - * Get the value of a command option. - * - * @param null|string $key - * @return null|array|bool|string - */ - public function option($key = null) - { - if ($key === 'pivot') { - return false; - } - - return parent::option($key); - } } diff --git a/src/Database/Migrations/stubs/migration.create.stub b/src/Console/Commands/stubs/migration.create.stub similarity index 100% rename from src/Database/Migrations/stubs/migration.create.stub rename to src/Console/Commands/stubs/migration.create.stub diff --git a/src/Database/Migrations/stubs/migration.stub b/src/Console/Commands/stubs/migration.stub similarity index 100% rename from src/Database/Migrations/stubs/migration.stub rename to src/Console/Commands/stubs/migration.stub diff --git a/src/Database/Migrations/stubs/migration.update.stub b/src/Console/Commands/stubs/migration.update.stub similarity index 100% rename from src/Database/Migrations/stubs/migration.update.stub rename to src/Console/Commands/stubs/migration.update.stub diff --git a/src/Eloquent/stubs/nanoid.model.stub b/src/Console/Commands/stubs/nanoid.model.stub similarity index 60% rename from src/Eloquent/stubs/nanoid.model.stub rename to src/Console/Commands/stubs/nanoid.model.stub index 3a99299..ae5d75c 100644 --- a/src/Eloquent/stubs/nanoid.model.stub +++ b/src/Console/Commands/stubs/nanoid.model.stub @@ -3,9 +3,11 @@ namespace {{ namespace }}; use Illuminate\Database\Eloquent\Factories\HasFactory; -use Malico\LaravelNanoid\Eloquent\Model; +use Illuminate\Database\Eloquent\Model; +use Malico\LaravelNanoid\HasNanoids; class {{ class }} extends Model { use HasFactory; + use HasNanoids; } diff --git a/src/Database/Migrations/MigrationCreator.php b/src/Database/Migrations/MigrationCreator.php index ee26af5..61c7033 100644 --- a/src/Database/Migrations/MigrationCreator.php +++ b/src/Database/Migrations/MigrationCreator.php @@ -14,6 +14,6 @@ public function __construct(Filesystem $files, $customStubPath = null) public function stubPath() { - return __DIR__.'/stubs'; + return __DIR__.'/../../Console/Commands/stubs'; } } diff --git a/src/Eloquent/InteractsWithNanoid.php b/src/Eloquent/InteractsWithNanoid.php deleted file mode 100644 index ca1d4bd..0000000 --- a/src/Eloquent/InteractsWithNanoid.php +++ /dev/null @@ -1,30 +0,0 @@ -nanoidLength)) { - return random_int($this->nanoidLength[0], $this->nanoidLength[1]); - } - - return $this->nanoidLength; - } - - /** - * Generate a nanoid. - */ - public function generateNanoid(): string - { - $client = new Client(); - - return $this->nanoidPrefix.$client->generateId($this->getNanoidLength(), Client::MODE_DYNAMIC); - } -} diff --git a/src/Eloquent/Model.php b/src/Eloquent/Model.php deleted file mode 100644 index 0c6d94b..0000000 --- a/src/Eloquent/Model.php +++ /dev/null @@ -1,57 +0,0 @@ -{$model->getKeyName()} = $model->generateNanoid(); - }); - } -} diff --git a/src/HasNanoids.php b/src/HasNanoids.php new file mode 100644 index 0000000..00b1003 --- /dev/null +++ b/src/HasNanoids.php @@ -0,0 +1,83 @@ +{$model->getKeyName()} = $model->generateNanoid(); + }); + } + + /** + * Generate a nanoid. + */ + protected function generateNanoid(): string + { + return $this->getNanoIdPrefix().$this->newNanoId(); + } + + protected function newNanoId(): string + { + return (new Client())->generateId($this->getNanoidLength(), Client::MODE_DYNAMIC); + } + + protected function getNanoIdPrefix(): string + { + if (property_exists($this, 'nanoidPrefix')) { + return $this->nanoidPrefix; + } + + if (method_exists($this, 'nanoidPrefix')) { + return $this->nanoidPrefix(); + } + + return ''; + } + + /** + * Get the nanoid length. + */ + protected function getNanoidLength(): ?int + { + $nanoIdLength = null; + + if (property_exists($this, 'nanoidLength')) { + $nanoIdLength = $this->nanoidLength; + } + + if (method_exists($this, 'nanoidLength')) { + $nanoIdLength = $this->nanoidLength(); + } + + if (is_array($nanoIdLength)) { + return random_int($nanoIdLength[0], $nanoIdLength[1]); + } + + return $nanoIdLength; + } + + /** + * Get the auto-incrementing key type. + * + * @return string + */ + public function getKeyType() + { + return 'string'; + } + + /** + * Get the value indicating whether the IDs are incrementing. + * + * @return bool + */ + public function getIncrementing() + { + return false; + } +} diff --git a/tests/Console/Commands/NanoidModelMakeCommandTest.php b/tests/Console/Commands/NanoidModelMakeCommandTest.php index 4b64ee6..edca806 100644 --- a/tests/Console/Commands/NanoidModelMakeCommandTest.php +++ b/tests/Console/Commands/NanoidModelMakeCommandTest.php @@ -15,9 +15,12 @@ test('it generates a model', function () { $this->artisan(NanoidModelMakeCommand::class, ['name' => 'TestModel'])->assertExitCode(0); - $this->assertFileExists(app_path('Models/TestModel.php')); - $this->assertStringContainsString('use Malico\LaravelNanoid\Eloquent\Model;', file_get_contents(app_path('Models/TestModel.php'))); - $this->assertStringContainsString('extends Model', file_get_contents(app_path('Models/TestModel.php'))); + $modelContent = file_get_contents(app_path('Models/TestModel.php')); + + $this->assertStringContainsString('use Malico\LaravelNanoid\HasNanoids', $modelContent); + $this->assertStringContainsString('extends Model', $modelContent); + $this->assertStringContainsString('use HasNanoids;', $modelContent); + }); test('it generates a migration if specified', function () { diff --git a/tests/Eloquent/ModelTest.php b/tests/Eloquent/ModelTest.php deleted file mode 100644 index 59fbf40..0000000 --- a/tests/Eloquent/ModelTest.php +++ /dev/null @@ -1,53 +0,0 @@ -getKey())->toBeString(); -}); - -test('creates nanoid with prefix before saving', function () { - $model = BasicModelWithPrefix::create(); - - expect(Str::is('pl_*', $model->getKey()))->toBeTrue(); -}); - -test('creates nanoid with length before saving', function () { - $model = BasicModelWithLength::create(); - - expect(Str::length($model->getKey()))->toBe(3); -}); - -test('user model creates with appropriate id', function () { - $model = UserModel::create(); - - expect($model->getKey())->toBeString(); -}); diff --git a/tests/HasNanoidsTest.php b/tests/HasNanoidsTest.php new file mode 100644 index 0000000..8337f59 --- /dev/null +++ b/tests/HasNanoidsTest.php @@ -0,0 +1,113 @@ +getKey())->toBeString(); +}); + +it('creates nanoid with prefix before saving', function () { + $model = BasicModelWithPrefix::create(); + + expect(Str::is('pl_*', $model->getKey()))->toBeTrue(); +}); + +it('creates nanoid with length before saving', function () { + $model = BasicModelWithLength::create(); + + expect(Str::length($model->getKey()))->toBe(3); +}); + +it('creates nanoid with length array before saving', function () { + $model = BasicModelWithLengthArray::create(); + + expect(Str::length($model->getKey()))->toBeGreaterThanOrEqual(3); + expect(Str::length($model->getKey()))->toBeLessThanOrEqual(5); +}); + +it('creates nanoid with prefix and length before saving', function () { + $model = BasicModelWithPrefixAndLength::create(); + + expect(Str::is('pl_*', $model->getKey()))->toBeTrue(); + expect(Str::length($model->getKey()))->toBe(6); // 3 + 3 +}); + +it('creates nanoid with prefix and length array before saving', function () { + $model = BasicModelWithPrefixAndLengthArray::create(); + + expect(Str::is('pl_*', $model->getKey()))->toBeTrue(); + expect(Str::length($model->getKey()))->toBeGreaterThanOrEqual(6); // 3 + 3 + expect(Str::length($model->getKey()))->toBeLessThanOrEqual(8); // 3 + 5 +}); + +it('creates nanoid with prefix method before saving', function () { + $model = BasicModelWithPrefixMethod::create(); + + expect(Str::is('pl_*', $model->getKey()))->toBeTrue(); +}); + +it('creates nanoid with length method before saving', function () { + $model = BasicModelWithLengthMethod::create(); + + expect(Str::length($model->getKey()))->toBe(3); +}); + +abstract class ModelTest extends Model +{ + use HasNanoids; + + protected $table = 'test_migrations_with_string_id'; +} + +class BasicModel extends ModelTest +{ +} + +class BasicModelWithPrefix extends ModelTest +{ + protected $nanoidPrefix = 'pl_'; +} + +class BasicModelWithLength extends ModelTest +{ + protected $nanoidLength = 3; +} + +class BasicModelWithLengthArray extends ModelTest +{ + protected $nanoidLength = [3, 5]; +} + +class BasicModelWithPrefixAndLength extends ModelTest +{ + protected $nanoidPrefix = 'pl_'; + + protected $nanoidLength = 3; +} + +class BasicModelWithPrefixAndLengthArray extends ModelTest +{ + protected $nanoidPrefix = 'pl_'; + + protected $nanoidLength = [3, 5]; +} + +class BasicModelWithPrefixMethod extends ModelTest +{ + public function nanoidPrefix(): string + { + return 'pl_'; + } +} + +class BasicModelWithLengthMethod extends ModelTest +{ + public function nanoidLength(): int + { + return 3; + } +}