public function testPositive()
 {
     Validator\LIVR::registerDefaultRules(array('my_ucfirst' => function () {
         return function ($value, $params, &$outputRef) {
             if (!isset($value) or $value == '') {
                 return;
             }
             $outputRef = ucfirst($value);
             return;
         };
     }, 'my_lc' => function () {
         return function ($value, $params, &$outputRef) {
             if (!isset($value) or $value == '') {
                 return;
             }
             $outputRef = mb_strtolower($value);
             return;
         };
     }, 'my_trim' => function () {
         return function ($value, $params, &$outputRef) {
             if (!isset($value) or $value == '') {
                 return;
             }
             $outputRef = trim($value);
             return;
         };
     }));
     $validator = new Validator\LIVR(array('word1' => array('my_trim', 'my_lc', 'my_ucfirst'), 'word2' => array('my_trim', 'my_lc'), 'word3' => array('my_ucfirst')));
     $output = $validator->validate(array('word1' => ' wordOne ', 'word2' => ' wordTwo ', 'word3' => 'wordThree '));
     $this->assertEquals($output, array('word1' => 'Wordone', 'word2' => 'wordtwo', 'word3' => 'WordThree '));
 }
 /**
  * @dataProvider negativeTestsProvider
  */
 public function testNegative($data, $test)
 {
     print "Negative test [{$test}] is run\n";
     $validator = new Validator\LIVR($data['rules']);
     $output = $validator->validate($data['input']);
     $this->assertFalse($output ? true : false, 'Validator should return false');
     $this->assertEquals($data['errors'], $validator->getErrors(), 'Validator should contain valid errors');
 }
 public function assertLIVR($data, $livr, $params = array())
 {
     $validator = new \Validator\LIVR($livr);
     $validated = $validator->validate($data);
     $errors = $validator->getErrors();
     if ($errors) {
         throw new \Exception("Failed asserting by LIVR rules. " . (count($params) ? "Params: " . print_r($params, 1) : '') . "Errors: " . print_r($errors, 1) . "data: " . print_r($data, 1));
     }
     return $validated;
 }
 public function testNegative()
 {
     print "NEGATIVE: Validate data with automatic trim\n";
     $input = array('code' => '   ', 'password' => ' 12  ', 'address' => array('street' => '  hell '));
     $expectedErrors = array('code' => 'REQUIRED', 'password' => 'TOO_SHORT', 'address' => array('street' => 'TOO_SHORT'));
     $validator = new Validator\LIVR($this->rules, true);
     $output = $validator->validate($input);
     if ($output) {
         throw new \Exception('Should contain error codes');
     }
     $this->assertEquals($expectedErrors, $validator->getErrors());
 }
Beispiel #5
0
 /**
  * コンストラクタ。
  *
  * @param array $rule 検証&フィルタ設定
  *
  */
 public function __construct($rule)
 {
     parent::__construct();
     $_ = $this;
     Validator\LIVR::defaultAutoTrim(true);
     // 検証&フィルタの設定
     $_->validator = new Validator\LIVR($rule);
     // カスタム検証ルール/フィルタの登録
     $_->registValidator();
 }
Beispiel #6
0
 public static function validate($data, $livr)
 {
     \Validator\LIVR::registerDefaultRules(['latin_string' => function () {
         return function ($value) {
             if (!isset($value) || $value === '') {
                 return;
             }
             if (!is_string($value)) {
                 return 'FORMAT_ERROR';
             }
             $validStringReg = '/^[a-zA-Z0-9\\-\\_\\+\\#№"\']+$/';
             if (!preg_match($validStringReg, $value)) {
                 return 'WRONG_STRING';
             }
             return;
         };
     }, 'phone' => function () {
         return function ($value) {
             if (!isset($value) || $value === '') {
                 return;
             }
             if (!is_string($value)) {
                 return 'FORMAT_ERROR';
             }
             $reg = '/^[0-9]{10}$/';
             if (!preg_match($reg, $value)) {
                 return 'WRONG_FORMAT';
             }
             return;
         };
     }]);
     $validator = new \Validator\LIVR($livr);
     $validated = $validator->validate($data);
     $errors = $validator->getErrors();
     if ($errors) {
         throw new X(['Type' => 'FORMAT_ERROR', 'Fields' => $errors]);
     }
     return $validated;
 }
Beispiel #7
0
 public static function listOfDifferentObjects($selectorField, $livrs, $ruleBuilders)
 {
     $validators = array();
     foreach ($livrs as $selectorValue => $livr) {
         $validator = new \Validator\LIVR($livr);
         $validator->registerRules($ruleBuilders)->prepare();
         $validators[$selectorValue] = $validator;
     }
     return function ($objects, $params, &$outputArr) use($validators, $selectorField) {
         $results = array();
         $errors = array();
         $hasErrors = false;
         foreach ($objects as $object) {
             if (!is_array($object) || !isset($object[$selectorField]) || !$validators[$object[$selectorField]]) {
                 $errors[] = 'FORMAT_ERROR';
                 continue;
             }
             $validator = $validators[$object[$selectorField]];
             $result = $validator->validate($object);
             if ($result) {
                 $results[] = $result;
                 $errors[] = null;
             } else {
                 $results[] = null;
                 $errors[] = $validator->getErrors();
                 $hasErrors = true;
             }
         }
         if ($hasErrors) {
             return $errors;
         } else {
             $outputArr = $results;
             return;
         }
     };
 }
Beispiel #8
0
 private function buildAliasedRule($alias)
 {
     if (!$alias['name']) {
         throw new \Exception("Alias name required");
     }
     if (!$alias['rules']) {
         throw new \Exception("Alias rules required");
     }
     return function ($ruleBuilders) use($alias) {
         $validator = new \Validator\LIVR(array('value' => $alias['rules']));
         $validator->registerRules($ruleBuilders)->prepare();
         return function ($value, $params, &$outputArr) use($validator, $alias) {
             $result = $validator->validate(array('value' => $value));
             if ($result) {
                 $outputArr = $result['value'];
                 return;
             } else {
                 $errors = $validator->getErrors();
                 return isset($alias['error']) ? $alias['error'] : $errors['value'];
             }
         };
     };
 }