定制 einlinuus/php-validator 二次开发

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

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

einlinuus/php-validator

最新稳定版本:1.0.0

Composer 安装命令:

composer require einlinuus/php-validator

包简介

Minimal library to validate and transform user input

README 文档

README

Capabilities

  • Check for datatype
  • Check for length / amount
  • Check for regex
  • Check for min / max entries in array
  • Check for unique entries in array
  • Clean strings (remove double spaces, trim, etc.)
  • Check for valid email format
  • Check for valid URL format
  • Check for specific array shape
  • Custom validation functions
  • Custom transformation functions

Use cases

  • Validate user input
  • Validate API input
  • Transform input (e.g. convert IDs to objects)

Installation

This package can be installed via composer:

composer require einlinuus/php-validator

New to composer?

Composer is a dependency manager for PHP. Composer can install this package from the packagist.org registry. Once installed, you'll find a vendor directory in your project.

You can import all required files by importing the autoload.php file created by composer: require_once __DIR__ . "/vendor/autoload.php"; With the autoload-file included in your project, you now have access to all classes and functions provided by this package.

Usage

Simply create a new instance of the Validator class and pass the input data to the constructor.

Next, chain the validation methods inside a try-catch block. If any of the validation methods fail, a ValidatorException will be thrown.

After the validation methods, you can get the output data by calling the get() method. The output data is your input data transformed by the transformation methods. If no transformation methods are used, the output data will be the same as the input data.

$input = "hello world";

$v = new Validator($input);
try {
    $v->isString("Input must be a string")
        ->isLowercase("Input must be lowercase")
        ->min(3, "Input must be at least 3 characters long")
        ->max(12, "Input must be at most 10 characters long");
} catch (ValidatorException $e) {
    die("Invalid: " . $e->getMessage());
}

$validated = $v->get();
var_dump($validated); // string(11) "hello world"

You can find more examples in the examples folder.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0
  • 更新时间: 2024-02-24