1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Crocodicstudio\Crudbooster\Controllers\ApiController; |
4
|
|
|
|
5
|
|
|
class ApiValidations |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @param $rowApi |
9
|
|
|
* @param $ctrl |
10
|
|
|
* @return mixed |
11
|
|
|
*/ |
12
|
|
|
public static function checkApiDefined($rowApi, $ctrl) |
13
|
|
|
{ |
14
|
|
|
if (!is_null($rowApi)) { |
15
|
|
|
return true; |
16
|
|
|
} |
17
|
|
|
//todo : translation |
18
|
|
|
$msg = 'Sorry this API is no longer available, maybe has changed by admin, or please make sure api url is correct.'; |
19
|
|
|
$result = ApiResponder::makeResult(0, $msg); |
20
|
|
|
ApiResponder::send($result, request()->all(), $ctrl); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param $methodType |
26
|
|
|
* @param $ctrl |
27
|
|
|
* @return mixed |
28
|
|
|
*/ |
29
|
|
|
public static function validateMethodType($methodType, $ctrl) |
30
|
|
|
{ |
31
|
|
|
if (!is_null($methodType) && request()->isMethod($methodType)) { |
32
|
|
|
return true; |
33
|
|
|
} |
34
|
|
|
//todo : translation |
35
|
|
|
$result = ApiResponder::makeResult(0, "The request method is not allowed !"); |
36
|
|
|
ApiResponder::send($result, request()->all(), $ctrl); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param $ctrl |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
public static function doCustomPreCheck($ctrl) |
44
|
|
|
{ |
45
|
|
|
$ctrl->hookValidate(); |
46
|
|
|
|
47
|
|
|
if (! $ctrl->validate) { |
48
|
|
|
return true; |
49
|
|
|
} // hook have to return true |
50
|
|
|
//todo : translation |
51
|
|
|
$result = ApiResponder::makeResult(0, 'Failed to execute API !'); |
52
|
|
|
ApiResponder::send($result, request()->all(), $ctrl); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $rowApi |
58
|
|
|
* @param $ctrl |
59
|
|
|
*/ |
60
|
|
|
public static function doValidations($rowApi, $ctrl) |
61
|
|
|
{ |
62
|
|
|
/* Check the row is exists or not */ |
63
|
|
|
self::checkApiDefined($rowApi, $ctrl); |
64
|
|
|
|
65
|
|
|
/* Method Type validation */ |
66
|
|
|
self::validateMethodType($rowApi->method_type, $ctrl); |
67
|
|
|
|
68
|
|
|
/* Do some custom pre-checking for posted data, if failed discard API execution */ |
69
|
|
|
self::doCustomPreCheck($ctrl); |
70
|
|
|
} |
71
|
|
|
} |