kjos / kjos-laravel-parameter-mapper
ParameterMapper tool for Laravel and other
Installs: 12
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/kjos/kjos-laravel-parameter-mapper
Requires
- php: ^7.0|^8.0
- illuminate/console: ^8.0|^10.0|^11.0|^12.0
- illuminate/database: ^8.0|^10.0|^11.0|^12.0
- illuminate/routing: ^8.0|^10.0|^11.0|^12.0
- illuminate/support: ^8.0|^10.0|^11.0|^12.0
- laravel/pint: ^1.20
Requires (Dev)
- fakerphp/faker: ^1.23
- mockery/mockery: ^1.6
- nunomaduro/collision: ^5.0
- orchestra/testbench: ^6.47
- pestphp/pest: ^1.23
- pestphp/pest-plugin: ^1.1
- pestphp/pest-plugin-laravel: ^1.4
- phpro/grumphp: ^2.5
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^9.5
README
π Multilanguage | Multilingual
- π«π· French Version
- π¬π§ English Version
π¬π§ English
Kjos Laravel Parameter Mapper
A Laravel package that allows you to dynamically map GET and POST parameters sent by the client to your application's backend keys, and vice versa. Useful for hiding the actual field names from the user or for standardizing your APIs.
Installation
For Laravel 12 or higher:
composer require kjos/kjos-laravel-parameter-mapper
Publish the configuration
php artisan vendor:publish --tag=parametermap
Configuration
return [ 'map' => [ // frontParam => backendParam 'id_ur' => 'user_id', 'name_lt' => 'last_name', 'ae' => 'age', ], // Valeurs spΓ©cifiques Γ mapper (ex: search=id_us => search=user_id) 'values-to-map' => [ 'search', ], // Mapper les clΓ©s dans des tableaux (ex: sort[id_us] => sort[user_id]) 'array-keys-to-map' => [ 'sort', ], 'reject-knowns' => true, ];
##Middleware
use Kjos\ParameterMapper\Middleware\MapRequestParameters; Route::middleware([MapRequestParameters::class])
Class ParameterMapper
use Kjos\ParameterMapper\Support\ParameterMapper; // Map front -> back $mapped = ParameterMapper::apply([ 'id_ur' => 1, 'name_lt' => 'Koffi', 'ae' => 10, 'sort' => ['id_ur' => 'asc'], 'search' => 'id_ur', ]); // Map back -> front $frontend = ParameterMapper::reverse([ 'user_id' => 1, 'last_name' => 'Koffi', 'age' => 10, 'sort' => ['user_id' => 'asc'], 'search' => 'user_id', ]);
Use in Factories
$datas = ParameterMapper::reverse([ 'user_id' => 1, 'last_name' => 'Koffi', 'age' => 10, ]);
Becomes:
[ 'id_ur' => 1, 'name_lt' => 'Koffi', 'ae' => 10, ]
API Example
Request:
GET /api/admins?search=id_us&sort[id_us]=asc
Automatically transformed into:
[ 'search' => 'user_id', 'sort' => ['user_id' => 'asc'] ];
π reject-knowns β Reject internal parameters
The reject-knowns parameter allows you to automatically reject any request that contains a parameter corresponding to an internal backend key.
Purpose
To prevent users from directly sending backend (internal) field names that are normally hidden behind the front-end map.
For example, if you have this map:
'map' => [ 'id_ur' => 'user_id', 'name_lt' => 'last_name', 'ae' => 'age', ],
and reject-knowns is enabled:
'reject-knowns' => true,
Then a request like:
GET /api/admins?user_id=1
will be rejected with an HTTP 404 error, because user_id is an internal parameter and should not be exposed on the front end.
Comment to enable it
In the configuration file config/parameter-mapper.php
<?php return [ 'map' => [ 'id_ur' => 'user_id', 'name_lt' => 'last_name', 'ae' => 'age', ], // Reject known backend parameters 'reject-knowns' => true, ];
Map the response
In the configuration file config/parameter-mapper.php
<?php return [ 'map' => [ ... ], // Will map the response 'map-response' => true, ];
Schema
π Directory Structure
project-root/
βββ CHANGELOG.md
βββ LICENSE
βββ README.md
βββ composer.json
βββ composer.lock
βββ config
β βββ parameter-mapper.php
βββ grumphp.yml
βββ phpunit.xml
βββ pint.json
βββ schema.png
βββ src
β βββ Middleware
β βββ ParameterMapperServiceProvider.php
β βββ Support
βββ tests
β βββ ExampleTest.php
β βββ Feature
β βββ Pest.php
β βββ TestCase.php
β βββ Unit
π€ Author
Maintained by Jean Koffi
π License
MIT Β© kjos/kjos-laravel-parameter-mapper
π€ Call for contributions
This project is open to contributions! Are you a developer, passionate about Laravel, or interested in multi-tenant architecture?
-Fork the project
-
Create a branch (klpm/my-feature)
-
Make a PR π§ͺ
