clippings/carpo

此包已被废弃,不再维护。作者建议使用harp-orm/validate包。

对象验证库

0.4 2014-08-28 08:39 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:32:36 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version

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();
}

您还可以使用getFirstgetNext方法遍历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'),
        ));
    }
}

它将添加以下方法到您的类中

方法 描述
validate() 执行getValidationAsserts()中指定的断言。将返回true或false,并将设置errors对象
getErrors() 返回一个Errors对象。如果尚未调用validate,将返回一个空的Errors对象
isEmptyErrors() 返回true或false
assertValid() 如果有任何错误,将抛出Harp\Validate\InvalidException

可用的断言

回调

断言给定回调的结果为true。您必须使用闭包对象,并将接收主题和值作为参数。

new Callback('state', function ($subject, $value) {
    return $value !== 'test';
})

电子邮件

断言值不是一个有效的电子邮件地址。使用一个小而快的正则表达式,应该可以处理大多数情况。

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

断言值是给定类的一个对象。如果类不存在,将抛出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相关字符转换为适当的编码。如果可用“intl”扩展,它还会将非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();

以下是此特性添加的所有方法。

方法 描述
getAsserts() 获取Asserts对象
addAssert(AbstractAssertion) 添加任意断言
assertCallback($name, $message) 添加Assert\Callback对象
assertEmail($name, $message) 添加Assert\Email对象
assertEmailStrict($name, $message) 添加Assert\EmailStrict对象
assertGreaterThan($name, $value, $message) 添加Assert\GreaterThan对象
assertInArray($name, $array, $message) 添加Assert\InArray对象
assertIP($name, $message) 添加Assert\IP对象
assertIsInteger($name, $message) 添加Assert\IsInteger对象
assertIsInstanceOf($name, $class, $message) 添加Assert\IsInstanceOf对象
assertIsFloat($name, $message) 添加Assert\IsFloat对象
assertLengthBetween($name, $min, $max, $message) 添加Assert\LengthBetween对象
assertLengthEquals($name, $length, $message) 添加Assert\LengthEquals对象
assertLengthGreaterThan($name, $length, $message) 添加Assert\LengthGreaterThan对象
assertLengthLessThan($name, $length, $message) 添加Assert\LengthLessThan对象
assertLessThan($name, $value, $message) 添加Assert\LessThan对象
assertMatches($name, $property, $message) 添加Assert\Matches对象
assertPresent($name, $message) 添加Assert\Present对象
assertRegEx($name, $pattern, $message) 添加Assert\RegEx对象
assertURL($name, $message) 添加Assert\URL对象
assertURLStrict($name, $message) 添加Assert\URLStrict对象

许可证

版权(c)2014,Clippings Ltd。由Ivan Kerin作为clippings.com的一部分开发。

在BSD-3-Clause许可证下,请参阅LICENSE文件。