protected function innerSet($key, $val) { $setMethod = 'set' . ucfirst($key); if (method_exists($this, $setMethod)) { $this->{$setMethod}($val); } else { $validateMethod = 'validate' . ucfirst($key); if (method_exists($this, $validateMethod)) { $validateResult = $this->{$validateMethod}($val); if (false === $validateResult) { return; } if ($validateResult instanceof \Generator) { $errors = new MultiException(); foreach ($validateResult as $error) { if ($error instanceof \Exception) { $errors[] = $error; } } if (!$errors->isEmpty()) { throw $errors; } } } $sanitizeMethod = 'sanitize' . ucfirst($key); if (method_exists($this, $sanitizeMethod)) { $val = $this->{$sanitizeMethod}($val); } if ('' == $key) { $this->__data[] = $val; } else { $this->__data[$key] = $val; } } }
public function register($data) { $errors = new MultiException(); if (empty($data->email)) { $errors->add('Не введен e-mail', self::ERROR_INVALID_EMAIL); } if (empty($data->password)) { $errors->add('Не введен пароль', self::ERROR_INVALID_PASSWORD); } if (empty($data->password2)) { $errors->add('Не введено подтверждение пароля', self::ERROR_INVALID_PASSWORD); } if ($data->password2 != $data->password) { $errors->add('Введенные пароли не совпадают', self::ERROR_INVALID_PASSWORD); } if (empty($data->firstname)) { $errors->add('Не введено имя', self::ERROR_INVALID_FIRSTNAME); } if (empty($data->lastname)) { $errors->add('Не введена фамилия', self::ERROR_INVALID_LASTNAME); } if (empty($data->phonenum)) { $errors->add('Не введен номер телефона', self::ERROR_INVALID_PHONENUM); } if (!$errors->isEmpty()) { throw $errors; } $user = User::findByEmail($data->email); if (!empty($user)) { $errors->add('Такой e-mail уже зарегистрирован', self::ERROR_INVALID_EMAIL); } if (!$errors->isEmpty()) { throw $errors; } $app = Application::getInstance(); /*if ($app->config->extensions->captcha->register) { if (empty($data->captcha)) { $errors->add('Не введена строка с картинки', self::ERROR_INVALID_CAPTCHA); } else { if (!$app->extensions->captcha->checkKeyString($data->captcha)) { $errors->add('Неверные символы с картинки', self::ERROR_INVALID_CAPTCHA); } } }*/ if (!$errors->isEmpty()) { throw $errors; } $user = new User(); $user->email = $data->email; $user->password = \T4\Crypt\Helpers::hashPassword($data->password); $user->save(); return $user; }
public function fill(array $data) { $errors = new MultiException(); foreach ($data as $key => $value) { try { $this->{$key} = $value; } catch (\Exception $e) { $errors[] = $e; } } /** * @var MultiException $errors */ if (!$errors->isEmpty()) { throw $errors; } }
/** * @param \T4\Core\IArrayable|array $data * @return static $this * @throws \T4\Core\MultiException */ public function fill($data) { if ($data instanceof IArrayable) { $data = $data->toArray(); } else { $data = (array) $data; } $errors = new MultiException(); foreach ($data as $key => $value) { try { $this->{$key} = $value; } catch (\Exception $e) { if ($e instanceof MultiException) { $errors->merge($e); } else { $errors->add($e); } } } if (!$errors->isEmpty()) { throw $errors; } return $this; }