*/ abstract class AbstractFormValidator { /** * configuration of validation * @var array */ protected $validation = []; /** * * @var array */ protected $errors = []; /** * * @var boolean */ protected $isValid; /** * valid a form. * @param array $form * @return $this */ public function validate(array $form) { $this->isValid = true; foreach ($form as $field => $value) { if (array_key_exists($field, $this->validation)) { $this->validField($field, $value, $this->validation[$field]); } } return $this; } /** * add an error message * @param type $field * @param type $message * @return \oat\oatbox\validator\AbstractFormValidator */ protected function addError($field, $message) { if (array_key_exists($field, $this->errors)) { $this->errors[$field][] = $message; } else { $this->errors[$field] = [$message]; } return $this; } /** * execute all validation for a field * @param string $field * @param mixed $value * @param array $config */ protected function validField($field, $value, $config) { foreach ($config as $validator) { $class = $validator['class']; $option = $validator['options']; /* @var $test ValidatorInterface */ $test = new $class($option); if (!$this->executeTest($value, $test)) { $this->isValid = false; $this->addError($field, $test->getMessage()); } } } /** * execute a validator * @param mixed $value * @param \oat\oatbox\validator\ValidatorInterface $validator * @return boolena */ protected function executeTest($value, ValidatorInterface $validator) { return $validator->evaluate($value); } /** * return all errors * @return array */ public function getErrors() { return $this->errors; } /** * return error message for a field * @param string $field * @return array */ public function getError($field) { if (array_key_exists($field, $this->errors)) { return $this->errors[$field]; } return null; } /** * return form valiation status * @return boolean * @throws \RuntimeException */ public function isValid() { if (is_null($this->isValid)) { throw new \RuntimeException('you must validate a form'); } return $this->isValid; } }