Skip to content

Commit 5ffbb58

Browse files
committed
♻️ type changed to resource from model
1 parent 1f0bae3 commit 5ffbb58

File tree

4 files changed

+13
-24
lines changed

4 files changed

+13
-24
lines changed

src/Relations/MorphTo.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ public function beforeMutating(Model $model, Relation $relation, array $mutation
2323
$model
2424
->{$relation->relation}()
2525
->{$mutationRelations[$relation->relation]['operation'] === 'detach' ? 'dissociate' : 'associate'}(
26-
app()->make(QueryBuilder::class, ['resource' => $relation->resourceForModel(
27-
$mutationRelations[$relation->relation]['type']
28-
)])
26+
app()->make(QueryBuilder::class, ['resource' => new $mutationRelations[$relation->relation]['type']])
2927
->applyMutation($mutationRelations[$relation->relation])
3028
);
3129
}
@@ -37,7 +35,7 @@ public function rules(Resource $resource, string $prefix)
3735
$prefix.'.type' => [
3836
'required_with:'.$prefix,
3937
Rule::in(
40-
array_map(function ($type) {return (new $type)::$model;}, $this->types)
38+
$this->types
4139
)
4240
]
4341
];

src/Relations/Relation.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,6 @@ public function resource() {
5959
return new $this->types[0];
6060
}
6161

62-
public function resourceForModel(string $model) {
63-
$array =
64-
array_filter(
65-
$this->types,
66-
function ($type) use ($model) {
67-
return (new $type)::$model === $model;
68-
}
69-
);
70-
return new (reset($array));
71-
72-
}
73-
7462
public function fromResource(Resource $fromResource) {
7563
return tap($this, function () use ($fromResource) {
7664
$this->fromResource = $fromResource;

tests/Feature/Controllers/MutateCreateMorphOperationsTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
use Lomkit\Rest\Tests\Support\Models\MorphToRelation;
2727
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;
2828
use Lomkit\Rest\Tests\Support\Rest\Resources\ModelResource;
29+
use Lomkit\Rest\Tests\Support\Rest\Resources\MorphedByManyResource;
30+
use Lomkit\Rest\Tests\Support\Rest\Resources\MorphToResource;
2931

3032
class MutateCreateMorphOperationsTest extends TestCase
3133
{
@@ -49,7 +51,7 @@ public function test_creating_a_resource_with_creating_first_morph_to_relation()
4951
'relations' => [
5052
'morphToRelation' => [
5153
'operation' => 'create',
52-
'type' => MorphToRelation::class,
54+
'type' => MorphToResource::class,
5355
'attributes' => []
5456
]
5557
]
@@ -89,7 +91,7 @@ public function test_creating_a_resource_with_creating_second_morph_to_relation(
8991
'relations' => [
9092
'morphToRelation' => [
9193
'operation' => 'create',
92-
'type' => MorphedByManyRelation::class,
94+
'type' => MorphedByManyResource::class,
9395
'attributes' => []
9496
]
9597
]
@@ -130,7 +132,7 @@ public function test_creating_a_resource_with_attaching_morph_to_relation(): voi
130132
'relations' => [
131133
'morphToRelation' => [
132134
'operation' => 'attach',
133-
'type' => MorphToRelation::class,
135+
'type' => MorphToResource::class,
134136
'key' => $morphToRelationToAttach->getKey()
135137
]
136138
]
@@ -177,7 +179,7 @@ public function test_creating_a_resource_with_updating_morph_to_relation(): void
177179
'morphToRelation' => [
178180
'operation' => 'update',
179181
'key' => $morphToRelationToUpdate->getKey(),
180-
'type' => MorphToRelation::class,
182+
'type' => MorphToResource::class,
181183
'attributes' => ['number' => 5001] // 5001 because with factory it can't exceed 5000
182184
]
183185
]

tests/Feature/Controllers/MutateUpdateMorphOperationsTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Lomkit\Rest\Tests\Support\Models\MorphToRelation;
2727
use Lomkit\Rest\Tests\Support\Policies\GreenPolicy;
2828
use Lomkit\Rest\Tests\Support\Rest\Resources\ModelResource;
29+
use Lomkit\Rest\Tests\Support\Rest\Resources\MorphToResource;
2930

3031
class MutateUpdateMorphOperationsTest extends TestCase
3132
{
@@ -50,7 +51,7 @@ public function test_updating_a_resource_with_creating_morph_to_relation(): void
5051
'relations' => [
5152
'morphToRelation' => [
5253
'operation' => 'create',
53-
'type' => MorphToRelation::class,
54+
'type' => MorphToResource::class,
5455
'attributes' => []
5556
]
5657
]
@@ -100,7 +101,7 @@ public function test_updating_a_resource_with_attaching_morph_to_relation(): voi
100101
'relations' => [
101102
'morphToRelation' => [
102103
'operation' => 'attach',
103-
'type' => MorphToRelation::class,
104+
'type' => MorphToResource::class,
104105
'key' => $morphToRelationToAttach->getKey()
105106
]
106107
]
@@ -147,7 +148,7 @@ public function test_updating_a_resource_with_detaching_morph_to_relation(): voi
147148
'relations' => [
148149
'morphToRelation' => [
149150
'operation' => 'detach',
150-
'type' => MorphToRelation::class,
151+
'type' => MorphToResource::class,
151152
'key' => $morphToRelationToDetach->getKey()
152153
]
153154
]
@@ -189,7 +190,7 @@ public function test_updating_a_resource_with_updating_morph_to_relation(): void
189190
'relations' => [
190191
'morphToRelation' => [
191192
'operation' => 'update',
192-
'type' => MorphToRelation::class,
193+
'type' => MorphToResource::class,
193194
'key' => $morphToRelationToUpdate->getKey(),
194195
'attributes' => ['number' => 5001] // 5001 because with factory it can't exceed 5000
195196
]

0 commit comments

Comments
 (0)