示例#1
0
 public static function create($response, $user)
 {
     if (count($user) != 6) {
         return putError('invalid request parameters', Users::ERROR_FORMAT, $response);
     }
     $user[Users::FNAME_KEY] = Validator::filterName($user, Users::FNAME_KEY);
     $user[Users::LNAME_KEY] = Validator::filterName($user, Users::LNAME_KEY);
     $user[Users::EMAIL_KEY] = Validator::filterEmail($user, Users::EMAIL_KEY);
     $user[Users::GENDER_KEY] = Validator::filterGender($user, Users::GENDER_KEY);
     $user[Users::PASSWORD_KEY] = Validator::filterPassword($user, Users::PASSWORD_KEY);
     $user[Users::BIRTHDATE_KEY] = Validator::filterDate($user, Users::BIRTHDATE_KEY);
     $user[Users::REMOTE_ADDR_KEY] = $_SERVER['REMOTE_ADDR'];
     if (!$user[Users::FNAME_KEY]) {
         return putError('invalid firstname parameter', Users::ERROR_FNAME_FORMAT, $response);
     } else {
         if (!$user[Users::LNAME_KEY]) {
             return putError('invalid lastname parameter', Users::ERROR_LNAME_FORMAT, $response);
         } else {
             if (!$user[Users::EMAIL_KEY]) {
                 return putError('invalid email parameter', Users::ERROR_EMAIL_FORMAT, $response);
             } else {
                 if (!$user[Users::PASSWORD_KEY]) {
                     return putError('invalid password parameter', Users::ERROR_PASSWORD_FORMAT, $response);
                 } else {
                     if (!$user[Users::GENDER_KEY]) {
                         return putError('invalid gender parameter', Users::ERROR_GENDER_FORMAT, $response);
                     } else {
                         if (!$user[Users::BIRTHDATE_KEY]) {
                             return putError('invalid birthdate parameter', Users::ERROR_BIRTHDATE_FORMAT, $response);
                         }
                     }
                 }
             }
         }
     }
     $user[Users::GENDER_KEY] = $user[Users::GENDER_KEY] == "male";
     $duplicate = UsersDB::duplicate($user[Users::EMAIL_KEY]);
     if ($duplicate) {
         return putError('duplicate email parameter', Users::ERROR_EMAIL_DUPLICATE, $response);
     }
     $user = UsersDB::create($user);
     if ($user === FALSE) {
         return putError('database connection error', DATABASE::ERROR_DATABASE_CONN, $response);
     }
     return putJsonBody(array('error' => false, 'result' => array('user_id' => $user[Users::ID_KEY], 'token' => $user[Users::TOKEN_KEY])), 200, $response);
 }