/** * 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 "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. $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'] . "> !"];
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()); }