okipa/laravel-request-sanitizer

该软件包已被废弃且不再维护。未建议替代软件包。

轻松清理您的请求输入。

1.1.2 2019-10-09 14:45 UTC

This package is auto-updated.

Last update: 2020-10-05 13:19:54 UTC


README

Source Code Latest Version Total Downloads License: MIT Build Status Coverage Status Quality Score

以下特性可以帮助您清理请求输入

兼容性

Laravel版本 PHP版本 软件包版本
^5.5 ^7.1 ^1.1
^5.0 ^7.0 ^1.0

目录

安装

  • 使用composer安装该软件包
composer require "okipa/laravel-request-sanitizer:^1.1"
  • 在您的 app/Http/Requests/Request.php 类中扩展 Okipa\LaravelRequestSanitizer\RequestSanitizer
<?php

use Okipa\LaravelRequestSanitizer\RequestSanitizer;

class Request extends RequestSanitizer
{
    // your laravel project base request custom features.
}

使用

<?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

属性

  • protected $sanitizeEntries = true

    递归清理请求条目。
    要检查数据将被如何清理,请查看所使用的软件包:https://github.com/Okipa/php-data-sanitizer
    将此属性声明为false以禁用请求条目清理。

  • protected $exceptFromSanitize = []

    从请求条目清理中排除声明的键(接受点符号表示法)。
    例如,如果您想保留以零开头的数字,这是一个不错的选择。

  • protected $excludeNullEntries = true

    递归地从请求中排除所有空条目。
    将此属性声明为false以禁用空条目排除。

  • protected $exceptFromNullExclusion = []

    从空条目排除中排除声明的键(接受点符号表示法)。

  • protected $safetyChecks = []

    设置哪些请求键(接受点符号表示法)应该根据其类型进行安全检查。
    用例:protected $safetyChecks = ['active' => 'boolean', 'permissions' => 'array'];
    接受类型值:boolean / array
    在此数组中声明的键将根据其声明的类型采用以下值(如果它们在请求中找不到)

    • 布尔值:false
    • 数组:[]

公共方法

  • before()

    此软件包为您提供了在请求中声明此方法的机会。
    它将在所有请求属性处理之前执行。

测试

composer test

变更日志

请参阅变更日志以获取更多关于最近更改的信息。

贡献

请参阅贡献指南以获取详细信息。

致谢

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。