Skip to content

Commit 3c86240

Browse files
author
Gautier Deleglise
committed
🐛 morph to relation global usage
1 parent fdee3a7 commit 3c86240

File tree

10 files changed

+17
-105
lines changed

10 files changed

+17
-105
lines changed

src/Concerns/Resource/Relationable.php

Lines changed: 0 additions & 12 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Lomkit\Rest\Relations;
44

5+
use Illuminate\Database\Eloquent\Builder;
56
use Illuminate\Database\Eloquent\Model;
67
use Illuminate\Validation\Rule;
78
use Lomkit\Rest\Contracts\QueryBuilder;
@@ -10,18 +11,6 @@
1011

1112
class MorphTo extends MorphRelation implements RelationResource
1213
{
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-
2514
/**
2615
* Perform actions before mutating the MorphTo relation.
2716
*
@@ -34,29 +23,8 @@ public function beforeMutating(Model $model, Relation $relation, array $mutation
3423
$model
3524
->{$relation->relation}()
3625
->{$mutationRelations[$relation->relation]['operation'] === 'detach' ? 'dissociate' : 'associate'}(
37-
app()->make(QueryBuilder::class, ['resource' => new $mutationRelations[$relation->relation]['type']()])
26+
app()->make(QueryBuilder::class, ['resource' => $relation->resource()])
3827
->applyMutation($mutationRelations[$relation->relation])
3928
);
4029
}
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-
}
6230
}

src/Relations/Relation.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Closure;
66
use Illuminate\Database\Eloquent\Builder;
77
use Illuminate\Support\Str;
8+
use Illuminate\Validation\Rule;
89
use Lomkit\Rest\Concerns\Makeable;
910
use Lomkit\Rest\Concerns\Relations\HasPivotFields;
1011
use Lomkit\Rest\Http\Requests\RestRequest;
@@ -18,7 +19,7 @@ class Relation implements \JsonSerializable
1819
use Mutates;
1920
use Constrained;
2021
public string $relation;
21-
protected array $types;
22+
protected string $type;
2223

2324
/**
2425
* The displayable name of the relation.
@@ -32,7 +33,7 @@ class Relation implements \JsonSerializable
3233
public function __construct($relation, $type)
3334
{
3435
$this->relation = $relation;
35-
$this->types = [$type];
36+
$this->type = $type;
3637
}
3738

3839
/**
@@ -103,7 +104,7 @@ public function hasMultipleEntries()
103104
*/
104105
public function resource()
105106
{
106-
$resource = $this->types[0];
107+
$resource = $this->type;
107108

108109
// If the resource isn't registered, do it
109110
if (!app()->has($resource)) {
@@ -164,7 +165,7 @@ public function jsonSerialize(): mixed
164165
$request = app(RestRequest::class);
165166

166167
return [
167-
'resources' => $this->types,
168+
'resource' => $this->type,
168169
'relation' => $this->relation,
169170
'constraints' => [
170171
'required_on_creation' => $this->isRequiredOnCreation($request),

src/Rules/AggregateField.php

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 7 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -61,45 +61,6 @@ public function test_creating_a_resource_with_creating_first_morph_to_relation()
6161
$this->assertNotNull(Model::first()->morph_to_relation_type);
6262
}
6363

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-
10364
public function test_creating_a_resource_with_attaching_morph_to_relation(): void
10465
{
10566
$modelToCreate = ModelFactory::new()->makeOne();

tests/Feature/Controllers/MutateUpdateMorphOperationsTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ public function test_updating_a_resource_with_creating_morph_to_relation(): void
6666
'relations' => [
6767
'morphToRelation' => [
6868
'operation' => 'create',
69-
'type' => MorphToResource::class,
7069
'attributes' => [],
7170
],
7271
],
@@ -116,7 +115,6 @@ public function test_updating_a_resource_with_attaching_morph_to_relation(): voi
116115
'relations' => [
117116
'morphToRelation' => [
118117
'operation' => 'attach',
119-
'type' => MorphToResource::class,
120118
'key' => $morphToRelationToAttach->getKey(),
121119
],
122120
],
@@ -163,7 +161,6 @@ public function test_updating_a_resource_with_detaching_morph_to_relation(): voi
163161
'relations' => [
164162
'morphToRelation' => [
165163
'operation' => 'detach',
166-
'type' => MorphToResource::class,
167164
'key' => $morphToRelationToDetach->getKey(),
168165
],
169166
],
@@ -205,7 +202,6 @@ public function test_updating_a_resource_with_updating_morph_to_relation(): void
205202
'relations' => [
206203
'morphToRelation' => [
207204
'operation' => 'update',
208-
'type' => MorphToResource::class,
209205
'key' => $morphToRelationToUpdate->getKey(),
210206
'attributes' => ['number' => 5001], // 5001 because with factory it can't exceed 5000
211207
],

tests/Support/Rest/Resources/ModelResource.php

Lines changed: 1 addition & 1 deletion
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)