phly/phly-rule-validation 问题修复 & 功能扩展

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

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

phly/phly-rule-validation

最新稳定版本:0.2.1

Composer 安装命令:

composer require phly/phly-rule-validation

包简介

Simple rule-based validation framework

关键字:

README 文档

README

Build Status

This library provides a barebones validation framework.

Goals of this library

The explicit goals of this library are:

  • Provide an idempotent way to validate individual items and/or data sets.
  • Provide an extensible framework for developing validation rules.
  • Allow handling optional data, with default values.
  • Allow reporting validation error messages.
  • Ensure missing required values are reported as validation failures.
  • Use as few dependencies as possible.

Non-goals:

  • Creating an extensive set of validation rule classes.
  • Providing extensive mechanisms for validating and returning nested data sets.
  • Providing a configuration-driven mechanism for creating rule sets.
  • Providing HTML form input representations or all metadata required to create HTML form input representations.

Installation

composer require phly/phly-rule-validation

Basic Usage

use Phly\RuleValidation\Result\Result;
use Phly\RuleValidation\RuleSet\RuleSet;
use Phly\RuleValidation\Rule\CallbackRule;

$rules = new RuleSet();
$rules->add(new CallbackRule('flag', function (mixed $value, array $data): Result {
    if (! is_bool($value)) {
        return Result::forInvalidValue('flag', $value, 'Not a boolean value');
    }
    return Result::forValidValue('flag', $value);
}, default: false));
$rules->add(new MyCustomRule());
// and so on

$resultSet = $rules->validate($someFormData);

if ($resultSet->isValid()) {
    $values = $resultSet->getValues();
    // do something with values
} else {
    $messages = $resultSet->getMessages();
    // do something with error messages
}

// Get a result for a single key:
$flagResult = $resultSet->flag; // or $resultSet->getResult('flag')

// Get the value from a single result
$flag = $flagResult->value();

// Get the validation status from a single result
if ($flagResult->isValid()) {
    // ...
}

// Get an error message for a single result
if (! $flagResult->isValid()) {
    echo $flagResult->message();
}

Documentation

Please see the documentation tree (docs/).

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2023-11-09