定制 estasi/validator 二次开发

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

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

estasi/validator

Composer 安装命令:

composer require estasi/validator

包简介

Validation classes for data validation, and the ability to link validators to create complex validation chains

README 文档

README

It is a set of necessary immutable validators. Provides a simple data validation chain mechanism that allows you to apply multiple validators in a given order.

Installation

To install with a composer:

composer require estasi/validator

Requirements

  • PHP 7.4 or newer
  • ext-mbstring
  • ext-intl
  • Data Structures: composer require php-ds/php-ds
    Polyfill is installed with the estasi/validator package.

Usage

Basic usage

<?php

declare(strict_types=1);

use Estasi\Validator\Email;

$email = 'john@doe.com';
$validator = new Email(Email::ALLOW_UNICODE);
if ($validator->isValid($email)) {
    // your code is here
} else {
    // print "Email "$email" is not correct!"
    echo $validator->getLastErrorMessage();
}

Custom message

<?php

declare(strict_types=1);

use Estasi\Validator\Email;

$email = 'john@doe.com';
$validator = new Email(Email::ALLOW_UNICODE, [Email::E_INVALID_EMAIL => 'Custom error message.']);
if ($validator->isValid($email)) {
    // your code is here
} else {
    // print "Custom error message."
    echo $validator->getLastErrorMessage();
}

or

<?php

declare(strict_types=1);

use Estasi\Validator\Email;

$email = 'john@doe.com';
$validator = new Email(Email::ALLOW_UNICODE);
if ($validator->isValid($email)) {
    // your code is here
} else {
    if ($validator->isLastError(Email::E_INVALID_EMAIL)) {
        echo "Custom error message.";
        // or your other code depending on the error
    }
    //...
}

Identical

Only the Identical validator can accept the second parameter in the isValid(), notValid() and __invoke() methods

Simple comparison

<?php

declare(strict_types=1);

use Estasi\Validator\Identical;

$token = 'string';
$value = 'string';

$validator = new Identical($token, Identical::STRICT_IDENTITY_VERIFICATION);
if ($validator->isValid($value)) {
    // your code is here
}

or

<?php

declare(strict_types=1);

use Estasi\Validator\Identical;

$context = 'string';
$value = 'string';

$validator = new Identical(null, Identical::STRICT_IDENTITY_VERIFICATION);
if ($validator->isValid($value, $context)) {
    // your code is here
}

Comparison with the value in the array

<?php

declare(strict_types=1);

use Estasi\Validator\Identical;

$token = 'email';
$value = 'john@doe.com';
$context = ['names' => ['firstname' => 'John', 'lastname' => 'Doe'], 'email' => 'john@doe.com'];

$validator = new Identical($token, Identical::STRICT_IDENTITY_VERIFICATION);
if ($validator->isValid($value, $context)) {
    // your code is here
}

You can also check the value of an unrestricted nested context if the context is an array. The nesting separator will thus be the symbol ".".

<?php

declare(strict_types=1);

use Estasi\Validator\Identical;

$token = 'names.lastname';
$value = 'Doe';
$context = ['names' => ['firstname' => 'John', 'lastname' => 'Doe'], 'email' => 'john@doe.com'];

$validator = new Identical($token, Identical::STRICT_IDENTITY_VERIFICATION);
if ($validator->isValid($value, $context)) {
    // your code is here
}

Chain

Here are two validator tasks in the chain: explicitly (by class declaration) and via the factory (array)

<?php

declare(strict_types=1);

use Estasi\Validator\{Chain,Identical,Regex};

$datum = [
    'password' => [
        'original' => 'password_25',
        'confirm'  => 'password_25'
    ]
];

$chain = new Chain();
$chain = $chain->attach(
                   new Regex('[A-Za-z0-9_]{8,12}', Regex::OFFSET_ZERO, [Regex::OPT_ERROR_VALUE_OBSCURED => true]),
                   Chain::WITH_BREAK_ON_FAILURE
               )
               ->attach(
                   [
                       Chain::VALIDATOR_NAME => 'identical',
                       Chain::VALIDATOR_OPTIONS => [
                           Identical::OPT_TOKEN => 'password.original',
                           Identical::OPT_ERROR_VALUE_OBSCURED => true
                       ]
                   ],
                   Chain::WITH_BREAK_ON_FAILURE
               );
if($chain->isValid($datum['password']['original'], $datum)) {
    // your code is here
}

License

All contents of this package are licensed under the BSD-3-Clause license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2020-05-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固