2018-01-05 09:33:23 +08:00
|
|
|
<?php
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
namespace App\Interfaces;
|
2018-01-05 09:33:23 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
|
|
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
2018-01-05 09:33:23 +08:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* Class Controller
|
|
|
|
*/
|
|
|
|
abstract class Controller extends \Illuminate\Routing\Controller
|
2018-01-05 09:33:23 +08:00
|
|
|
{
|
2018-03-20 09:50:40 +08:00
|
|
|
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
|
|
|
|
|
2018-03-31 00:08:53 +08:00
|
|
|
/**
|
|
|
|
* Write a error to the flash and redirect the user to a route
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-31 00:08:53 +08:00
|
|
|
* @param $message
|
|
|
|
* @param $route
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-31 00:08:53 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function flashError($message, $route)
|
|
|
|
{
|
|
|
|
flash()->error($message);
|
|
|
|
return redirect(route($route))->withInput();
|
|
|
|
}
|
|
|
|
|
2018-01-05 09:33:23 +08:00
|
|
|
/**
|
|
|
|
* Shortcut function to get the attributes from a request while running the validations
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-05 09:33:23 +08:00
|
|
|
* @param Request $request
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param array $attrs_or_validations
|
|
|
|
* @param array $addtl_fields
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-05 09:33:23 +08:00
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return array
|
2018-01-05 09:33:23 +08:00
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function getFromReq($request, $attrs_or_validations, $addtl_fields = null)
|
2018-01-05 09:33:23 +08:00
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
// See if a list of values is passed in, or if a validation list is passed in
|
2018-01-05 09:33:23 +08:00
|
|
|
$is_validation = false;
|
2018-03-20 09:50:40 +08:00
|
|
|
if (\count(array_filter(array_keys($attrs_or_validations), '\is_string')) > 0) {
|
2018-01-05 09:33:23 +08:00
|
|
|
$is_validation = true;
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($is_validation) {
|
2018-01-05 09:33:23 +08:00
|
|
|
$this->validate($request, $attrs_or_validations);
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields = [];
|
2018-03-20 09:50:40 +08:00
|
|
|
foreach ($attrs_or_validations as $idx => $field) {
|
|
|
|
if ($is_validation) {
|
2018-01-05 09:33:23 +08:00
|
|
|
$field = $idx;
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($request instanceof Request) {
|
2018-01-05 09:33:23 +08:00
|
|
|
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 {
|
2018-08-27 02:51:47 +08:00
|
|
|
/* @noinspection NestedPositiveIfStatementsInspection */
|
2018-03-20 09:50:40 +08:00
|
|
|
if (array_key_exists($field, $request)) {
|
2018-01-05 09:33:23 +08:00
|
|
|
$fields[$field] = $request[$field];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if (!empty($addtl_fields) && \is_array($addtl_fields)) {
|
2018-01-05 09:33:23 +08:00
|
|
|
$fields = array_merge($fields, $addtl_fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple normalized method for forming the JSON responses
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-05 09:33:23 +08:00
|
|
|
* @param $message
|
2018-08-27 00:40:04 +08:00
|
|
|
* @param null|mixed $count
|
|
|
|
*
|
2018-01-05 09:33:23 +08:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
|
|
|
*/
|
2018-03-20 09:50:40 +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-08-27 00:40:04 +08:00
|
|
|
'message' => $message,
|
2018-01-05 10:08:22 +08:00
|
|
|
];
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if ($count !== null) {
|
2018-01-05 10:08:22 +08:00
|
|
|
$attrs['count'] = $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($attrs);
|
2018-01-05 09:33:23 +08:00
|
|
|
}
|
|
|
|
}
|