定制 hublint/valfactory 二次开发

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

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

hublint/valfactory

Composer 安装命令:

composer require hublint/valfactory

包简介

Simple validation library

README 文档

README

Simple PHP vaidation library.

  • Lightweight.
  • No dependencies.
  • Suppports multiple validations on a single entity with less code.
  • Errors are stored in a single array which can be parsed as JSON or other suitable format for different use cases.

It's simple, and lets you validate inputs/data in three steps.

  1. Collect Data.
  2. Run Validations.
  3. Check for Errors.

ValFactory does NOT care how you collect your data, it only makes sure the data are what you're expecting, before you decide what to do with them.

ValFactory does NOT encrypt, authorize, or authenticate, it's a factory that only validates.

Happy Validating!

Example

<?php
/**
 * @var Composer\Autoload\ClassLoader $autoload
*/
$autoload = require 'vendor/autoload.php';


// Basic Example

$val = new \Hublint\ValFactory\Validator();

// Test Data
$num = 1233455;
$alphanum = "Hello 123";
$text = 'Hello Dolly';
$email = 'you@mail.com';
$password = 'secret';
$passwordConfirm = 'secret';

// This may be also be an array of emails from the database.
$existingEmails = ['akeemovic@slackwave.net', 'halayindex@slackwave.net'];

/**
 * Format
 * $val->validate('key1', $data1)->valMethod1();
 * $val->validate('key2', $data2)->valMethod2()->valMethod3();
*/

// Run Validations
$val->validate('alphanum', $alphanum)->notEmpty()->alphaNum()->noWhiteSpace()->limitMin(10);
$val->validate('text', $text)->notEmpty()->alpha()->noWhiteSpace()->limitMax(10);
$val->validate('number', $num)->notEmpty()->numeric()->limit(5, 10);
$val->validate('email', $email)->email()->unique($existingEmails, "We won't take that Email");
$val->validate('password', $password)->notEmpty()->noWhiteSpace();
$val->validate('password_confirmation', $passwordConfirm)->sameAs($password);

// Check for errors
if ($val->passed()) {
	echo "Hurray!";
} else {
	foreach($val->errors as $error){
		echo $error . '<br>';
	}
}

// The following produces the same results as the above
if ($val->failed()) {
	foreach($val->errors as $error){
		echo $error . '<br>';
	}
} else {
	echo "Hurray!";
}

/*
 * If validation(s) passed successsfully, $val->failed() returns FALSE, $val->errors returns [] - an empty array.
 * If validation(s) passed successsfully, $val->passed() rerurns TRUE, $val->errors returns [] - an empty array.
 *
 * But,
 *
 * If the validations failed, $val->failed() returns TRUE, $val->errors returns an array of errors.
 * If the validations failed, $val->passed() rerurns FALSE, $val->errors returns an array of errors.
*/

Available Methods

<?php
/**
 * Available Methods
 * $customErrorMessage is for setting custom errros, and is optional. 
 * So, only set it when you don't want the default errors. 
*/

// Validation not empty
notEmpty($customErrorMessage);

// Alphabets Only
alpha($customErrorMessage);

// Numeric values only
numeric($customErrorMessage);

// Alphanumeric characters only
alphaNum($customErrorMessage);

// Must not contain white space
noWhiteSpace($customErrorMessage);

// Validate email
email($customErrorMessage);

// Checks for uniqueness of data in correspondence to $haystack
unique($haystack, $customErrorMessage);

// Validate with your own RegExp pattern
matchPattern($customPattern, $customErrorMessage);

// Validate data is same as $matchingValue
sameAs($matchingValue, $customErrorMessage);

// Set minimmum and maximmum limits
limit($minCount, $maxCount, $customErrorMessage);

// Set minimmum limit
limitMin($minCount, $customErrorMessage);

// Set maximmum limit
limitMax($maxCount, $customErrorMessage);


/** 
 * NOTE: Validation methods must be chained to one validate('key', $data) root method.
 * Example;
 * $val->validate('key', $data)->limit(5, 15);
 * $val->validate('key', $data)->alpha('Characters must be only alphabets Aa-Zz')->noWhiteSpace(5, 15);
*/

installation

Minimmum requirement is PHP 5.4.x. Although, ValFactory might work on lower versions of PHP, they are currently neither tested nor recommended.

via composer.json:

{
    "require": {
        "hublint/valfactory": "*"
    }
}

and then

composer install

or

composer update -o

via command line

composer require hublint/valfactory

hublint/valfactory 适用场景与选型建议

hublint/valfactory 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 59 次下载、GitHub Stars 达 2, 最近一次更新时间为 2018 年 10 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「validator」 「validation」 「form validation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 hublint/valfactory 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hublint/valfactory 我们能提供哪些服务?
定制开发 / 二次开发

基于 hublint/valfactory 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-10-29