2018-01-05 09:33:23 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
|
|
|
|
|
|
|
class RestController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Shortcut function to get the attributes from a request while running the validations
|
|
|
|
* @param Request $request
|
|
|
|
* @param array $attrs_or_validations
|
|
|
|
* @param array $addtl_fields
|
|
|
|
* @return array
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
|
|
|
*/
|
|
|
|
public function getFromReq($request, $attrs_or_validations, $addtl_fields=null)
|
|
|
|
{
|
|
|
|
# See if a list of values is passed in, or if a validation list is passed in
|
|
|
|
$is_validation = false;
|
|
|
|
if(\count(array_filter(array_keys($attrs_or_validations), '\is_string')) > 0) {
|
|
|
|
$is_validation = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($is_validation) {
|
|
|
|
$this->validate($request, $attrs_or_validations);
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields = [];
|
|
|
|
foreach($attrs_or_validations as $idx => $field) {
|
|
|
|
if($is_validation) {
|
|
|
|
$field = $idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($request instanceof Request) {
|
|
|
|
if ($request->filled($field)) {
|
2018-01-05 11:05:26 +08:00
|
|
|
$fields[$field] = $request->input($field);
|
2018-01-05 09:33:23 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(array_key_exists($field, $request)) {
|
|
|
|
$fields[$field] = $request[$field];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!empty($addtl_fields) && \is_array($addtl_fields)) {
|
|
|
|
$fields = array_merge($fields, $addtl_fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run a validation
|
|
|
|
* @param $request
|
|
|
|
* @param $rules
|
|
|
|
* @return bool
|
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
|
|
|
*/
|
2018-01-05 10:08:22 +08:00
|
|
|
public function validate($request, $rules)
|
2018-01-05 09:33:23 +08:00
|
|
|
{
|
2018-01-05 10:08:22 +08:00
|
|
|
if($request instanceof Request) {
|
|
|
|
$validator = Validator::make($request->all(), $rules);
|
|
|
|
} else {
|
|
|
|
$validator = Validator::make($request, $rules);
|
|
|
|
}
|
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
if (!$validator->passes()) {
|
|
|
|
throw new BadRequestHttpException($validator->errors(), null, 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple normalized method for forming the JSON responses
|
|
|
|
* @param $message
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2018-01-05 10:08:22 +08:00
|
|
|
public function message($message, $count=null)
|
2018-01-05 09:33:23 +08:00
|
|
|
{
|
2018-01-05 10:08:22 +08:00
|
|
|
$attrs = [
|
2018-01-05 09:33:23 +08:00
|
|
|
'message' => $message
|
2018-01-05 10:08:22 +08:00
|
|
|
];
|
|
|
|
|
|
|
|
if($count !== null) {
|
|
|
|
$attrs['count'] = $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($attrs);
|
2018-01-05 09:33:23 +08:00
|
|
|
}
|
|
|
|
}
|