okipa / laravel-request-sanitizer
Easily sanitize your request inputs.
Installs: 6 603
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.1
- illuminate/support: ~5.5.0||~5.6.0||~5.7.0||~5.8.0||^6.0
- okipa/php-data-sanitizer: ^1.0
Requires (Dev)
- mockery/mockery: ^1.0
- nunomaduro/larastan: ^0.4
- orchestra/testbench: ~3.8.0||^4.0
- php-coveralls/php-coveralls: ^2.1
- phpmd/phpmd: ^2.0
- squizlabs/php_codesniffer: ^3.3
This package is auto-updated.
Last update: 2020-10-05 13:19:54 UTC
README
Sanitize your request inputs with the following features :
- entries sanitizing (https://github.com/Okipa/php-data-sanitizer).
- null entries exclusion.
- values safety check.
Compatibility
Laravel version | PHP version | Package version |
---|---|---|
^5.5 | ^7.1 | ^1.1 |
^5.0 | ^7.0 | ^1.0 |
Table of Contents
Installation
- Install the package with composer :
composer require "okipa/laravel-request-sanitizer:^1.1"
- Extends the
Okipa\LaravelRequestSanitizer\RequestSanitizer
in yourapp/Http/Requests/Request.php
class.
<?php use Okipa\LaravelRequestSanitizer\RequestSanitizer; class Request extends RequestSanitizer { // your laravel project base request custom features. }
Usage
<?php namespace App\Http\Requests; use Okipa\LaravelRequestSanitizer\RequestSanitizer; class EditUserRequest extends RequestSanitizer { protected $sanitizeEntries = true; // default value protected $exceptFromSanitize = ['user.phone_number']; // except the phone number from the sanitizing treatment in order to keep the phone number first zero (example : 0240506070) protected $excludeNullEntries = true; // default value protected $exceptFromNullExclusion = ['user.company_name']; // is kept in the request keys even if its value is null protected $safetyChecks = ['user.newsletter.subscription' => 'boolean', 'user.permissions' => 'array']; // will make sure that the declared keys will be returned with a default value if not found in the request /** * Execute some treatments just after the request creation */ public function before() { // execute your custom request treatments here $this->merge(['formatted_date' => Carbon::createFromFormat('d/m/Y H:i:s', $this->input('user.created_at')->toDateTimeString()]); } /** * Set the validation rules * * @return array */ public function rules() { return [ // other rules ... 'user.phone_number' => 'required|string', 'user.company_name' => 'nullable|string|max:255', 'user.newsletter.subscription' => 'required|boolean', 'user.permission' => 'required|array', 'formatted_date' => 'required|date|format:Y-m-d H:i:s' ]; } }
API
Properties
protected $sanitizeEntries = true
Recursively sanitize the request entries.
To check how data will be sanitized, check the used package : https://github.com/Okipa/php-data-sanitizer.
Declare this property to false to disable the request entries sanitizing.protected $exceptFromSanitize = []
Except the declared keys (dot notation accepted) from the request entries sanitizing.
It can be a good option when you have numbers beginning with a zero that you want to keep that way, for example.protected $excludeNullEntries = true
Recursively exclude all the null entries from the request.
Declare this property to false to disable the null entries exclusion.protected $exceptFromNullExclusion = []
Except the declared keys (dot notation accepted) from the null entries exclusion.
protected $safetyChecks = []
Set which request keys (dot notation accepted) should be safety checked, according to their types.
Use case :protected $safetyChecks = ['active' => 'boolean', 'permissions' => 'array'];
.
Accepted types values :boolean
/array
.
The keys declared in this array will take the following values (according to their declared types) if they are not found in the request :- boolean :
false
- array:
[]
- boolean :
Public methods
before()
This package gives you the opportunity to declare this method in your request.
It will be executed before all the request attributes treatments.
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.