1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Crocodicstudio\Crudbooster\controllers\ApiController; |
4
|
|
|
|
5
|
|
|
use Crocodicstudio\Crudbooster\helpers\CRUDBooster; |
6
|
|
|
|
7
|
|
|
class ValidationRules |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param $param |
11
|
|
|
* @param $typeExcept |
12
|
|
|
* @param $table |
13
|
|
|
* @return string |
14
|
|
|
*/ |
15
|
|
|
public function make($param, $typeExcept, $table): string |
16
|
|
|
{ |
17
|
|
|
$type = $param['type']; |
18
|
|
|
$config = $param['config']; |
19
|
|
|
|
20
|
|
|
$format_validation = []; |
21
|
|
|
|
22
|
|
|
if ($param['required']) { |
23
|
|
|
$format_validation[] = 'required'; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
if (in_array($type, ['unique', 'exists'])) { |
27
|
|
|
$format_validation[] = $this->existOrUnique($config, $type); |
28
|
|
|
} elseif (in_array($type, ['date_format', 'digits_between', 'in', 'mimes', 'min', 'max', 'not_in'])) { |
29
|
|
|
$format_validation[] = $type.':'.$config; |
30
|
|
|
} elseif (! in_array($type, $typeExcept)) { |
31
|
|
|
$format_validation[] = $type; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if ($param['name'] == 'id') { |
35
|
|
|
$format_validation[] = 'exists:'.CRUDBooster::parseSqlTable($table)['table'].',id'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return implode('|', $format_validation); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $config |
43
|
|
|
* @param $type |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
private function existOrUnique($config, $type) |
47
|
|
|
{ |
48
|
|
|
$config = explode(',', $config); |
49
|
|
|
$table_exist = $config[0]; |
50
|
|
|
$table_exist = CRUDBooster::parseSqlTable($table_exist)['table']; |
51
|
|
|
$field_exist = $config[1]; |
52
|
|
|
$config = ($field_exist) ? $table_exist.','.$field_exist : $table_exist; |
53
|
|
|
|
54
|
|
|
return $type.':'.$config; |
55
|
|
|
} |
56
|
|
|
} |