Skip to content

Commit f5b87e8

Browse files
committed
Automatically resolve resource type from class
Signed-off-by: Mior Muhammad Zaki <[email protected]>
1 parent 397c25b commit f5b87e8

File tree

11 files changed

+38
-80
lines changed

11 files changed

+38
-80
lines changed

src/Illuminate/Http/Resources/JsonApi/AnonymousResourceCollection.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function with($request)
2626
->flatten(depth: 1)
2727
->uniqueStrict('_uniqueKey')
2828
->map(fn ($included) => Arr::except($included, ['_uniqueKey']))
29+
->values()
2930
->all(),
3031
...($implementation = JsonApiResource::$jsonApiInformation)
3132
? ['jsonapi' => $implementation]

src/Illuminate/Http/Resources/JsonApi/Concerns/ResolvesJsonApiElements.php

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ public function resolveResourceIdentifier(JsonApiRequest $request): string
7777
return $resourceId;
7878
}
7979

80-
if (! $this->resource instanceof Model) {
80+
if (! ($this->resource instanceof Model || method_exists($this->resource, 'getKey'))) {
8181
throw ResourceIdentificationException::attemptingToDetermineIdFor($this);
8282
}
8383

84-
return static::resourceIdFromModel($this->resource);
84+
return (string) $this->resource->getKey();
8585
}
8686

8787
/**
@@ -96,11 +96,21 @@ public function resolveResourceType(JsonApiRequest $request): string
9696
return $resourceType;
9797
}
9898

99+
if (static::class !== JsonApiResource::class) {
100+
return Str::of(static::class)->classBasename()->basename('Resource')->snake()->pluralStudly();
101+
}
102+
99103
if (! $this->resource instanceof Model) {
100104
throw ResourceIdentificationException::attemptingToDetermineTypeFor($this);
101105
}
102106

103-
return static::resourceTypeFromModel($this->resource);
107+
$modelClassName = $this->resource::class;
108+
109+
$morphMap = Relation::getMorphAlias($modelClassName);
110+
111+
return Str::of(
112+
$morphMap !== $modelClassName ? $morphMap : class_basename($modelClassName)
113+
)->snake()->pluralStudly();
104114
}
105115

106116
/**
@@ -358,28 +368,6 @@ protected function resolveResourceMetaInformation(JsonApiRequest $request): arra
358368
return $this->toMeta($request);
359369
}
360370

361-
/**
362-
* Get the resource ID from the given Eloquent model.
363-
*/
364-
public static function resourceIdFromModel(Model $model): string
365-
{
366-
return $model->getKey();
367-
}
368-
369-
/**
370-
* Get the resource type from the given Eloquent model.
371-
*/
372-
public static function resourceTypeFromModel(Model $model): string
373-
{
374-
$modelClassName = $model::class;
375-
376-
$morphMap = Relation::getMorphAlias($modelClassName);
377-
378-
return static::normalizeResourceType(
379-
$morphMap !== $modelClassName ? $morphMap : class_basename($modelClassName)
380-
);
381-
}
382-
383371
/**
384372
* Indicate that relationship loading should respect the request's "includes" query string.
385373
*
@@ -413,12 +401,4 @@ public function includePreviouslyLoadedRelationships()
413401

414402
return $this;
415403
}
416-
417-
/**
418-
* Normalize the resource type.
419-
*/
420-
public static function normalizeResourceType(string $value): string
421-
{
422-
return Str::of($value)->snake()->pluralStudly();
423-
}
424404
}

src/Illuminate/Http/Resources/JsonApi/JsonApiResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ public function with($request)
136136
'included' => $this->resolveIncludedResourceObjects($request)
137137
->uniqueStrict('_uniqueKey')
138138
->map(fn ($included) => Arr::except($included, ['_uniqueKey']))
139+
->values()
139140
->all(),
140141
...($implementation = static::$jsonApiInformation)
141142
? ['jsonapi' => $implementation]

tests/Integration/Http/Resources/JsonApi/Fixtures/AuthorApiResource.php renamed to tests/Integration/Http/Resources/JsonApi/Fixtures/AuthorResource.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Illuminate\Http\Request;
66
use Illuminate\Http\Resources\JsonApi\JsonApiResource;
77

8-
class AuthorApiResource extends JsonApiResource
8+
class AuthorResource extends JsonApiResource
99
{
1010
protected array $relationships = [
1111
'comments',
@@ -20,10 +20,4 @@ public function toAttributes(Request $request)
2020
'email' => $this->email,
2121
];
2222
}
23-
24-
#[\Override]
25-
public function toType(Request $request)
26-
{
27-
return 'authors';
28-
}
2923
}

tests/Integration/Http/Resources/JsonApi/Fixtures/Comment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Illuminate\Database\Eloquent\Model;
99

1010
#[UseFactory(CommentFactory::class)]
11-
#[UseResource(CommentApiResource::class)]
11+
#[UseResource(CommentResource::class)]
1212
class Comment extends Model
1313
{
1414
use HasFactory;

tests/Integration/Http/Resources/JsonApi/Fixtures/CommentApiResource.php renamed to tests/Integration/Http/Resources/JsonApi/Fixtures/CommentResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Illuminate\Http\Resources\JsonApi\JsonApiResource;
66

7-
class CommentApiResource extends JsonApiResource
7+
class CommentResource extends JsonApiResource
88
{
99
/**
1010
* The resource's attributes.

tests/Integration/Http/Resources/JsonApi/Fixtures/Post.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Illuminate\Database\Eloquent\Model;
99

1010
#[UseFactory(PostFactory::class)]
11-
#[UseResource(PostApiResource::class)]
11+
#[UseResource(PostResource::class)]
1212
class Post extends Model
1313
{
1414
use HasFactory;

tests/Integration/Http/Resources/JsonApi/Fixtures/PostApiResource.php renamed to tests/Integration/Http/Resources/JsonApi/Fixtures/PostResource.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
use Illuminate\Http\Resources\JsonApi\JsonApiResource;
66

7-
class PostApiResource extends JsonApiResource
7+
class PostResource extends JsonApiResource
88
{
99
protected array $attributes = [
1010
'title',
1111
'content',
1212
];
1313

1414
protected array $relationships = [
15-
'author' => AuthorApiResource::class,
15+
'author' => AuthorResource::class,
1616
'comments',
1717
];
1818
}

tests/Integration/Http/Resources/JsonApi/Fixtures/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Illuminate\Foundation\Auth\User as Authenticatable;
99
use Orchestra\Testbench\Factories\UserFactory;
1010

11-
#[UseResource(UserApiResource::class)]
11+
#[UseResource(UserResource::class)]
1212
#[UseFactory(UserFactory::class)]
1313
class User extends Authenticatable
1414
{

tests/Integration/Http/Resources/JsonApi/Fixtures/UserApiResource.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)