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 oneOf() { $first_arg = func_get_arg(0); if (is_array($first_arg) && !\Validator\LIVR\Util::isAssocArray($first_arg)) { $allowedValues = $first_arg; } else { $allowedValues = func_get_args(); array_pop($allowedValues); # pop rule_builders } return function ($value) use($allowedValues) { if (!isset($value) or $value === '') { return; } if (!\Validator\LIVR\Util::isStringOrNumber($value)) { return 'FORMAT_ERROR'; } if (!in_array($value, $allowedValues)) { return 'NOT_ALLOWED_VALUE'; } 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; }