truongwp/sanitizer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

truongwp/sanitizer

Composer 安装命令:

composer require truongwp/sanitizer

包简介

Simple and stand-alone sanitizer library with no dependencies.

README 文档

README

Sanitizer is a simple and stand-alone PHP sanitizer library with no dependencies.

Installation

Uses Composer to install and update:

composer require "truongwp/sanitizer=*"

Sanitizer require PHP >= 5.3

Usage

<?php
$sanitizer = new Truongwp\Sanitizer\Sanitizer();

$input = array(
    'name'     => '  Foo bar ',
    'age'      => ' 24 ',
);

$rules = array(
    'name'     => 'trim|strtolower|ucwords',
    'age'      => 'intval',
);

$output = $sanitizer->sanitize($input, $rules);

The $output:

array(
    'name'     => 'Foo Bar',
    'age'      => 24,
);

Multiple rules can be passed as string delimiter by | or use an array:

<?php
$rules = array(
    'name'     => array('trim', 'strtolower', 'ucwords'),
    'age'      => 'intval',
);

By default, rule name is PHP function. So you can easily add a custom function to sanitize.

<?php
function trim_slasses($value) {
    return trim($value, '/');
}

$sanitizer = new Truongwp\Sanitizer\Sanitizer();

$input = array(
    'name' => '//foo',
);
$rules = array(
    'name' => 'trim_slasses',
);
$output = $sanitizer->sanitize($input, $rules);

The result:

array(
    'name' => 'foo',
)

If you want to pass additional parameters to sanitizer function, you can append them to rule name and delimited by :.

<?php
function prefix_suffix($value, $prefix = '', $suffix = '') {
    return $prefix . $value . $suffix;
}

$sanitizer = new Truongwp\Sanitizer\Sanitizer();

$input = array(
    'name' => 'foo',
);
$rules = array(
    'name' => 'prefix_suffix:prefix_:_suffix',
);
$output = $sanitizer->sanitize($input, $rules);

The result:

array(
    'name' => 'prefix_foo_suffix',
)

You can also add custom sanitizer class by using SanitizerRegistry.

<?php
class DateFormatSanitizer implements Truongwp\Sanitizer\Contracts\RuleSanitizer
{
    /**
     * Sanitize value.
     *
     * @param mixed $value Value need to sanitize.
     * @return mixed
     */
    public function sanitize($value)
    {
        $args = func_get_args();
        $format = empty($args[1]) ? 'Y-m-d' : $args[1];

        $timestamp = strtotime($value);
        return date($format, $timestamp);
    }
}

// Register rule sanitizers.
Truongwp\Sanitizer\Registries\SanitizerRegistry::set('date_format', new DateFormatSanitizer());

$sanitizer = new Truongwp\Sanitizer\Sanitizer();

$input = array(
    'day' => '05/30/2017',
);
$rules = array(
    'name' => 'date_format:Y-m-d',
);
$output = $sanitizer->sanitize($input, $rules);

The result:

array(
    'day' => '2017-05-30',
)

Contributing

Contributor: @truongwp

Bug reports or Pull requests are welcome.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-05-30

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固