generated from red-explosion/laravel-package-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fa2f86
commit abcc448
Showing
13 changed files
with
156 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RedExplosion\Sqids\Contracts; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
interface Prefix | ||
{ | ||
/** | ||
* @param class-string<Model> $model | ||
* @return string | ||
*/ | ||
public function prefix(string $model): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RedExplosion\Sqids\Prefixes; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Str; | ||
use RedExplosion\Sqids\Contracts\Prefix; | ||
|
||
class ConstantPrefix implements Prefix | ||
{ | ||
/** | ||
* Use the first 3 constants as the model prefix. | ||
* | ||
* @param class-string<Model> $model | ||
* @return string | ||
*/ | ||
public function prefix(string $model): string | ||
{ | ||
$classBasename = class_basename(class: $model); | ||
|
||
$prefix = str_replace(search: ['a', 'e', 'i', 'o', 'u'], replace: '', subject: $classBasename); | ||
|
||
$prefix = rtrim(mb_strimwidth(string: $prefix, start: 0, width: 3, encoding: 'UTF-8')); | ||
|
||
return Str::lower(value: $prefix); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RedExplosion\Sqids\Prefixes; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Str; | ||
use RedExplosion\Sqids\Contracts\Prefix; | ||
|
||
class SimplePrefix implements Prefix | ||
{ | ||
/** | ||
* Use the first 3 characters as the model prefix. | ||
* | ||
* @param class-string<Model> $model | ||
* @return string | ||
*/ | ||
public function prefix(string $model): string | ||
{ | ||
$classBasename = class_basename(class: $model); | ||
|
||
$prefix = rtrim(mb_strimwidth(string: $classBasename, start: 0, width: 3, encoding: 'UTF-8')); | ||
|
||
return Str::lower(value: $prefix); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use RedExplosion\Sqids\Prefixes\ConstantPrefix; | ||
use Workbench\App\Models\Charge; | ||
use Workbench\App\Models\Customer; | ||
|
||
it(description: 'can generate a prefix without vowels', closure: function (): void { | ||
expect(new ConstantPrefix()) | ||
->prefix(model: Customer::class) | ||
->toBe(expected: 'cst') | ||
->prefix(model: Charge::class) | ||
->toBe(expected: 'chr'); | ||
; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use RedExplosion\Sqids\Prefixes\SimplePrefix; | ||
use Workbench\App\Models\Charge; | ||
use Workbench\App\Models\Customer; | ||
|
||
it(description: 'can generate a default prefix', closure: function (): void { | ||
expect(new SimplePrefix()) | ||
->prefix(model: Customer::class) | ||
->toBe(expected: 'cus') | ||
->prefix(model: Charge::class) | ||
->toBe(expected: 'cha'); | ||
; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters