dabrahim/array-validator

PHP 的数组验证器。用于处理 $_GET / $_POST 数组,非常实用。

v1.2.0 2018-10-19 12:15 UTC

This package is auto-updated.

Last update: 2024-09-20 02:36:15 UTC


README

一个自定义库,用于简化表单数据验证的痛苦。非常适合验证 $_POST$_GET 数组

入门指南

  1. 需要 PHP 5.4.x
  2. 需要 composer
  3. 切换到工作目录并运行 composer require dabrahim/array-validator

基本用法

$constraints = array(
    'email' => (object)[
        'prettyName' => 'E-mail',
        'type' => ArrayValidator::TYPE_EMAIL,
    ],
    'firstName' => (object) [
        'prettyName' => 'Prénom',
        'type' => ArrayValidator::TYPE_NAME
    ],
    'lastName' => (object) [
        'prettyName' => 'Nom',
        'type' => ArrayValidator::TYPE_NAME
    ]
);

$a = array(
    'email' => 'john.doe@gmail.com',
    'lastName' => 'Doe',
    'firstName' => 'John'
);

try {
    $av = new ArrayValidator($a, $constraints);
    $av->validate();

    echo "Tout est OK !";

} catch (InvalidValueFormat $e) {
    echo "User error: " .$e->getMessage();

} catch (MissingKeyException $e) {
    echo "User error: " . $e->getMessage();

} catch (Exception $e) {
    echo "Developer error: " . $e->getMessage();
}