Skip to content

Instantly share code, notes, and snippets.

@Buccaneersdan
Last active August 8, 2022 22:49
Show Gist options
  • Save Buccaneersdan/a47d582923e933dae5cc4deb6938a1fb to your computer and use it in GitHub Desktop.
Save Buccaneersdan/a47d582923e933dae5cc4deb6938a1fb to your computer and use it in GitHub Desktop.
A middleware that transforms columns in Laravel-JsonResponses from snake-case to camel-case recursively
This is a Laravel-Middleware that transforms the keys of a JSON-Response coming directly from an Eloquent-Model from snake-case (e.g. user_id) to camel-case (e.g. userId).
The files should be located/created in this locations:
/app/Http/Middleware/CamelCaseJson.php
/routes/api.php
/app/Http/Kernel.php
<?php
use App\Http\Controllers\MyController;
use Illuminate\Support\Facades\Route;
Route::middleware([
'auth:sanctum'
])
->group(function () {
Route::group(['prefix' => 'myGroupName'], function () {
Route::get(
'/api-endpoint-name',
[MyController::class, 'myControllerFunction']
)
->middleware('response.json.camelCaseColumns');
});
});
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\JsonResponse;
use stdClass;
use Illuminate\Support\Str;
class CamelCaseJson
{
public static function convertObjectPropertiesToCamelCase($dataItem)
{
$newObject = new stdClass();
foreach ($dataItem as $columnKey => $columnValue) {
$camelCaseKey = Str::camel($columnKey);
if (is_object($columnValue)) {
$newObject->{$camelCaseKey} = CamelCaseJson::convertObjectPropertiesToCamelCase($columnValue);
} else if (is_array($columnValue)) {
$newObject->{$camelCaseKey} = CamelCaseJson::convertArrayItemsToCamelCase($columnValue);
} else {
$newObject->{$camelCaseKey} = $columnValue;
}
}
return $newObject;
}
public static function convertArrayItemsToCamelCase($dataItem)
{
$newArray = [];
foreach ($dataItem as $columnKey => $columnValue) {
$camelCaseKey = Str::camel($columnKey);
if (is_object($columnValue)) {
$newArray[$camelCaseKey] = CamelCaseJson::convertObjectPropertiesToCamelCase($columnValue);
} else if (is_array($columnValue)) {
$newArray[$camelCaseKey] = CamelCaseJson::convertArrayItemsToCamelCase($columnValue);
} else {
$newArray[$camelCaseKey] = $columnValue;
}
}
return $newArray;
}
public function handle($request, Closure $next)
{
$response = $next($request);
if ($response instanceof JsonResponse) {
$data = $response->getData();
foreach ($data as $indexKey => $dataItem) {
$newKey = Str::camel($indexKey);
if (is_object($dataItem) || is_array($dataItem)) {
if (is_array($dataItem)) {
$data->{$newKey} = CamelCaseJson::convertArrayItemsToCamelCase($dataItem);
} else if (is_object($dataItem)) {
$data->{$newKey} = CamelCaseJson::convertObjectPropertiesToCamelCase($dataItem);
}
} else {
$data->{$newKey} = $dataItem;
}
}
$response->setData($data);
}
return $response;
}
}
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
// ...
protected $routeMiddleware = [
// ...
'response.json.camelCaseColumns' => \App\Http\Middleware\CamelCaseJson::class
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment