-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiController.php
More file actions
109 lines (96 loc) · 3.08 KB
/
Copy pathApiController.php
File metadata and controls
109 lines (96 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
namespace App\Controllers;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Validation\Exceptions\ValidationException;
use Config\Services;
use Config\App;
use Psr\Log\LoggerInterface;
use Exception;
/**
* Class ApiController
*/
class ApiController extends BaseController
{
/**
* Instance of the main Request object.
*
* @var IncomingRequest|CLIRequest
*/
protected $request;
/**
* An array of helpers to be loaded automatically upon
* class instantiation. These helpers will be available
* to all other controllers that extend BaseController.
*
* @var array
*/
protected $helpers = [];
/**
* Constructor.
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @param LoggerInterface $logger
*/
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->db = \Config\Database::connect();
$this->app = new App;
}
public function ResponceError(mixed $message)
{
return Services::ResponceError($message);
}
public function ResponceItem(string $message, mixed $data, mixed $extdata = [])
{
return Services::ResponceItem($message, $data, $extdata);
}
public function ResponceList(string $message, mixed $records, int $page = 1, int $limit = 10, int $find_records = 0, int $total_records = 0)
{
return Services::ResponceList($message, $records, $page, $limit, $find_records, $total_records);
}
public function getUserByJWT(IncomingRequest $request)
{
$authenticationHeader = $request->getServer('HTTP_AUTHORIZATION');
try {
helper('jwt');
$encodedToken = getJWTFromRequest($authenticationHeader);
return validateJWTFromRequest($encodedToken);
} catch (Exception $e) {
return false;
}
}
public function getRequestInput(IncomingRequest $request)
{
try {
$input = json_decode($request->getBody(), true, 512, JSON_THROW_ON_ERROR);
} catch (\Throwable $th) {
return [];
}
return $input;
}
public function validateRequest($input, array $rules, array $messages = [])
{
$this->validator = Services::Validation()->setRules($rules);
if (is_string($rules)) {
$validation = config('Validation');
if (!isset($validation->$rules)) {
throw ValidationException::forRuleNotFound($rules);
}
if (!$messages) {
$errorName = $rules . '_errors';
$messages = $validation->$errorName ?? [];
}
$rules = $validation->$rules;
}
return $this->validator->setRules($rules, $messages)->run($input);
}
public function ResponceCORS()
{
return Services::ResponseCORS();
}
}