1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Webfactor\Laravel\Generators\Schemas; |
4
|
|
|
|
5
|
|
|
class MigrationField |
6
|
|
|
{ |
7
|
|
|
private $name; |
8
|
|
|
|
9
|
|
|
private $type; |
10
|
|
|
|
11
|
|
|
private $nullable = false; |
12
|
|
|
|
13
|
|
|
private $unique = false; |
14
|
|
|
|
15
|
|
|
private $default = null; |
16
|
|
|
|
17
|
|
|
private $foreign = null; |
18
|
|
|
|
19
|
|
|
public function __construct(string $field) |
20
|
|
|
{ |
21
|
|
|
$this->parse($field); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
private function parse(string $field) |
25
|
|
|
{ |
26
|
|
|
$params = collect(explode(':', $field)); |
27
|
|
|
|
28
|
|
|
$this->name = $params->pull(0); |
29
|
|
|
$this->type = $params->pull(1); |
30
|
|
|
|
31
|
|
|
foreach ($params as $param) { |
32
|
|
|
$this->fillObject($param); |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
private function fillObject(string $param) |
37
|
|
|
{ |
38
|
|
|
if ($param == 'nullable') { |
39
|
|
|
return $this->nullable = true; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ($param == 'unique') { |
43
|
|
|
return $this->unique = true; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($param == 'foreign') { |
47
|
|
|
return $this->foreign = true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (starts_with($param, 'default(')) { |
51
|
|
|
preg_match('/\((.*)\)/', $param, $match); |
52
|
|
|
|
53
|
|
|
return $this->default = $match[1]; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getName() |
61
|
|
|
{ |
62
|
|
|
return $this->name; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
|
|
public function getType() |
69
|
|
|
{ |
70
|
|
|
return $this->type; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return mixed |
|
|
|
|
75
|
|
|
*/ |
76
|
|
|
public function getDefault() |
77
|
|
|
{ |
78
|
|
|
return $this->default; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function isNullable() |
85
|
|
|
{ |
86
|
|
|
return $this->nullable; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function isUnique(): bool |
93
|
|
|
{ |
94
|
|
|
return $this->unique; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function makeValidationRule(): string |
98
|
|
|
{ |
99
|
|
|
$rule = 'required'; |
100
|
|
|
|
101
|
|
|
switch ($this->getType()) { |
102
|
|
|
|
103
|
|
|
case 'string': |
104
|
|
|
$rule .= '|between:3,255'; |
105
|
|
|
break; |
106
|
|
|
|
107
|
|
|
case 'integer': |
108
|
|
|
$rule .= '|integer'; |
109
|
|
|
break; |
110
|
|
|
|
111
|
|
|
case 'date': |
112
|
|
|
$rule .= '|date'; |
113
|
|
|
break; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $rule; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function makeColumn() |
120
|
|
|
{ |
121
|
|
|
return [ |
122
|
|
|
'name' => 'title', |
123
|
|
|
'type' => 'text', |
124
|
|
|
'label' => 'Title', |
125
|
|
|
]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function makeField() |
129
|
|
|
{ |
130
|
|
|
return [ |
131
|
|
|
'name' => 'title', |
132
|
|
|
'type' => 'text', |
133
|
|
|
'label' => 'Title', |
134
|
|
|
]; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.