Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonResource #1

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/JsonResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Baethon\Laravel\Resource;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Illuminate\Http\Resources\Json\JsonResource as BaseResource;

class JsonResource extends BaseResource
{
public function toArray($request)
{
if (is_null($this->resource)) {
return [];
}

if (is_array($this->resource)) {
return $this->resource;
}

if ($this->resource instanceof Model) {
return array_merge(
$this->resource->attributesToArray(),
$this->wrapRelations($request)
);
}

return $this->resource->toArray();
}

private function wrapRelations($request): array
{
$getRelations = (function () {
return $this->getArrayableRelations();
})->bindTo($this->resource, $this->resource);

$relations = [];

foreach ($getRelations() as $key => $value) {
if ($this->resource::$snakeAttributes) {
$key = Str::snake($key);
}

$relations[$key] = resource($value)->toArray($request);
}

return $relations;
}
}
66 changes: 66 additions & 0 deletions tests/Integration/JsonResourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Tests\Unit;

use App\Models\Post;
use App\Models\Tag;
use Baethon\Laravel\Resource\JsonResource;
use Baethon\Laravel\Resource\ServiceProvider;
use Illuminate\Contracts\Support\Arrayable;
use Orchestra\Testbench\TestCase;

class JsonResourceTest extends TestCase
{
protected function getPackageProviders($app)
{
return [
ServiceProvider::class,
];
}

public function test_it_wraps_relations_using_resource()
{
$post = new Post(['text' => 'Lorem ipsum']);
$post->setRelation('dummy_tags', collect([
new Tag(['name' => 'foo']),
new Tag(['name' => 'bar']),
]));

$resource = new JsonResource($post);
$expected = [
'text' => 'Lorem ipsum',
'dummy_tags' => [
['name' => 'foo', 'tag' => true],
['name' => 'bar', 'tag' => true],
],
];

$this->assertEquals($expected, $resource->resolve());
}

/**
* @dataProvider otherResourcesProvider
*/
public function test_it_handles_other_resources($resource, $expected)
{
$this->assertEquals($expected, (new JsonResource($resource))->resolve());
}

public function otherResourcesProvider()
{
return [
[null, []],
[['foo' => 'baz'], ['foo' => 'baz']],
[
new class () implements Arrayable
{
public function toArray()
{
return ['foo' => 'baz'];
}
},
['foo' => 'baz']
],
];
}
}