/** * This method checks a given configuration. * @param array $inConfiguration List of parameters that define the configuration. * @return array If the given configuration is valid, then the method returns an empty array. * Otherwise, the method returns a list of error messages. */ public static function checkConfiguration(array $inConfiguration) { $set = new SpecificationsSet(); $set->addInputSpecification(new Specification(DocOption::DOC_PATH))->addInputSpecification(new Specification(DocOption::SCHEMA_PATH))->addInputSpecification(new Specification(EntryPointOption::PROC_BASE_NS))->addInputSpecification(new Specification(EntryPointOption::SQL_BASE_NS))->addInputSpecification(new Specification(EntryPointOption::PROC_REPO_PATH))->addInputSpecification(new Specification(EntryPointOption::SQL_REPO_PATH)); if ($set->check($inConfiguration)) { return []; } return array_values($set->getErrorsOnInputsInIsolationFromTheOthers()); }
/** * {@inheritdoc} * @see InterfaceConnector */ public static function checkConfiguration(array $inConfiguration) { $set = new SpecificationsSet(); foreach (self::getConfigurationParameters() as $_parameterSpec) { $name = $_parameterSpec[InterfaceConnector::OPTION_NAME]; $mandatory = $_parameterSpec[InterfaceConnector::OPTION_MANDATORY]; $set->addInputSpecification(new Specification($name, $mandatory, !$mandatory)); } if ($set->check($inConfiguration)) { return []; } return array_values($set->getErrorsOnInputsInIsolationFromTheOthers()); }
} 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"; } 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"; } }
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()); }