hublint / valfactory
简单的验证库
0.0.1
2018-12-16 10:48 UTC
Requires
- php: >=5.4.0
This package is auto-updated.
Last update: 2024-09-19 16:28:35 UTC
README
简单的PHP验证库。
- 轻量级。
- 无依赖。
- 支持对单个实体进行多验证,代码更少。
- 错误存储在一个数组中,可以解析为JSON或其他适合不同用例的格式。
简单易用,只需三步即可验证输入/数据。
- 收集数据。
- 运行验证。
- 检查错误。
ValFactory不关心你如何收集数据,它只确保数据符合你的预期,在你决定如何处理它们之前。
ValFactory不加密、授权或认证,它只是一个只进行验证的工厂。
享受验证过程!
示例
<?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. */
可用方法
<?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); */
安装
最低要求是PHP 5.4.x。虽然ValFactory可能在更低的PHP版本上工作,但目前既未测试也不推荐。
通过composer.json
{ "require": { "hublint/valfactory": "*" } }
然后
composer install
或者
composer update -o
通过命令行
composer require hublint/valfactory