Skip to content

Commit 6063ef9

Browse files
authored
Merge pull request #89 from Lomkit/fix/morph-to-relation
Fix/morph to relation
2 parents fdee3a7 + 92906f7 commit 6063ef9

File tree

10 files changed

+15
-109
lines changed

10 files changed

+15
-109
lines changed

src/Concerns/Resource/Relationable.php

-12
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,6 @@ public function relation($name)
3434
return $relation;
3535
}
3636

37-
/**
38-
* Get the resource associated with a relation by name.
39-
*
40-
* @param string $name
41-
*
42-
* @return \Lomkit\Rest\Http\Resource
43-
*/
44-
public function relationResource($name)
45-
{
46-
return $this->relation($name)?->resource();
47-
}
48-
4937
/**
5038
* The calculated relations if already done in this request.
5139
*

src/Query/Traits/PerformSearch.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public function applyInstructions($instructions)
192192
public function include($include)
193193
{
194194
return $this->queryBuilder->with($include['relation'], function (Relation $query) use ($include) {
195-
$resource = $this->resource->relationResource($include['relation']);
195+
$resource = $this->resource->relation($include['relation'])?->resource();
196196

197197
$queryBuilder = $this->newQueryBuilder(['resource' => $resource, 'query' => $query]);
198198

@@ -220,7 +220,7 @@ public function applyIncludes($includes)
220220
public function aggregate($aggregate)
221221
{
222222
return $this->queryBuilder->withAggregate([$aggregate['relation'] => function (Builder $query) use ($aggregate) {
223-
$resource = $this->resource->relationResource($aggregate['relation']);
223+
$resource = $this->resource->relation($aggregate['relation'])?->resource();
224224

225225
$queryBuilder = $this->newQueryBuilder(['resource' => $resource, 'query' => $query]);
226226

src/Relations/MorphTo.php

+1-36
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,11 @@
33
namespace Lomkit\Rest\Relations;
44

55
use Illuminate\Database\Eloquent\Model;
6-
use Illuminate\Validation\Rule;
76
use Lomkit\Rest\Contracts\QueryBuilder;
87
use Lomkit\Rest\Contracts\RelationResource;
9-
use Lomkit\Rest\Http\Resource;
108

119
class MorphTo extends MorphRelation implements RelationResource
1210
{
13-
/**
14-
* Create a new MorphTo instance.
15-
*
16-
* @param string $relation The name of the relation.
17-
* @param array $types An array of allowed types for the relation.
18-
*/
19-
public function __construct($relation, array $types)
20-
{
21-
$this->relation = $relation;
22-
$this->types = $types;
23-
}
24-
2511
/**
2612
* Perform actions before mutating the MorphTo relation.
2713
*
@@ -34,29 +20,8 @@ public function beforeMutating(Model $model, Relation $relation, array $mutation
3420
$model
3521
->{$relation->relation}()
3622
->{$mutationRelations[$relation->relation]['operation'] === 'detach' ? 'dissociate' : 'associate'}(
37-
app()->make(QueryBuilder::class, ['resource' => new $mutationRelations[$relation->relation]['type']()])
23+
app()->make(QueryBuilder::class, ['resource' => $relation->resource()])
3824
->applyMutation($mutationRelations[$relation->relation])
3925
);
4026
}
41-
42-
/**
43-
* Define validation rules for the MorphTo relation.
44-
*
45-
* @param resource $resource The resource associated with the relation.
46-
* @param string $prefix The prefix used for validation rules.
47-
*
48-
* @return array An array of validation rules.
49-
*/
50-
public function rules(Resource $resource, string $prefix)
51-
{
52-
return [
53-
...parent::rules($resource, $prefix),
54-
$prefix.'.type' => [
55-
'required_with:'.$prefix,
56-
Rule::in(
57-
$this->types
58-
),
59-
],
60-
];
61-
}
6227
}

src/Relations/Relation.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Relation implements \JsonSerializable
1818
use Mutates;
1919
use Constrained;
2020
public string $relation;
21-
protected array $types;
21+
protected string $type;
2222

2323
/**
2424
* The displayable name of the relation.
@@ -32,7 +32,7 @@ class Relation implements \JsonSerializable
3232
public function __construct($relation, $type)
3333
{
3434
$this->relation = $relation;
35-
$this->types = [$type];
35+
$this->type = $type;
3636
}
3737

3838
/**
@@ -103,7 +103,7 @@ public function hasMultipleEntries()
103103
*/
104104
public function resource()
105105
{
106-
$resource = $this->types[0];
106+
$resource = $this->type;
107107

108108
// If the resource isn't registered, do it
109109
if (!app()->has($resource)) {
@@ -164,7 +164,7 @@ public function jsonSerialize(): mixed
164164
$request = app(RestRequest::class);
165165

166166
return [
167-
'resources' => $this->types,
167+
'resource' => $this->type,
168168
'relation' => $this->relation,
169169
'constraints' => [
170170
'required_on_creation' => $this->isRequiredOnCreation($request),

src/Rules/AggregateField.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function passes($attribute, $value)
8686
*/
8787
protected function buildValidationRules($attribute, $value)
8888
{
89-
$relationResource = $this->resource->relationResource($value['relation']);
89+
$relationResource = $this->resource->relation($value['relation'])?->resource();
9090

9191
if (is_null($relationResource)) {
9292
return [];

src/Rules/Includable.php

+5-7
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,18 @@ public function setValidator(\Illuminate\Validation\Validator $validator): stati
5656
*/
5757
public function validate(string $attribute, mixed $value, Closure $fail): void
5858
{
59-
$relationResource = $this->resource->relationResource($value['relation']);
59+
$resource = $this->resource->relation($value['relation'])?->resource();
6060

61-
if (is_null($relationResource)) {
61+
if (is_null($resource)) {
6262
return;
6363
}
6464

6565
$this
6666
->validator
6767
->setRules(
68-
is_null($relationResource) ?
69-
[] :
70-
[
71-
$attribute => [new SearchRules($relationResource, app(RestRequest::class), false)],
72-
]
68+
[
69+
$attribute => [new SearchRules($resource, app(RestRequest::class), false)],
70+
]
7371
)
7472
->validate();
7573
}

src/Rules/NestedRelation.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function validate(string $attribute, mixed $value, Closure $fail): void
6060
$relationResource = $this->resource;
6161

6262
do {
63-
$relationResource = $relationResource->relationResource(Str::before($relation, '.'));
63+
$relationResource = $relationResource->relation(Str::before($relation, '.'))?->resource();
6464

6565
if ($relationResource === null) {
6666
$fail('The relation is not allowed');

tests/Feature/Controllers/MutateCreateMorphOperationsTest.php

-40
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Lomkit\Rest\Tests\Support\Models\MorphToManyRelation;
1818
use Lomkit\Rest\Tests\Support\Models\MorphToRelation;
1919
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;
20-
use Lomkit\Rest\Tests\Support\Rest\Resources\MorphedByManyResource;
2120
use Lomkit\Rest\Tests\Support\Rest\Resources\MorphToResource;
2221

2322
class MutateCreateMorphOperationsTest extends TestCase
@@ -61,45 +60,6 @@ public function test_creating_a_resource_with_creating_first_morph_to_relation()
6160
$this->assertNotNull(Model::first()->morph_to_relation_type);
6261
}
6362

64-
public function test_creating_a_resource_with_creating_second_morph_to_relation(): void
65-
{
66-
$modelToCreate = ModelFactory::new()->makeOne();
67-
68-
Gate::policy(Model::class, GreenPolicy::class);
69-
Gate::policy(MorphedByManyRelation::class, GreenPolicy::class);
70-
71-
$response = $this->post(
72-
'/api/models/mutate',
73-
[
74-
'mutate' => [
75-
[
76-
'operation' => 'create',
77-
'attributes' => [
78-
'name' => $modelToCreate->name,
79-
'number' => $modelToCreate->number,
80-
],
81-
'relations' => [
82-
'morphToRelation' => [
83-
'operation' => 'create',
84-
'type' => MorphedByManyResource::class,
85-
'attributes' => [],
86-
],
87-
],
88-
],
89-
],
90-
],
91-
['Accept' => 'application/json']
92-
);
93-
94-
$this->assertMutatedResponse(
95-
$response,
96-
[$modelToCreate],
97-
);
98-
99-
$this->assertNotNull(Model::first()->morph_to_relation_id);
100-
$this->assertNotNull(Model::first()->morph_to_relation_type);
101-
}
102-
10363
public function test_creating_a_resource_with_attaching_morph_to_relation(): void
10464
{
10565
$modelToCreate = ModelFactory::new()->makeOne();

tests/Feature/Controllers/MutateUpdateMorphOperationsTest.php

-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use Lomkit\Rest\Tests\Support\Models\MorphToManyRelation;
1818
use Lomkit\Rest\Tests\Support\Models\MorphToRelation;
1919
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;
20-
use Lomkit\Rest\Tests\Support\Rest\Resources\MorphToResource;
2120

2221
class MutateUpdateMorphOperationsTest extends TestCase
2322
{
@@ -66,7 +65,6 @@ public function test_updating_a_resource_with_creating_morph_to_relation(): void
6665
'relations' => [
6766
'morphToRelation' => [
6867
'operation' => 'create',
69-
'type' => MorphToResource::class,
7068
'attributes' => [],
7169
],
7270
],
@@ -116,7 +114,6 @@ public function test_updating_a_resource_with_attaching_morph_to_relation(): voi
116114
'relations' => [
117115
'morphToRelation' => [
118116
'operation' => 'attach',
119-
'type' => MorphToResource::class,
120117
'key' => $morphToRelationToAttach->getKey(),
121118
],
122119
],
@@ -163,7 +160,6 @@ public function test_updating_a_resource_with_detaching_morph_to_relation(): voi
163160
'relations' => [
164161
'morphToRelation' => [
165162
'operation' => 'detach',
166-
'type' => MorphToResource::class,
167163
'key' => $morphToRelationToDetach->getKey(),
168164
],
169165
],
@@ -205,7 +201,6 @@ public function test_updating_a_resource_with_updating_morph_to_relation(): void
205201
'relations' => [
206202
'morphToRelation' => [
207203
'operation' => 'update',
208-
'type' => MorphToResource::class,
209204
'key' => $morphToRelationToUpdate->getKey(),
210205
'attributes' => ['number' => 5001], // 5001 because with factory it can't exceed 5000
211206
],

tests/Support/Rest/Resources/ModelResource.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function relations(RestRequest $request): array
6464
HasManyThrough::make('hasManyThroughRelation', HasManyThroughResource::class),
6565

6666
// Morph relationships
67-
MorphTo::make('morphToRelation', [MorphToResource::class, MorphedByManyResource::class]),
67+
MorphTo::make('morphToRelation', MorphToResource::class),
6868
MorphOne::make('morphOneRelation', MorphOneResource::class),
6969
MorphOneOfMany::make('morphOneOfManyRelation', MorphOneOfManyResource::class),
7070
MorphMany::make('morphManyRelation', MorphManyResource::class),

0 commit comments

Comments
 (0)