Passed
Push — master ( 284e72...816a06 )
by Iman
04:12
created

FieldDetector::isExceptional()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 1
dl 0
loc 3
c 0
b 0
f 0
cc 1
rs 10
1
<?php
2
3
namespace crocodicstudio\crudbooster\Modules\ModuleGenerator\ControllerGenerator;
4
5
class FieldDetector
6
{
7
    /**
8
     * @param $fieldName string
9
     * @return bool
10
     */
11
    public static function isExceptional($fieldName)
12
    {
13
        return in_array($fieldName, ['id', 'created_at', 'updated_at', 'deleted_at']);
14
    }
15
16
    /**
17
     * @param $fieldName string
18
     * @return bool
19
     */
20
    public static function isForeignKey($fieldName)
21
    {
22
        return starts_with($fieldName, 'id_') || ends_with($fieldName, '_id');
23
    }
24
25
    public static function isUploadField($fieldName)
26
    {
27
        return self::isWithin($fieldName, 'UPLOAD_TYPES');
28
    }
29
30
    public static function isWithin($fieldName, $configKey)
31
    {
32
        return in_array($fieldName, explode(',', cbConfig($configKey)));
33
    }
34
35
    public static function detect($colName)
36
    {
37
        $map = [
38
            'isPassword',
39
            'isImage',
40
            'isGeographical',
41
            'isPhone',
42
            'isEmail',
43
            'isNameField',
44
            'isUrlField',
45
        ];
46
        foreach ($map as $methodName){
47
            if (self::$methodName($colName)) {
48
                return $methodName;
49
            }
50
        }
51
        return 'not found';
52
    }
53
54
    /**
55
     * @param $fieldName string
56
     * @return bool
57
     */
58
    public static function isPassword($fieldName)
59
    {
60
        return self::isWithin($fieldName, 'PASSWORD_FIELDS_CANDIDATE');
61
    }
62
63
    /**
64
     * @param $fieldName string
65
     * @return bool
66
     */
67
    public static function isImage($fieldName)
68
    {
69
        return self::isWithin($fieldName, 'IMAGE_FIELDS_CANDIDATE');
70
    }
71
72
    /**
73
     * @param $fieldName string
74
     * @return bool
75
     */
76
    public static function isGeographical($fieldName)
77
    {
78
        return in_array($fieldName, ['latitude', 'longitude']);
79
    }
80
81
    /**
82
     * @param $fieldName string
83
     * @return bool
84
     */
85
    public static function isPhone($fieldName)
86
    {
87
        return self::isWithin($fieldName, 'PHONE_FIELDS_CANDIDATE');
88
    }
89
90
    /**
91
     * @param $fieldName string
92
     * @return bool
93
     */
94
    public static function isEmail($fieldName)
95
    {
96
        return self::isWithin($fieldName, 'EMAIL_FIELDS_CANDIDATE');
97
    }
98
99
    /**
100
     * @param $fieldName string
101
     * @return bool
102
     */
103
    public static function isNameField($fieldName)
104
    {
105
        return self::isWithin($fieldName, 'NAME_FIELDS_CANDIDATE');
106
    }
107
108
    /**
109
     * @param $fieldName string
110
     * @return bool
111
     */
112
    public static function isUrlField($fieldName)
113
    {
114
        return self::isWithin($fieldName, 'URL_FIELDS_CANDIDATE');
115
    }
116
}