harp-orm / validate
对象验证库
This package is auto-updated.
Last update: 2024-09-21 20:33:26 UTC
README
Harp Validate 是一个验证库。它根据预定义的断言生成对象的错误。
快速示例
use Harp\Validate\Assert; use Harp\Validate\Asserts; $asserts = new Asserts(array( new Assert\Present('title'), new Assert\LengthBetween('title', 20, 100), new Assert\Email('newsletter_email'), )); $subject = new stdClass(); $subject->title = 'small title'; $subject->newsletter_email = 'invalid email'; // title should be between 10 and 20 letters, newsletter_email should be a valid email echo $asserts->getErrors($subject);
错误
$asserts->getErrors($subject)
的结果是实际的 Errors
对象。它是一个迭代器,包含所有错误,并且有 ->humanize()
方法来显示所有错误。您也可以遍历它并分别获取所有错误。将其转换为字符串会自动调用 ->humanize()
。
$errors = $asserts->getErrors($subject); foreach($errors => $error) { echo $error->getName(); echo $error->getMessage(); }
您还可以使用 getFirst
和 getNext
方法遍历 Errors 对象。
$errors = $asserts->validate($subject); echo $errors->getFirst(); echo $errors->getNext();
ValidateTrait
您还可以将一个特殊特性添加到对象中,使其变为“可验证”的。
use Harp\Validate\ValidateTrait; use Harp\Validate\Asserts; use Harp\Validate\Assert\Present; class Model { use ValidateTrait; public $test; public function getValidationAsserts() { return new Asserts(array( new Present('test'), )); } }
它将为您的类添加以下方法
可用的断言
回调
断言给定回调的结果为真。您必须使用闭包对象,并将主题和值作为参数接收。
new Callback('state', function ($subject, $value) { return $value !== 'test'; })
断言值不是一个有效的电子邮件地址。使用一个小巧快速的 regex,应该可以处理大多数情况。
new Email('email_address') new Email('email_address', 'some custom message')
EmailStrict
断言值不是一个有效的电子邮件地址。使用比 Email
更慢但更全面的检查。
new EmailStrict('email_address') new EmailStrict('email_address', 'some custom message')
GreaterThan
断言值大于设置的长度。值可以是 int、float 或甚至数字字符串
new GreaterThan('price', 20) new GreaterThan('price', 20, 'some custom message')
InArray
断言值在数组中,使用简单的 in_array
调用。如果数组为空,将抛出 InvalidArgumentException。
new InArray('state', array('big', 'small')) new InArray('state', array('big', 'small'), 'some custom message')
IP
断言值的 IP 地址是有效的,内部使用 filter_var()
。
new IP('last_login_ip') new IP('last_login_ip', 'some custom message')
IsInstanceOf
断言值是给定类的对象 is_a
调用。如果类不存在,将抛出 InvalidArgumentException。
new IsInstanceOf('state', 'My\Example\Item') new IsInstanceOf('state', 'My\Example\Item', 'some custom message')
LengthBetween
断言值的字符串长度在两个设置的长度之间(包括)。内部使用 mb_strlen()
。
new LengthLessThan('name', 10, 200) new LengthLessThan('name', 10, 200, 'some custom message')
LengthEquals
断言值是精确的字符串长度。内部使用 mb_strlen()
。
new LengthEquals('name', 20), new LengthEquals('name', 20, 'some custom message')
LengthGreaterThan
断言值的字符串长度大于设置的长度。内部使用 mb_strlen()
。
new LengthGreaterThan('name', 20) new LengthGreaterThan('name', 20, 'some custom message')
LengthLessThan
断言值的字符串长度小于设置的长度。内部使用 mb_strlen()
。
new LengthLessThan('name', 20) new LengthLessThan('name', 20, 'some custom message')
LessThan
断言值小于设置的长度。值可以是 int、float 或甚至数字字符串
new LessThan('price', 20) new LessThan('price', 20, 'some custom message')
Matches
断言一个属性的值与另一个属性的值匹配
new Matches('password', 'password_confirmation') new Matches('password', 'password_confirmation', 'some custom message')
IsInteger
断言值是整数。
new IsInteger('quantity') new IsInteger('quantity', 'some custom message')
IsFloat
断言值是浮点数。
new IsFloat('frequency') new IsFloat('frequency', 'some custom message')
Present
断言值是空的
new Present('title') new Present('title', 'some custom message if needed')
RegEx
断言值匹配给定的正则表达式。直接传递给 preg_match()
new RegEx('card_number', '/\d{20}/') new RegEx('card_number', '/\d{20}/', 'some custom message')
URL
断言值是有效的 URL。将 URL 中的所有 UTF 相关字符转换为正确的编码。它还将非 ASCII 域名转换为 "idn",如果可用。这类似于浏览器通常所做的。
new URL('website') new URL('website', 'some custom message')
URLStrict
断言值是有效的 URL。使用 php 的 filter_var()
方法。
new URLStrict('website') new URLStrict('website', 'some custom message')
AssertsTrait
这个特性使您能够轻松地将断言添加到另一个对象中。
class TestConfig { use AssertsTrait; } $config = new TestConfig(); $config ->assertPresent('name') ->assertURL('homepage', 'must have a valid homepage'); // Return the Asserts object $config->getAsserts();
以下是该特性添加的所有方法。
许可证
版权(c)2014,Clippings Ltd。由 Ivan Kerin 在 clippings.com 的部分开发。
根据 BSD-3-Clause 许可证,请参阅 LICENSE 文件。