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:----SIhPYXkoqPX2Rx/mkCUmvLG0tkAIVOElZe/G4MJT+d78rE8RdihVFFQXUuQMM5qMKsjXd9QQTNC2mJrni8z9iGMiwQ82lgs3qQGVt5oJD6M8PPgSxeQ0U3UrwGKc9wxQp0KU9y+rl73fGo2fY/Hi9D5Dnb7yjmyJ8G8BrG3LCAcl0FwtpXpyrVPQZUHGsL1AGfi8rkOeQb+TEeBBJXcMvCe/Vp77AlK7/uD1f814WxvksXgcTFPhl+vXWpz/Gw6+uiRDizAVjT7HyPcbZWGtM7vmC5tiV3rGBfpRT+BJuyRok74ieCIJ0cRClZ52c1+3IAT9B0tznDAXhxOiXxlL4SrtvTQGWGzWtY17n2oZwNRJmDCm3g/2ZjjGdSqyaN/Mu1W+t8+v0gjqjtjqs+CNEf6PhHVRTskqQ+98Qesq6qiCOuQb7V48+vdrk99hzFSUcnn5eyuWP31uO4lHwgrH/WDlJS9yPj64o5xoiLXuiS94g2Tso+oed4Ymc+pY42newHGsmMILh7eYOiBMxU5lE9azqIFLMPpJWIaOUxGM3qyzY9KMonRiQms3Wn16V/SPmFCmHozJ8S4krG1vFU9dWj1951e7fAGAgMtVbOObyFQ3yDk/jG8xSv3A8p9hk7dDq8gd7f4qDFbDyDjL5CFAVWlVVgyE9QxgLO7YpvgDzWg=----ATTACHMENT:----MTcxMTM3NTk1MTQwNDU1IDYyNDQzOTUzMzU0OTAxNjAgOTY1MTE0MzA5MTgyOTQyMQ==