Completed
Push — master ( ed7824...4c153c )
by Maarten
01:49 queued 10s
created

RuleParser   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 126
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A rulesToMigrationColumns() 0 19 5
A getType() 0 10 3
A getMax() 0 10 2
A isRequired() 0 5 2
A isUnique() 0 4 1
A isUnsigned() 0 4 1
A isForeign() 0 4 1
A contains() 0 4 1
1
<?php
2
3
namespace Mtolhuys\LaravelSchematics\Services;
4
5
class RuleParser
6
{
7
    public static $columnTypes = [
8
        'date',
9
        'dateTime',
10
        'decimal',
11
        'integer',
12
        'string',
13
        'text',
14
        'time',
15
        'timestamp',
16
        'unsigned',
17
    ];
18
19
    /**
20
     * Parse the rules to column creation methods
21
     *
22
     * @param $rules
23
     * @return string
24
     */
25
    public static function rulesToMigrationColumns(array $rules): string
26
    {
27
        $columns = '$table->increments(\'id\');' . PHP_EOL;
28
29
        foreach ($rules as $column => $rule) {
30
            $type = self::getType($rule);
31
            $max = self::getMax($rule);
32
            $unsigned = self::isUnsigned($rule) ? '->unsigned()' : '';
33
            $nullable = self::isRequired($rule) ? '' : '->nullable()';
34
            $unique = self::isUnique($rule) ? '->unique()' : '';
35
36
            $columns .=
37
                str_repeat(' ', 12) .
38
                "\$table->{$type}('$column'{$max}){$unsigned}{$nullable}{$unique};" .
39
                PHP_EOL;
40
        }
41
42
        return $columns . str_repeat(' ', 12) . '$table->timestamps();';
43
    }
44
45
    /**
46
     * Check if $rule contains any of the supported type
47
     * WARNING: be aware of word length vs. word matching f.e. 'date' <-> 'dateTime'
48
     * In that case the longest word should appear last in the array
49
     *
50
     * @param $rule
51
     * @return mixed|string
52
     */
53
    public static function getType($rule)
54
    {
55
        foreach (self::$columnTypes as $type) {
56
            if (stripos($rule, strtolower($type)) !== false) {
57
                return $type;
58
            }
59
        }
60
61
        return 'string';
62
    }
63
64
    /**
65
     * Parses the max:* rule
66
     *
67
     * @param $rule
68
     * @return string
69
     */
70
    public static function getMax($rule): string
71
    {
72
        $max = (int)substr($rule, strpos($rule, 'max:') + 4);
73
74
        if ($max > 0) {
75
            return ", $max";
76
        }
77
78
        return '';
79
    }
80
81
    /**
82
     * Checks if column can be set to nullable
83
     *
84
     * @param $rule
85
     * @return boolean
86
     */
87
    public static function isRequired($rule): bool
88
    {
89
        return self::contains($rule, 'required')
90
            || self::isUnique($rule);
91
    }
92
93
    /**
94
     * Checks if columns needs to be set to unique
95
     *
96
     * @param $rule
97
     * @return boolean
98
     */
99
    public static function isUnique($rule): bool
100
    {
101
        return self::contains($rule, 'unique');
102
    }
103
104
    /**
105
     * Checks if columns is unsigned
106
     *
107
     * @param $rule
108
     * @return boolean
109
     */
110
    public static function isUnsigned($rule): bool
111
    {
112
        return self::contains($rule, 'unsigned');
113
    }
114
115
    /**
116
     * Checks if columns needs to be constructed as a foreign key
117
     *
118
     * @param $rule
119
     * @return boolean
120
     */
121
    public static function isForeign($rule): bool
122
    {
123
        return self::contains($rule, 'foreign');
124
    }
125
126
    private static function contains($rule, $needle): bool
127
    {
128
        return stripos($rule, $needle) !== false;
129
    }
130
}
131
132