-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to convert resource keys to camel case.
- Loading branch information
Showing
9 changed files
with
106 additions
and
6 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace ToneflixCode\ResourceModifier\Services\Json; | ||
|
||
use Illuminate\Container\Container; | ||
use Illuminate\Contracts\Support\Arrayable; | ||
use Illuminate\Http\Resources\Json\JsonResource as IlluminateJsonResource; | ||
use JsonSerializable; | ||
|
||
class JsonResource extends IlluminateJsonResource | ||
{ | ||
/** | ||
* Resolve the resource to an array. | ||
* | ||
* @param \Illuminate\Http\Request|null $request | ||
* @return array | ||
*/ | ||
public function resolve($request = null) | ||
{ | ||
$data = $this->toArray( | ||
$request ?: Container::getInstance()->make('request') | ||
); | ||
|
||
if ($data instanceof Arrayable) { | ||
$data = $data->toArray(); | ||
} elseif ($data instanceof JsonSerializable) { | ||
$data = $data->jsonSerialize(); | ||
} | ||
|
||
if (config('resource-modifier.prefer_camel_casing', false) === true) { | ||
return collect($this->filter((array) $data)) | ||
->mapWithKeys(fn($value, $key) => [str($key)->camel()->toString() => $value]) | ||
->toArray(); | ||
} | ||
|
||
return $this->filter((array) $data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
use Illuminate\Support\Facades\Route; | ||
use ToneflixCode\ResourceModifier\Tests\App\Http\Resources\UserResource; | ||
use ToneflixCode\ResourceModifier\Tests\Models\User; | ||
|
||
test('can convert resource to camel case', function () { | ||
|
||
$user = User::factory()->create(); | ||
|
||
Route::get('test/users', function () use ($user) { | ||
return present(fn() => new UserResource($user)); | ||
}); | ||
|
||
$response = $this->get('/test/users'); | ||
|
||
! json_validate($response->content()) && $response->dump(); | ||
|
||
$response->assertCreated(); | ||
expect($response->collect('data')->keys()[3])->toBeCamelCase(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters