Skip to content

Instantly share code, notes, and snippets.

@clouddueling
Created May 4, 2014 16:55
Show Gist options
  • Save clouddueling/edc4692771713d0fdcf9 to your computer and use it in GitHub Desktop.
Save clouddueling/edc4692771713d0fdcf9 to your computer and use it in GitHub Desktop.
<?php namespace Modules\Api;
use Validator;
use User;
use Input;
use Response;
use Sanitize;
class Api
{
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 extend($base = array(), $replacements = array())
{
$base = !is_array($base) ? array() : $base;
$replacements = !is_array($replacements) ? array() : $replacements;
$ret = array();
foreach ($base as $key => $val) {
if (is_int($key)) { // value without key
$ret[$val] = '';
} else {
$ret[$key] = $val;
}
}
return array_replace_recursive($ret, $replacements);
}
public static function inputs($defaults = array())
{
$inputs = json_decode(file_get_contents('php://input'), true);
if (empty($inputs)) {
$inputs = Input::all();
}
return static::extend($defaults, static::clean($inputs));
}
public static function is_eloquent($obj)
{
if (! is_object($obj)) {
return false;
}
if (isset($obj->attributes)) {
return true;
}
return false;
}
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)
{
$v = Validator::make(array(
'email' => $email
), array(
'email' => 'required|email',
));
if ($v->fails()) {
$message = current($v->errors->messages);
return $message[0];
}
return true;
}
public static function is_email_unique($email)
{
if (! self::is_email($email)) {
return false;
}
$user_check = User::where_email($email)
->where_deleted(0)
->first();
if ($user_check) {
return false;
}
return true;
}
public static function update(&$model, $ignore = array(), $inputs = false)
{
$inputs = $inputs === false ? self::inputs() : $inputs;
$ignore = is_array($ignore) ? $ignore : [];
$guarded = method_exists($model, 'guarded') ? $model->guarded() : [];
foreach ($inputs as $key => $value) {
if (in_array($key, $ignore) || in_array($key, $guarded) || isset($ignore[$key])) {
continue;
}
$model->$key = $inputs[$key];
}
// When ignoring values make sure they are set in case they are used later.
foreach ($ignore as $key => $value) {
if (! isset($inputs[$key])) {
// Arrays
if (is_array($value) || is_object($value)) {
$inputs[$key] = $value;
continue;
}
// Values
if (is_numeric($key) ) {
$inputs[$value] = "";
continue;
}
// Keys with values
$inputs[$key] = $value;
}
}
return $inputs;
}
public static function response($data, $status = 200, $headers = [])
{
return Response::make($data, $status, $headers);
}
public static function json($data)
{
return str_replace(
array(
"&amp;",
"&lt;",
),
array(
"&",
"<"
),
json_encode(static::to_json($data), JSON_NUMERIC_CHECK)
);
}
public static function to_json($obj)
{
if (is_object($obj)) {
$obj = static::is_eloquent($obj) ? to_json($obj) : $obj;
}
if (is_array($obj)) {
$new = array();
foreach($obj as $key => $val) {
$new[$key] = self::to_json($val);
}
} else {
$new = $obj;
}
return $new;
}
public static function success($message = true)
{
return json([
'success' => $message
]);
}
public static function error($message)
{
return json([
'error' => $message
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment