From 97f1f8fcb413b5b144cd643b29e992041c6e7ab5 Mon Sep 17 00:00:00 2001 From: Enrico De Lazzari Date: Mon, 4 Dec 2023 14:57:43 +0100 Subject: [PATCH] ADD resolveMorphedModel helper (#19) --- README.md | 13 +++++++++++++ src/Helper.php | 2 ++ src/Macros/ResolveMorphedModel.php | 18 ++++++++++++++++++ tests/HelpersTest.php | 18 ++++++++++++++++-- tests/Support/Models/Post.php | 15 +++++++++++++++ 5 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/Macros/ResolveMorphedModel.php create mode 100644 tests/Support/Models/Post.php diff --git a/README.md b/README.md index b8c32d8..6d4992c 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ hlp()->sanitizeUrl('mywebsite.com'); // using the helper function - [`isUrl`](#isurl) - [`modelKeyName`](#modelkeyname) - [`morphClassOf`](#morphclassof) +- [`resolveMorphedModel`](#resolvemorphedmodel) - [`paginationLimit`](#paginationlimit) - [`pipe`](#pipe) - [`sanitizeArrayOfStrings`](#sanitizearrayofstrings) @@ -170,6 +171,18 @@ hlp()->morphClassOf($model); // returns 'user' hlp()->morphClassOf(User::class); // returns 'user' ``` +### `resolveMorphedModel` + +The `resolveMorphedModel` function returns the fully qualified model class name of a given morph class name. + +You can either pass the singular or plural name of the morph class name. + +```php +hlp()->resolveMorphedModel('users'); // returns 'App\Models\User' + +hlp()->resolveMorphedModel('user'); // returns 'App\Models\User' +``` + ### `paginationLimit` The `paginationLimit` function returns the amount of items per page. diff --git a/src/Helper.php b/src/Helper.php index 01bd6c3..6b4df00 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -13,6 +13,7 @@ * @method bool isUrl(mixed $url) * @method string modelKeyName(\Illuminate\Database\Eloquent\Model|string $model) * @method string morphClassOf(\Illuminate\Database\Eloquent\Model|string $model) + * @method string resolveMorphedModel(string $alias) * @method int paginationLimit(int $default = 16, int $max = 48) * @method mixed pipe(mixed $passable, mixed $pipes) * @method array|null sanitizeArrayOfStrings(array|null $value, array|string|null $only = null) @@ -39,6 +40,7 @@ public static function defaultMacros(): Collection 'isUrl' => \Maize\Helpers\Macros\IsUrl::class, 'modelKeyName' => \Maize\Helpers\Macros\ModelKeyName::class, 'morphClassOf' => \Maize\Helpers\Macros\MorphClassOf::class, + 'resolveMorphedModel' => \Maize\Helpers\Macros\ResolveMorphedModel::class, 'paginationLimit' => \Maize\Helpers\Macros\PaginationLimit::class, 'pipe' => \Maize\Helpers\Macros\Pipe::class, 'sanitizeArrayOfStrings' => \Maize\Helpers\Macros\SanitizeArrayOfStrings::class, diff --git a/src/Macros/ResolveMorphedModel.php b/src/Macros/ResolveMorphedModel.php new file mode 100644 index 0000000..0bde9cb --- /dev/null +++ b/src/Macros/ResolveMorphedModel.php @@ -0,0 +1,18 @@ +singular($alias) + ) ?? $alias; + }; + } +} diff --git a/tests/HelpersTest.php b/tests/HelpersTest.php index 4a67470..3a365f3 100644 --- a/tests/HelpersTest.php +++ b/tests/HelpersTest.php @@ -2,11 +2,17 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Foundation\Auth\User; use Maize\Helpers\Tests\Support\Models\Article; +use Maize\Helpers\Tests\Support\Models\Post; use function PHPUnit\Framework\assertNotEquals; +beforeEach(function () { + Relation::morphMap(['article' => Article::class]); +}); + it('anonymizeFilename', function (?string $name, ?string $ext) { $filename = "{$name}{$ext}"; $expect = expect(hlp()->anonymizeFilename($filename)); @@ -70,8 +76,16 @@ it('morphClassOf', function (mixed $model, string $result) { expect(hlp()->morphClassOf($model))->toBe($result); })->with([ - ['model' => Article::class, 'result' => Article::class], - ['model' => new Article(), 'result' => Article::class], + ['model' => Article::class, 'result' => 'article'], + ['model' => new Article(), 'result' => 'article'], +]); + +it('resolveMorphedModel', function (mixed $model, ?string $result) { + expect(hlp()->resolveMorphedModel($model))->toBe($result); +})->with([ + ['model' => 'article', 'result' => Article::class], + ['model' => 'articles', 'result' => Article::class], + ['model' => Post::class, 'result' => Post::class], ]); it('paginationLimit', function (?int $limit, ?int $default, ?int $max, int $result) { diff --git a/tests/Support/Models/Post.php b/tests/Support/Models/Post.php new file mode 100644 index 0000000..b279e16 --- /dev/null +++ b/tests/Support/Models/Post.php @@ -0,0 +1,15 @@ +