/** * observe Employee event saving * 1. check need rehash * 2. unique username * 3. act, accept or refuse * * @param $model * @return bool */ public function saving($model) { $errors = new MessageBag(); //1. check need rehash if (Hash::needsRehash($model->password)) { $model->password = bcrypt($model->password); } if (is_null($model->id)) { $id = 0; } else { $id = $model->id; } //2. unique username if (!is_null($model->username)) { $other_employee = Employee::username($model->uniqid)->notid($id)->first(); if ($other_employee) { $errors->add('Employee', 'Username sudah terdaftar'); } if ($errors->count()) { $model['errors'] = $errors; return false; } } return true; }
/** * validate input parameter (need to parse) based on policy code * * @param array of policy (contain code) * @return boolean */ public static function generate($code, $id, $name) { $original = explode(' ', strtolower($name)); $modify = $original[0]; $countog = count($original) - 1; foreach ($original as $keyx => $valuex) { if (is_array($valuex) || $valuex != '') { $countog = $keyx; } } $idxuname = 0; do { $uname = Employee::username($modify . '.' . $code)->first(); if ($uname) { if (isset($original[$countog])) { $modify = $modify . $original[$countog][$idxuname]; } else { $modify = $modify . $modify; } $idxuname++; } } while ($uname); return $modify . '.' . $code; }