Last active
December 28, 2015 11:39
-
-
Save clouddueling/7494826 to your computer and use it in GitHub Desktop.
Simplifying a rest php api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Api | |
| { | |
| public static function is_password($password, $confirm) | |
| { | |
| $v = Validator::make(array( | |
| 'password' => $password, | |
| 'password_confirmation' => $confirm | |
| ), array( | |
| 'password' => 'required|confirmed' | |
| )); | |
| if ($v->fails()) { | |
| $message = current($v->errors->messages); | |
| return $message[0]; | |
| } | |
| return true; | |
| } | |
| public static function is_email($email, $unique = true) | |
| { | |
| $v = Validator::make(array( | |
| 'email' => $email | |
| ), array( | |
| 'email' => 'required|email' . ($unique ? '|unique:users' : ''), | |
| )); | |
| if ($v->fails()) { | |
| return $v->errors; | |
| } | |
| return true; | |
| } | |
| public static function clean($html) | |
| { | |
| if (is_array($html)) { | |
| foreach ($html as $key => $value) | |
| $html[$key] = s($value); | |
| return $html; | |
| } | |
| return Sanitize::purify($html); | |
| } | |
| public static function get_inputs() | |
| { | |
| if ($_SERVER['REQUEST_METHOD'] == 'PUT') { | |
| $request_string = file_get_contents('php://input'); | |
| $inputs = json_decode($request_string, 1); | |
| } else { | |
| $inputs = Input::all(); | |
| } | |
| return static::clean($inputs); | |
| } | |
| public static function update(&$model, $ignore = array()) | |
| { | |
| $inputs = self::get_inputs(); | |
| foreach ($inputs as $key => $value) { | |
| if (is_array($ignore)) { | |
| if (in_array($key, $ignore)) { | |
| continue; | |
| } | |
| } | |
| $model->$key = $s[$key]; | |
| } | |
| return $inputs; | |
| } | |
| public static function json($data) | |
| { | |
| return json($data); | |
| } | |
| public static function success($message = true) | |
| { | |
| return Response::json(array( | |
| 'success' => $message | |
| )); | |
| } | |
| public static function error($message) | |
| { | |
| return Response::json(array( | |
| 'error' => $message | |
| ), 404); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Api_V1_User_Controller extends Base_Controller | |
| { | |
| public function post_index() | |
| { | |
| $user = new User; | |
| $s = Api::update($user, array('password', 'password_confirmation')); | |
| $pass_check = Api::is_password($s['password'], $s['password_confirmation']); | |
| if ($pass_check !== true) { | |
| return Api::error($pass_check); | |
| } | |
| if (! Api::is_email($user->email)) { | |
| return Api::error("Either email was invalid or is already registered."); | |
| } | |
| $user->account_user_id = Auth::user()->account_user_id; | |
| $user->password = Hash::make($s['password']); | |
| $user->permissions = Auth::user()->permissions; | |
| $user->save(); | |
| Api::success(array( | |
| 'user' => to_json($user), | |
| )); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment