Example #1
0
// 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.
$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 an empty array.
// 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 [];
        }