This package contains a very basic inputs manager.
This package is an implementation of a simple inputs' manager., (*1)
Inputs validation is a common task. This process often involves two steps :, (*2)
For example, letâs consider the validation of a register form., (*3)
This package implements this simple 2 steps validation process., (*4)
Inputs' specifications are instances of
dbeurive\Input\Specification
. A specifications' set is an instance ofdbeurive\Input\SpecificationsSet
., (*5)
At this point, all inputsâ individual validity rules are defined. And an optional global validator that ensures a coherence between inputsâ values is specified. You can submit a set of inputs' values to the specificationsâ set., (*6)
From the command line:, (*7)
composer require dbeurive/input.
From your composer.json
file:, (*8)
{ "require": { "dbeurive/input": "1.0.*" } }
The detailed documentation of the API can be extracted from the code by using PhpDocumentor.
The file phpdoc.xml
contains the required configuration for PhpDocumentor
.
To generate the API documentation, just move into the root directory of this package and run PhpDocumentor
from this location., (*9)
Note:, (*10)
Since all the PHP code is documented using PhpDoc annotations, you should be able to exploit the auto completion feature from your favourite IDE. If you are using Eclipse, NetBeans or PhPStorm, you probably wonât need to consult the generated API documentation., (*11)
Specify inputs:, (*12)
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();
Create a set of specifications:, (*13)
$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);
Test a set of inputs' values against the set of specifications:, (*14)
$values = ['Path' => '/tmp/my-file.txt', 'Token' => 'Port']; $status = $set->check($values);
Inspect the status., (*15)
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: this example shows how to use the package., (*16)