Skip to content

Commit d9dbf86

Browse files
authored
Merge pull request #207 from RonasIT/202-resource-fields
[202] resource fields
2 parents db58b8f + ecfdb49 commit d9dbf86

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

src/Generators/ResourceGenerator.php

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

33
namespace RonasIT\Support\Generators;
44

5+
use Illuminate\Support\Arr;
56
use RonasIT\Support\Events\SuccessCreateMessage;
67
use RonasIT\Support\Exceptions\ResourceAlreadyExistsException;
78

@@ -53,6 +54,7 @@ public function generateResource(): void
5354
'entity' => $this->model,
5455
'namespace' => $this->getNamespace('resources'),
5556
'model_namespace' => $this->getNamespace('models', $this->modelSubFolder),
57+
'fields' => when($this->fields, fn () => Arr::flatten($this->fields)),
5658
]);
5759

5860
$this->saveClass('resources', "{$this->model}Resource", $resourceContent, $this->model);

stubs/resource.blade.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,19 @@
88
*/
99
class {{ $entity }}Resource extends BaseResource
1010
{
11+
@if (empty($fields))
1112
//TODO implement custom serialization logic or remove method redefining
13+
@endif
1214
public function toArray($request): array
1315
{
14-
return parent::toArray($request);
15-
}
16+
@if (!empty($fields))
17+
return [
18+
@foreach($fields as $field)
19+
'{{ $field }}' => $this->resource->{{ $field }},
20+
@endforeach
21+
];
22+
@else
23+
return parent::toArray($request);
24+
@endif
25+
}
1626
}

tests/ResourceGeneratorTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,36 @@ public function testCreateResources()
6969
]);
7070
}
7171

72+
public function testCreateResourcesWithFields()
73+
{
74+
app(ResourceGenerator::class)
75+
->setModel('Post')
76+
->setFields([
77+
'integer' => ['priority'],
78+
'integer-required' => ['media_id'],
79+
'float' => ['seo_score'],
80+
'float-required' => ['rating'],
81+
'string' => ['description'],
82+
'string-required' => ['title'],
83+
'boolean' => ['is_reviewed'],
84+
'boolean-required' => ['is_published'],
85+
'timestamp' => ['reviewed_at', 'created_at', 'updated_at'],
86+
'timestamp-required' => ['published_at'],
87+
'json' => ['meta'],
88+
])
89+
->generate();
90+
91+
$this->assertGeneratedFileEquals('post_resource_with_fields.php', 'app/Http/Resources/Post/PostResource.php');
92+
$this->assertGeneratedFileEquals('post_collection_resource.php', 'app/Http/Resources/Post/PostsCollectionResource.php');
93+
94+
$this->assertEventPushedChain([
95+
SuccessCreateMessage::class => [
96+
'Created a new Resource: PostResource',
97+
'Created a new CollectionResource: PostsCollectionResource',
98+
],
99+
]);
100+
}
101+
72102
public function testCreateResourcesResourceStubNotExist()
73103
{
74104
config(['entity-generator.stubs.resource' => 'incorrect_stub']);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace App\Http\Resources\Post;
4+
5+
use RonasIT\Support\Http\BaseResource;
6+
use App\Models\Post;
7+
8+
/**
9+
* @property Post $resource
10+
*/
11+
class PostResource extends BaseResource
12+
{
13+
public function toArray($request): array
14+
{
15+
return [
16+
'priority' => $this->resource->priority,
17+
'media_id' => $this->resource->media_id,
18+
'seo_score' => $this->resource->seo_score,
19+
'rating' => $this->resource->rating,
20+
'description' => $this->resource->description,
21+
'title' => $this->resource->title,
22+
'is_reviewed' => $this->resource->is_reviewed,
23+
'is_published' => $this->resource->is_published,
24+
'reviewed_at' => $this->resource->reviewed_at,
25+
'created_at' => $this->resource->created_at,
26+
'updated_at' => $this->resource->updated_at,
27+
'published_at' => $this->resource->published_at,
28+
'meta' => $this->resource->meta,
29+
];
30+
}
31+
}

0 commit comments

Comments
 (0)