Last active
August 8, 2022 22:49
-
-
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 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 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 |
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
<?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'); | |
}); | |
}); |
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
<?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; | |
} | |
} |
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
<?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