定制 celiovmjr/smartvalidator 二次开发

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

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

celiovmjr/smartvalidator

最新稳定版本:2.0.5

Composer 安装命令:

composer require celiovmjr/smartvalidator

包简介

Validate. Sanitize. Secure Your Data.

README 文档

README

The SmartValidator is a PHP class designed to validate and filter input data based on configurable rules. It supports a variety of simple and complex validations, ensuring that data meets specified criteria before being considered valid.

Installation

To use the SmartValidator, include the SmartValidator.php class file in your PHP project and instantiate the class as needed.

require_once 'SmartValidator.php';

// Basic usage example
$data = [
    'name' => 'John Doe',
    'email' => 'john@example.com',
    'age' => 30,
];

$rules = [
    'name' => 'string|required',
    'email' => 'email|required',
    'age' => 'int|required|min:18',
];

$validator = new Validator\Application\SmartValidator($data, $rules);
$validatedData = $validator->getValidated();

// $validatedData will contain the validated data according to the specified rules

Features

Constructor

public function __construct(array|object $data, array $rules)
  • $data: Associative array containing the data to be validated.
  • $rules: Associative array where keys represent property names to validate, and values are strings containing validation rules separated by pipe (|).

Supported Validation Methods

The SmartValidator supports the following types of validation:

  • Simple Types:

    • string: Validates if the value is a string and applies sanitization.
    • bool: Validates and converts the value to boolean.
    • int: Validates and converts the value to integer.
    • float: Validates and converts the value to float.
    • email: Validates if the value is a valid email address.
    • url: Validates if the value is a valid URL.
    • ip: Validates if the value is a valid IP address.
    • required: Validates if the value is not null or empty.
    • uuid: Validates if the value is a valid UUID.
  • Complex Validations:

    • min:x: Validates if the numeric value is greater than or equal to x, or if the string has at least x characters.
    • max:x: Validates if the numeric value is less than or equal to x, or if the string has at most x characters.
    • range:min,max: Validates if the value is within the specified range.
    • format:format: Validates if the value matches the specified date format.
    • before:date,format: Validates if the value is a date before the specified date.
    • after:date,format: Validates if the value is a date after the specified date.
    • in:val1,val2,...: Validates if the value is among the allowed values.
    • size:size: Validates if the size of the value is equal to size.
    • mime:type1,type2,...: Validates if the MIME type of the value is among the allowed types.

Exceptions

The SmartValidator throws InvalidArgumentException when a validation fails, with detailed error messages explaining the reason for the validation failure.

Return

  • getValidated(): Returns an associative array containing successfully validated data according to the specified rules.
public function getValidated(): array

Final Notes

The SmartValidator offers a flexible solution for data validation in PHP, providing a robust mechanism to ensure input data meets defined requirements before processing or storing. It facilitates the implementation of complex validations in a clear and structured manner, promoting data security and integrity in PHP applications.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固