예제 #1
0
 protected function setUp()
 {
     $validatorV1 = function ($inValue) {
         if (1 === preg_match('/^A|B|C$/', $inValue)) {
             return true;
         }
         return "The given value <{$inValue}> is not valid.";
     };
     $validatorV2 = function ($inValue) {
         if (1 === preg_match('/^A|B|C$/', $inValue)) {
             return true;
         }
         return "The given value <{$inValue}> is not valid.";
     };
     $this->__validator1 = $validatorV1;
     $this->__validator2 = $validatorV2;
     $this->__inputA = new Specification("A");
     // Is not mandatory, may be null, no validator.
     $this->__inputB = new Specification("B");
     // Is not mandatory, must not be null, no validator.
     $this->__inputV1 = new Specification("V1");
     // Is not mandatory, must not be null, validator.
     $this->__inputC = new Specification("C");
     // Is mandatory, may be null, no validator.
     $this->__inputD = new Specification("D");
     // Is mandatory, must not be null, no validator.
     $this->__inputV2 = new Specification("V2");
     // Is mandatory, must not be null, validator.
     $this->__inputA->setMandatory(false)->setCanBeNull();
     $this->__inputB->setMandatory(false)->setCanNotBeNull();
     $this->__inputV1->setMandatory(false)->setCanNotBeNull()->setValidator($validatorV1);
     $this->__inputC->setMandatory(true)->setCanBeNull();
     $this->__inputD->setMandatory(true)->setCanNotBeNull();
     $this->__inputV2->setMandatory(true)->setCanNotBeNull()->setValidator($validatorV2);
     $this->__set = new SpecificationsSet();
     $this->__set->addInputSpecification($this->__inputA)->addInputSpecification($this->__inputB)->addInputSpecification($this->__inputC)->addInputSpecification($this->__inputD)->addInputSpecification($this->__inputV1)->addInputSpecification($this->__inputV2);
 }
예제 #2
0
파일: example.php 프로젝트: dbeurive/input
    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.
$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'])) {