makaveli/laravel-converter
Composer 安装命令:
composer require makaveli/laravel-converter
包简介
Advanced Converter utils for Laravel
关键字:
README 文档
README
🌍 Languages
- 🇺🇸 English (default)
- 🇷🇺 Русская версия
Table of Contents
Introduction
The makaveli/laravel-converter library provides utilities for data transformation in Laravel applications. This library allows you to transform array keys between different naming conventions (snake_case, camelCase, kebab-case, StudlyCase), proccess query params in requests and automatically convert data for response. It simplifies working with data, especially in APIs where consistent key naming is important.
Main goals for this library are:
- Automatically convert array keys and requests.
- Prepare sort and pagination in query params
- Integration with Laravel ^10+ and PHP 8.2+
- License: MIT.
- Author: Michael Udovenko.
Installation
-
Download library from Composer:
composer require makaveli/laravel-converter -
Publish configuration file (this will create
config/converter.phpfile in your project):php artisan vendor:publish --tag=converter-config -
Register provider in
config/app.phpfile (if it's not registered automatically)'providers' => [ // ... \Converter\Providers\ConverterServiceProvider::class, ],
-
(optional) Add middleware to convert your responses in
app/Http/Kernel.php:protected $middleware = [ // ... \Converter\Middleware\ConvertResponseTo::class, ];
Configuration
Configuration file config/converter.php includes the following settings:
'convert_from' => CaseConstants::CASE_CAMEL: Original keys style (for convert from).'convert_to' => CaseConstants::CASE_SNAKE: Target keys style (for convert to).'query_params': Settings for proccess query params.'descending_default_value' => 'asc': Default direction value for sort (ascending/descending).'sort_by_default_value' => 'created_at': Default field value for sort.'request_sort_by': Keys for sort in request ('name','descending_key','sort_by_key').'return_descending_name' => 'descending': Return param name for direction sort value.'return_sort_by_name' => 'sort_by': Return param name for sort value.
You can override these values for adapt them to your project.
Main components
The library is divided into modules (namespace Converter). Below are the main components.
Constants
- CaseConstants: Include case styles:
CASE_SNAKE('snake' — for snake_case).CASE_CAMEL('camel' — for camelCase).CASE_KEBAB('kebab' — for kebab-case).CASE_STUDLY('studly' — for StudlyCase).
DTO
- ConverterDTO: Base class for convert data.
__constructoris initCaseConverterclass.getQueryParams(Request $request, array $paramNames, ?array $sortByMapper = null, string $sortByKey = 'sort_by')extracts and converts query params (with sort mapping).getRequestData(array $data): converts array data to target case (from config).
Helper methods
-
ConverterHelpers: Advantage methods which use
CaseConverterclassconvertResult(mixed $resource)Converts the result to the desired format
-
QueryParams: Advantage methods for query params
defaultPaginationParamsKeys(): Returns default keys for pagination (showDeleted, rowsPerPage and etc.).getSortParams(array $params, array $sortTypes): Proccesses sort params with default values.convertArrStrToArrNumber(array $params): Conversts from an array of strings to array of numbers.mapSortBy(array $params, array $mapper, string $sortByKey = 'sort_by'): Mappings fields of sort.
Middleware
- ConvertResponseTo: Middleware for automatically converts JSON responses to other case from config (from-case). Can be used globally or for specific routes.
Providers
- ConverterServiceProvider: Registers and publishes config.
Core class
-
CaseConverter: Main converter.
convert(string $case, array $data): Recursive converts array keys to certain case. Supports nested arrays. ThrowInvalidArgumentExceptionclass if case is unknown.
Examples
Convert array
use Converter\CaseConverter; use Converter\Constants\CaseConstants; $converter = new CaseConverter(); $data = ['first_name' => 'John', 'last_name' => 'Doe']; $converted = $converter->convert(CaseConstants::CASE_CAMEL, $data); // Result: ['firstName' => 'John', 'lastName' => 'Doe']
Convert Eloquent Model or Collection (or other object that has toJson() method)
$user = User::findOrFail($id); $users = User::all(); return response()->json([ 'user' => ConverterHelpers::convertResult($user), 'users' => ConverterHelpers::convertResult($users) ]); // If you have CaseConstants::CASE_CAMEL in your config file all fields of user model and collection will be converted from snake_case to camelCase
Proccess query-params
use Converter\DTO\ConverterDTO; use Illuminate\Http\Request; $dto = new ConverterDTO(); $params = ConverterDTO::getQueryParams($request, ['sort_by', 'descending'], ['price' => 'price_asc']); // Converts and maps to snake_case (by config).
Middleware
After adding ConvertResponseTo middleware to Kernel.php all yours JSON-reponses will be automatically convert (for example from case_snake to camelCase).
Recommendations
- Use in APIs to maintain consistent key naming.
- Configure it for integration with other libraries (for example
makaveli/laravel-core). - For nested data converter works recursive.
- If you need custom case add it to
CaseConstantsand refresh validation.
This library is easy and lightweight so it's perfect for utilitarian tasks. If you need extra information visit GitHub page for this project
makaveli/laravel-converter 适用场景与选型建议
makaveli/laravel-converter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 16 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 makaveli/laravel-converter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 makaveli/laravel-converter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 makaveli/laravel-converter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Alfabank REST API integration
High-performance, opinionated PHP 8 web framework with O(1) routing, parallel async MySQL, type-safe asset bridge, and React-island frontend
Laravel package for Accurate Online API integration.
The skeleton application for the Geoffrey agent framework.
Shared RCX Laravel DataTables UI and configuration helpers.
统计信息
- 总下载量: 16
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-27