Passed
Push — master ( 62919d...1f65fa )
by Iman
04:23
created

ValidationRules::make()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 16
nop 3
dl 0
loc 24
rs 8.9137
c 0
b 0
f 0
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
}