Created
November 21, 2013 18:12
-
-
Save clouddueling/7586643 to your computer and use it in GitHub Desktop.
API Handler that makes processing CRUD and other requests more automatic.
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_eloquent($obj) | |
| { | |
| if (is_array($obj)) { | |
| $obj = isset($obj[0]) ? $obj[0] : $obj; | |
| } | |
| return ($obj instanceof Laravel\Database\Eloquent\Model); | |
| } | |
| 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', | |
| )); | |
| if ($v->fails()) { | |
| $message = current($v->errors->messages); | |
| return $message[0]; | |
| } | |
| if ($unique) { | |
| $user_check = static::is_email_unique($email); | |
| if ($user_check !== true) { | |
| return "Email is already taken."; | |
| } | |
| } | |
| return true; | |
| } | |
| public static function is_email_unique($email) | |
| { | |
| $user_check = User::where_email($email) | |
| ->where_deleted(0) | |
| ->first(); | |
| if ($user_check) { | |
| return false; | |
| } | |
| 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 inputs($defaults = array()) | |
| { | |
| if (in_array($_SERVER['REQUEST_METHOD'], array('PUT', 'POST'))) { | |
| $request_string = file_get_contents('php://input'); | |
| $inputs = json_decode($request_string, 1); | |
| } else { | |
| $inputs = Input::all(); | |
| } | |
| return extend($defaults, static::clean($inputs)); | |
| } | |
| public static function update(&$model, $ignore = array()) | |
| { | |
| $inputs = self::inputs(); | |
| foreach ($inputs as $key => $value) { | |
| if (is_array($ignore)) { | |
| if (in_array($key, $ignore)) { | |
| continue; | |
| } | |
| } | |
| $model->$key = $inputs[$key]; | |
| } | |
| return $inputs; | |
| } | |
| public static function json($data) | |
| { | |
| $new_data = array(); | |
| foreach($data as $key => $value) { | |
| if (static::is_eloquent($value)) { | |
| $value = to_json($value); | |
| } | |
| $new_data[$key] = $value; | |
| } | |
| return json($new_data); | |
| } | |
| public static function success($message = true) | |
| { | |
| return Response::json(array( | |
| 'success' => $message | |
| )); | |
| } | |
| public static function error($message) | |
| { | |
| return Response::json(array( | |
| 'error' => $message | |
| )); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment