Esempio n. 1
0
// 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 [];
        }
    }
    return [];
};
$set->setValidator($finalValidator);
// Test a set of inputs' values against the set of specifications:
$values = ['Path' => '/tmp/my-file.txt', 'Token' => 'Port'];
$status = $set->check($values);
// Inspect the status.
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";
Esempio n. 2
0
 function testCheckOptionsList()
 {
     // A:  Is not mandatory, may be null, no validator.
     // B:  Is not mandatory, must not be null, no validator.
     // V1: Is not mandatory, must not be null, validator.
     // C:  Is mandatory, may be null, no validator.
     // D:  Is mandatory, must not be null, no validator.
     // V2: Is mandatory, must not be null, validator.
     // -------------------------------------------------------------------------------------------------------------
     // Test without validator.
     // -------------------------------------------------------------------------------------------------------------
     // A, B and V1 are not mandatory.
     $list = ['C' => null, 'D' => 10, 'V2' => 'A'];
     $this->assertTrue($this->__set->check($list));
     $this->assertFalse($this->__set->hasErrorsOnInputsInIsolationFromTheOthers());
     $this->assertFalse($this->__set->hasErrorsOnFinalValidation());
     // A may be null.
     // B and V1 are not mandatory.
     $list = ['A' => null, 'C' => null, 'D' => 10, 'V2' => 'A'];
     $this->assertTrue($this->__set->check($list));
     $this->assertFalse($this->__set->hasErrorsOnInputsInIsolationFromTheOthers());
     $this->assertFalse($this->__set->hasErrorsOnFinalValidation());
     $list = ['A' => 1, 'C' => null, 'D' => 10, 'V2' => 'A'];
     $this->assertTrue($this->__set->check($list));
     $this->assertFalse($this->__set->hasErrorsOnInputsInIsolationFromTheOthers());
     $this->assertFalse($this->__set->hasErrorsOnFinalValidation());
     // A may be null.
     // B must not be null.
     $list = ['A' => null, 'B' => 1, 'C' => null, 'D' => 10, 'V2' => 'A'];
     $this->assertTrue($this->__set->check($list));
     // V1 is not mandatory.
     $this->assertFalse($this->__set->hasErrorsOnInputsInIsolationFromTheOthers());
     $this->assertFalse($this->__set->hasErrorsOnFinalValidation());
     $list = ['A' => null, 'B' => null, 'C' => null, 'D' => 10, 'V2' => 'A'];
     $this->assertFalse($this->__set->check($list));
     // B is not valid.
     $this->assertCount(1, $this->__set->getErrorsOnInputsInIsolationFromTheOthers());
     $this->assertTrue($this->__set->hasErrorsOnInputsInIsolationFromTheOthers());
     $this->assertFalse($this->__set->hasErrorsOnFinalValidation());
     // A may be null.
     // B must not be null.
     // V1 must be "A|B|C".
     $list = ['A' => null, 'B' => 1, 'V1' => 'B', 'C' => null, 'D' => 10, 'V2' => 'A'];
     $this->assertTrue($this->__set->check($list));
     $this->assertFalse($this->__set->hasErrorsOnInputsInIsolationFromTheOthers());
     $this->assertFalse($this->__set->hasErrorsOnFinalValidation());
     $list = ['A' => null, 'B' => 1, 'V1' => 'D', 'C' => null, 'D' => 10, 'V2' => 'A'];
     $this->assertFalse($this->__set->check($list));
     // V1 is not valid
     $this->assertCount(1, $this->__set->getErrorsOnInputsInIsolationFromTheOthers());
     $this->assertTrue($this->__set->hasErrorsOnInputsInIsolationFromTheOthers());
     $this->assertFalse($this->__set->hasErrorsOnFinalValidation());
     $list = ['A' => null, 'B' => null, 'V1' => 'D', 'C' => null, 'D' => 10, 'V2' => 'A'];
     $this->assertFalse($this->__set->check($list));
     // V1 and B are not valid
     $this->assertCount(2, $this->__set->getErrorsOnInputsInIsolationFromTheOthers());
     $this->assertTrue($this->__set->hasErrorsOnInputsInIsolationFromTheOthers());
     $this->assertFalse($this->__set->hasErrorsOnFinalValidation());
     // C is mandatory.
     // D is mandatory.
     // V2 is mandatory.
     $list = ['A' => null, 'B' => 1, 'V1' => 'B'];
     $this->assertFalse($this->__set->check($list));
     $this->assertCount(3, $this->__set->getErrorsOnInputsInIsolationFromTheOthers());
     // C, D, V2 are not valid.
     $this->assertTrue($this->__set->hasErrorsOnInputsInIsolationFromTheOthers());
     $this->assertFalse($this->__set->hasErrorsOnFinalValidation());
     // C is mandatory.
     // D must not be null.
     // V2 is mandatory.
     $list = ['A' => null, 'B' => 1, 'V1' => 'B', 'D' => null];
     $this->assertFalse($this->__set->check($list));
     $this->assertCount(3, $this->__set->getErrorsOnInputsInIsolationFromTheOthers());
     // C, D, V2 are not valid.
     $this->assertTrue($this->__set->hasErrorsOnInputsInIsolationFromTheOthers());
     $this->assertFalse($this->__set->hasErrorsOnFinalValidation());
     // -------------------------------------------------------------------------------------------------------------
     // Test with validator.
     // -------------------------------------------------------------------------------------------------------------
     $validator = function ($inInputs) {
         if (array_key_exists('A', $inInputs) && array_key_exists('B', $inInputs)) {
             if (is_int($inInputs['A']) && is_int($inInputs['B'])) {
                 return [];
             }
             return ["A and B must be integers!"];
         }
         return [];
     };
     $this->__set->setValidator($validator);
     // A may be null.
     // B must not be null.
     // But, if A and B are simultaneously defined, then they must be integers => check will fail.
     $list = ['A' => null, 'B' => 1, 'C' => null, 'D' => 10, 'V2' => 'A'];
     $this->assertFalse($this->__set->check($list));
     $this->assertCount(0, $this->__set->getErrorsOnInputsInIsolationFromTheOthers());
     // No error on parameters.
     $this->assertCount(1, $this->__set->getErrorsOnFinalValidation());
     // But the final test fails.
     $this->assertFalse($this->__set->hasErrorsOnInputsInIsolationFromTheOthers());
     $this->assertTrue($this->__set->hasErrorsOnFinalValidation());
     // A may be null.
     // B must not be null.
     // A and B are integers => the check will succeed.
     $list = ['A' => 10, 'B' => 1, 'C' => null, 'D' => 10, 'V2' => 'A'];
     $this->assertTrue($this->__set->check($list));
     $this->assertFalse($this->__set->hasErrorsOnInputsInIsolationFromTheOthers());
     $this->assertFalse($this->__set->hasErrorsOnFinalValidation());
 }