定制 creativitykills/sanity 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

creativitykills/sanity

最新稳定版本:1.0.0

Composer 安装命令:

composer require creativitykills/sanity

包简介

Array Sanitizers Package.

README 文档

README

Build Status

This package is a Sanitizer for PHP arrays. It's best used to sanitize request inputs like input from $_POST and/or $_GET.

Usage

Step 1: Install via Composer

composer require 'creativitykills/sanity'

Step 2: Add the Service Provider

Open config/app.php and, to your "providers" array at the bottom, add:

"CreativityKills\Sanity\SanityServiceProvider"

Sanitize Requests

Out of the box you can start running requests through the sanitizer.

<?php

// Possibly input from $_POST or $_GET global array
$someArray = ['name' => ' JOHN DOE ', 'email' => ' JOHN@DOE.COM '];

// Rules to validate against
$sanitizerRules = ['name' => 'ucwords|trim', 'email' => 'strtolower|trim'];

$sanitizer = new \CreativityKills\Sanity\Sanitizer;

$someArray = $sanitizer->sanitize($someArray, $sanitizerRules);

// array(
//    'name'  => 'John Doe',
//    'email' => 'john@doe.com'
// )
var_dump($someArray);

Custom Sanitizers

The sweet aspect of Sanity is extending the Sanity class. You can create a custom extension of the class.

<?php

use CreativityKills\Sanity\Sanitizer;

class UserSanitizer extends Sanitizer {
    
    protected $rules = [
        'name'  => 'ucwords|trim|remove_excess_white_spaces',
        'email' => 'strtolower|trim'
    ];
    
    public function sanitizeRemoveExcessWhiteSpaces($value)
    {
        return preg_replace('/\s+/', ' ', $value)
    }
}

Notice the custom remove_excess_white_spaces sanitizer is called from the method sanitizeRemoveExcessWhiteSpaces. All snake cased sanitizer rules are converted to camel cases.

Now we can call our custom sanitizer from our application like so:

<?php

// Possibly input from $_POST or $_GET global array
$someArray = ['name' => ' JOHN   DOE ', 'email' => ' JOHN@DOE.COM '];
                
$someArray = (new UserSanitizer)->sanitize($someArray);

// array(
//    'name'  => 'John Doe',
//    'email' => 'john@doe.com'
// )
var_dump($someArray);

统计信息

  • 总下载量: 12
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-03-10