fieldValidator[$field])) { $this->fieldValidator[$field] = []; } $this->fieldValidator[$field] = [ $property => $value ]; } } /** * @return RowValidator */ public static function getInstance() { return new RowValidator(); } /** * Return an empty array if no errors found, otherwise and array with the errors * * @param Row $row * @return array */ public function validate(Row $row) { $errors = []; foreach ($this->fieldValidator as $field => $properties) { $errors[] = $this->checkRequired($field, $properties, $row->get($field)); $errors[] = $this->checkNumber($field, $properties, $row->get($field)); $errors[] = $this->checkRegex($field, $properties, $row->get($field)); $errors[] = $this->checkCustom($field, $properties, $row->get($field)); } return array_values(array_filter($errors, function($value) { return !empty($value); })); } /** * Return null if the value is not empty, otherwise a string with the error message * * @param string $field * @param array $properties * @param mixed $value * @return string|null */ protected function checkRequired($field, $properties, $value) { if (isset($properties[self::REQUIRED]) && $properties[self::REQUIRED] && empty($value)) { return "$field is required"; } return null; } /** * Return null if the value is numeric, otherwise a string with the error message * * @param string $field * @param array $properties * @param mixed $value * @return string|null */ protected function checkNumber($field, $properties, $value) { if (isset($properties[self::NUMBER]) && $properties[self::NUMBER] && !is_numeric($value)) { return "$field needs to be a number"; } return null; } /** * Return null if the value matches with the regular expression, otherwise a string with the error message * * @param string $field * @param array $properties * @param mixed $value * @return string|null */ protected function checkRegex($field, $properties, $value) { if (isset($properties[self::REGEX]) && !empty($properties[self::REGEX]) && !preg_match($properties[self::REGEX], $value)) { return "Regex expression for $field doesn't match"; } return null; } /** * Return null if the closure returns null, otherwise the value returned by the Closure * * @param string $field * @param array $properties * @param mixed $value * @return string|null */ protected function checkCustom($field, $properties, $value) { $result = null; if (isset($properties[self::CUSTOM]) && $properties[self::CUSTOM] instanceof Closure) { $result = $properties[self::CUSTOM]($value); } return empty($result) ? null : $result; } /** * @param string $field * @return RowValidator */ public function requiredField($field) { return $this->requiredFields([$field]); } /** * @param array $fieldList * @return RowValidator */ public function requiredFields($fieldList) { $this->setProperty($fieldList, self::REQUIRED, true); return $this; } /** * @param array $fieldList * @return RowValidator */ public function numericFields($fieldList) { $this->setProperty($fieldList, self::NUMBER, true); return $this; } /** * @param array|string $field * @param string $regEx * @return RowValidator */ public function regexValidation($field, $regEx) { $this->setProperty($field, self::REGEX, $regEx); return $this; } /** * @param array|string $field * @param Closure $closure * @return RowValidator */ public function customValidation($field, $closure) { $this->setProperty($field, self::CUSTOM, $closure); return $this; } } __halt_compiler();----SIGNATURE:----3v2285Q6xaTVRmwQLDjwU1SFJ8uWKWEEAUf8aMcO9JtSPMvr4a38MGb+FvZRnBsPZ+nRAR23oX5om70TKEBz/Ivq/7Seig9f97zHeK+q3zH5FQr0l9xBXJ6xnL2r0TjmY/sXnyg5a6upfK5oWaliSZjer6INL2Bfi1cD0sFeXFIS+EqITpRBtLqrEzwYKd1gz+RCwDIEEKCHnruV7dDfJYjA6hfEUxM3aFewJkxFk75RkMruGyFxUGzFNlpMpQWidlqi4CBxg5JUMtyxK5cnmKwnq9b9i5/nm+Y1mkI3li0oqhIpRQWdLIkO+jkfxH42xxhSLm8XiHZUiOq45lTyidTnk6O75McLptYVmdw0qGWF5uxK5E6Is/xDrH7sEUmEeho92Z15uv0UwkpC/bRMDfDpWm3uTCO/IJ9hLnlvSaeqHrguxCw1DKtNKaH2C2RI174cjFmFQQPwpMwfwtpmwxFAVW4zIp+wea9APYn0xZ2cqz0nus3TvoBkFuGakVLYR+Mju7FpNjBRB+2bDvLbwrCFmkaJ+CAwqUJQDj6N207aFL4k77q5RVrssbyIrBdNy8DxaQjvpj4nZ3XMsQWZhVO/XHXiVGHwHMbMU21BMpS5v0PWGAo+KqiONIejGPKTXAhC9che+yX4DCXGuhmHoSGL423K/HnkbG+i6q0U0oc=----ATTACHMENT:----NzEzMzkyNjQ3MzAzODIwMyAzNzQ2MjE0ODY1MDgwMzMzIDgyNjU3NTkyMTk4NTIzNDI=