Skip to content

Commit

Permalink
ADD resolveMorphedModel helper (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
enricodelazzari authored Dec 4, 2023
1 parent f95fca8 commit 97f1f8f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions src/Macros/ResolveMorphedModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Maize\Helpers\Macros;

use Illuminate\Database\Eloquent\Relations\Relation;
use Maize\Helpers\HelperMacro;

class ResolveMorphedModel implements HelperMacro
{
public function __invoke(): \Closure
{
return function (string $alias): string {
return Relation::getMorphedModel(
str()->singular($alias)
) ?? $alias;
};
}
}
18 changes: 16 additions & 2 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions tests/Support/Models/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Maize\Helpers\Tests\Support\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
use HasFactory;

protected $fillable = [
//
];
}

0 comments on commit 97f1f8f

Please sign in to comment.