定制 hanneskod/clean 二次开发

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

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

hanneskod/clean

最新稳定版本:3.0.0

Composer 安装命令:

composer require hanneskod/clean

包简介

A clean (as in simple) data cleaner (as in validation tool)

README 文档

README

Packagist Version Build Status Quality Score

A clean (as in simple) data cleaner (as in validation tool)

Why?

Sometimes it's necessary to perform complex input validation, and a number of tools exist for this purpose (think Respect\Validation). At other times (arguably most times) built in php functions such as the ctype-family and regular expressions are simply good enough. At these times pulling in a heavy validation library to perform basic tasks can be unnecessarily complex.

Clean acts as a thin wrapper around callables and native php functions, in less than 100 logical lines of code, and allows you to filter and validate user input through a simple and compact fluent interface.

Installation

composer require hanneskod/clean

Clean requires php 7.4 or later and has no userland dependencies.

Usage

Basic usage consists of grouping a set of Rules in an ArrayValidator.

use hanneskod\clean\ArrayValidator;
use hanneskod\clean\Rule;

$validator = new ArrayValidator([
    'foo' => (new Rule)->match('ctype_digit'),
    'bar' => (new Rule)->match('ctype_alpha'),
]);

$tainted = [
    'foo' => 'not-valid only digits allowed',
    'bar' => 'valid'
];

try {
    $validator->validate($tainted);
} catch (Exception $e) {
    echo $e->getMessage();
}

Defining rules

Rules are defined using the pre(), match() and post() methods.

  1. pre() takes any number of callable arguments to act as pre-match filters. Filters take an argument and return it in it's filtered state.
  2. post() takes any number of callable arguments to act as post-match filters. Filters take an argument and return it in it's filtered state.
  3. match() takes any number of callable arguments to act as validators. The callable should take an argument and return true if the argument is valid and false if it is not.

A rule definition might look like this:

use hanneskod\clean\Rule;

$rule = (new Rule)->pre('trim')->match('ctype_alpha')->post('strtoupper');

// outputs FOOBAR
echo $rule->validate('   foobar   ');

Using the regexp matcher

The Rule validator comes with one special case matcher: regexp() to match string input against a posix style regular expression (preg_match()).

use hanneskod\clean\Rule;

$rule = (new Rule)->regexp('/A/');

// outputs ABC
echo $rule->validate('ABC');

Making input optional

Rules may define a default value using the def() method. The default value is used as a replacement for null. This effectively makes the field optional in an ArrayValidator setting.

use hanneskod\clean\ArrayValidator;
use hanneskod\clean\Rule;

$validator = new ArrayValidator([
    'key' => (new Rule)->def('baz')
]);

$data = $validator->validate([]);

// outputs baz
echo $data['key'];

Specifying custom exception messages

When validation fails an exception is thrown with a generic message describing the error. Each rule may define a custom exception message using the msg() method to fine tune this behaviour.

use hanneskod\clean\Rule;

$rule = (new Rule)->msg('Expecting numerical input')->match('ctype_digit');

try {
    $rule->validate('foo');
} catch (Exception $e) {
    // outputs Expecting numerical input
    echo $e->getMessage();
}

Ignoring unknown input items

By default unkown intput items triggers exceptions.

use hanneskod\clean\ArrayValidator;

$validator = new ArrayValidator([]);

// throws a clean\Exception as key is not present in validator
$validator->validate(['this-key-is-not-definied' => '']);

Use ignoreUnknown() to switch this functionality off.

use hanneskod\clean\ArrayValidator;

$validator = (new ArrayValidator)->ignoreUnknown();

$clean = $validator->validate(['this-key-is-not-definied' => 'foobar']);

// outputs empty
echo empty($clean) ? 'empty' : 'not empty';

Nesting validators

ArrayValidators can be nested as follows:

use hanneskod\clean\ArrayValidator;
use hanneskod\clean\Rule;

$validator = new ArrayValidator([
    'nested' => new ArrayValidator([
        'foo' => new Rule
    ])
]);

$tainted = [
    'nested' => [
        'foo' => 'bar'
    ]
];

$clean = $validator->validate($tainted);

//outputs bar
echo $clean['nested']['foo'];

Inspecting validation results using applyTo()

The validate() method throws an exception as soon as a match fails. This may of course not always be what you want. You can inspect the validation result directly by using the applyTo() method instead.

use hanneskod\clean\Rule;

$rule = (new Rule)->match('ctype_digit');

$result = $rule->applyTo('12345');

$result->isValid() == true;

// outputs 12345
echo $result->getValidData();

Catching all of the failures

Individual errors can be accessed using the result object.

use hanneskod\clean\ArrayValidator;
use hanneskod\clean\Rule;

$validator = new ArrayValidator([
    '1' => (new Rule)->match('ctype_digit')->msg('failure 1'),
    '2' => (new Rule)->match('ctype_digit')->msg('failure 2'),
]);

// Both 1 and 2 will fail as they are not numerical
$result = $validator->applyTo(['1' => '', '2' => '']);

//outputs failure 1failure 2
foreach ($result->getErrors() as $errorMsg) {
    echo $errorMsg;
}

Identifying a failing rule

use hanneskod\clean\ArrayValidator;
use hanneskod\clean\Rule;

$validator = new ArrayValidator([
    'foo' => (new Rule)->match('ctype_digit'),
    'bar' => (new Rule)->match('ctype_digit'),
]);

$result = $validator->applyTo([
    'foo' => 'not-valid',
    'bar' => '12345'
]);

// outputs foo
echo implode(array_keys($result->getErrors()));

Implementing custom validators

use hanneskod\clean\AbstractValidator;
use hanneskod\clean\Rule;
use hanneskod\clean\ValidatorInterface;

class NumberRule extends AbstractValidator
{
    protected function create(): ValidatorInterface
    {
        return (new Rule)->match('ctype_digit');
    }
}

// Outputs 1234
echo (new NumberRule)->validate('1234');

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: WTFPL
  • 更新时间: 2015-12-31

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固