public static function listOfObjects($livr, $ruleBuilders) { $validator = new \Validator\LIVR($livr); $validator->registerRules($ruleBuilders)->prepare(); return function ($objects, $params, &$outputArr) use($validator) { if (!isset($objects) || $objects === '') { return; } if (!is_array($objects) || \Validator\LIVR\Util::isAssocArray($objects)) { return 'FORMAT_ERROR'; } $results = array(); $errors = array(); $hasErrors = false; foreach ($objects as $object) { $result = $validator->validate($object); if ($result) { $errors[] = null; $results[] = $result; } else { $hasErrors = true; $errors[] = $validator->getErrors(); $results[] = null; } } if ($hasErrors) { return $errors; } else { $outputArr = $results; return; } }; }
public static function equalToField($field) { return function ($value, $params) use($field) { if (!isset($value) || $value === '') { return; } if (!\Validator\LIVR\Util::isStringOrNumber($value)) { return 'FORMAT_ERROR'; } if ($value != $params[$field]) { return 'FIELDS_NOT_EQUAL'; } return; }; }
public static function like($re) { $re = '/' . $re . '/'; if (func_num_args() == 3) { #Passed regexp flag $flags = func_get_arg(1); if ($flags && $flags != 'i') { throw new Exception("Only 'i' regexp flag supported, but '" . $flags . "' passed"); } $re .= $flags; } return function ($value) use($re) { if (!isset($value) or $value === '') { return; } if (!\Validator\LIVR\Util::isStringOrNumber($value)) { return 'FORMAT_ERROR'; } if (!preg_match($re, $value)) { return 'WRONG_FORMAT'; } return; }; }
private function autoTrim($data) { if (is_string($data)) { return trim($data); } elseif (\Validator\LIVR\Util::isAssocArray($data)) { $trimmedData = array(); foreach ($data as $key => $value) { $trimmedData[$key] = $this->autoTrim($value); } return $trimmedData; } return $data; }
public static function numberBetween($minNumer, $maxNumer) { return function ($value) use($minNumer, $maxNumer) { if (!isset($value) or $value === '') { return; } if (!\Validator\LIVR\Util::isStringOrNumber($value)) { return 'FORMAT_ERROR'; } if ($value < $minNumer) { return 'TOO_LOW'; } if ($value > $maxNumer) { return 'TOO_HIGH'; } return; }; }