dbeurive/input

此包包含一个非常基础的输入管理器。

1.0.1 2016-05-01 08:35 UTC

This package is not auto-updated.

Last update: 2024-09-20 17:42:53 UTC


README

此包是简单输入管理器的实现。

输入验证是一个常见的任务。此过程通常涉及两个步骤

  • 首先,我们单独验证每个输入。
  • 然后,如果所有输入单独有效,我们将在与其他输入的关系中验证输入集。

例如,让我们考虑注册表单的验证。

  • 一些字段是“独立”的。例如,名字就是这种情况。输入值的有效性不依赖于其他字段的值。
  • 然而,其他字段不能独立于其他字段进行验证。通常,这种情况适用于“密码”字段。通常,注册表单要求您确认密码。因此,您有两个密码输入字段,这些字段的值必须相同。

此包实现了这个简单的2步验证过程。

  • 首先,您指定每个输入。这些规范将被用于验证每个输入相对于其他输入。一个规范包含以下信息:输入的名称是什么?输入是否必需?输入的值可以是null吗?输入需要特殊验证器吗?
  • 然后,您将先前创建的规范分组到一组规范中。您可以为规范集分配一个全局验证器,该验证器将用于验证所有条目相对于其他条目的关系。请注意,此可选验证器在所有输入单独有效(根据其各自的规范)时执行。

输入规范是dbeurive\Input\Specification的实例。规范集是dbeurive\Input\SpecificationsSet的实例。

此时,所有输入的单独有效性规则都已定义。还指定了一个可选的全局验证器,以确保输入值之间的一致性。您可以将一组输入值提交给规范集。

安装

从命令行

composer require dbeurive/input.

从您的composer.json文件

{
    "require": {
        "dbeurive/input": "1.0.*"
    }
}

API文档

API的详细文档可以通过使用PhpDocumentor从代码中提取。文件phpdoc.xml包含PhpDocumentor所需的配置。要生成API文档,只需将此包的根目录移动到该位置并运行PhpDocumentor

注意

由于所有PHP代码都使用PhpDoc注释进行文档化,因此您应该能够从您最喜欢的IDE中利用自动完成功能。如果您使用Eclipse、NetBeans或PhPStorm,您可能不需要查阅生成的API文档。

概述

指定输入

use dbeurive\Input\Specification;
use dbeurive\Input\SpecificationsSet;

// Define a validator for the option.
// Please note that defining a validator for an input is optional.
$pathValidator = function($inPath) {
    if (file_exists($inPath)) {
        return true;
    }
    return "The file which path is \"$inPath\" does not exist.";
};

// We say that:
//   - The name of the input is "Path".
//   - The input is mandatory.
//   - The input can not be null.
//   - The input has a validator.
$pathSpecification = new Specification("Path", true, false, $pathValidator);

// You can also use mutators to specify an input.
// The input named "token" is not mandatory and its value can be null.
// It does not have any specific validator.
$tokenSpecification = new Specification("Token");
$tokenSpecification->setMandatory(false)
    ->setCanBeNull();

创建一组规范

$set = new SpecificationsSet();
$set->addInputSpecification($pathSpecification)
    ->addInputSpecification($tokenSpecification);
    
// Print a summary.
            
foreach ($set->inputsSummary() as $_name => $_summary) {
    echo "$_name => $_summary\n";
}

// Note: you may specify a final validator.
// If the file exists, and if a token is specified, then make sure that the token is found in the file.
// If everything is OK, the validator must return true.
// Otherwise, it must return a list of errors' identifiers (you are free to return any kind of values...).
// Note: here we return a list of error messages
$finalValidator = function($inInputs) {
    $data = file_get_contents($inInputs['Path']);
    if (array_key_exists('Token', $inInputs) && (! is_null($inInputs['Token']))) {
        if (false === strstr($data, $inInputs['Token'])) {
            return ["The file " . $inInputs['Path'] . " exists, but it does not contain the token <" . $inInputs['Token'] . "> !"];
        } else {
            return true;
        }
    }
    return true;
};

$set->setValidator($finalValidator);

测试一组输入值与规范集

$values = ['Path' => '/tmp/my-file.txt', 'Token' => 'Port'];

$status = $set->check($values);

检查状态。

if ($status) {

    // Inputs are valid.
    echo "The set of inputs' values is valid\n";

} else {

    // Inputs are not valid.

    // Check the validity of errors looked in isolation from the others.
    if ($set->hasErrorsOnInputsInIsolationFromTheOthers()) {

        echo "Some inputs' values are not valid:\n";
        foreach ($set->getErrorsOnInputsInIsolationFromTheOthers() as $_inputName => $_errorIdentifier) {
            // Here, we returned strings (error messages)... but you can return whatever objects you want...
            echo "  - $_inputName: $_errorIdentifier\n";
        }

        exit(0); // The final validator is not executed.
    }

    echo "All inputs' values are individually valid.\n";

    // This means that the final validation failed !
    echo "But the final validation failed:\n";
    foreach ($set->getErrorsOnFinalValidation() as $_index => $_errorIdentifier) {
        // Here, we returned strings (error messages)... but you can return whatever objects you want...
        echo "  - $_errorIdentifier\n";
    }
}

示例

example.php:此示例展示了如何使用该包。