42 lines
1.1 KiB
PHP
Executable File
42 lines
1.1 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Traits;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
trait IsAPI
|
|
{
|
|
|
|
/**
|
|
* @author kai_khanh_97 🐃🐃🐃
|
|
*
|
|
* @param array $data
|
|
* @param int $statusCode
|
|
*
|
|
* @return \Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory
|
|
*/
|
|
public function apiResponse($data, $statusCode = 200)
|
|
{
|
|
return response(json_encode($data, JSON_UNESCAPED_SLASHES))
|
|
->header('Content-Type', 'application/json; charset=utf-8')
|
|
->setStatusCode($statusCode);
|
|
}
|
|
|
|
/**
|
|
* @author kai_khanh_97 🐃🐃🐃
|
|
*
|
|
* @param array $rules
|
|
*
|
|
* @return \Illuminate\Http\Response|\Illuminate\Contracts\Routing\ResponseFactory
|
|
*/
|
|
public function apiValidation(Request $req, array $rules, array $messages = [])
|
|
{
|
|
$validator = validator($req->all(), $rules, $messages);
|
|
if ($validator->fails()) {
|
|
$response = $this->apiResponse($validator->errors(), 400);
|
|
throw new ValidationException($validator, $response);
|
|
}
|
|
}
|
|
}
|