1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Prateekkarki\Laragen\Models; |
4
|
|
|
|
5
|
|
|
class DataOption |
6
|
|
|
{ |
7
|
|
|
const TYPE_PARENT = 'parent'; |
8
|
|
|
|
9
|
|
|
const TYPE_RELATED = 'related'; |
10
|
|
|
|
11
|
|
|
const COLUMN_UNIQUE = 'unique'; |
12
|
|
|
|
13
|
|
|
protected $specialType; |
14
|
|
|
|
15
|
|
|
protected $uniqueFlag; |
16
|
|
|
|
17
|
|
|
protected $size; |
18
|
|
|
|
19
|
|
|
protected $column; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Array of data type options |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $optionArray; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Key to type conversion array. |
30
|
|
|
* |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $keyToType = [ |
34
|
|
|
'int' =>'integer', |
35
|
|
|
'string' =>'string', |
36
|
|
|
'bool' =>'boolean', |
37
|
|
|
'text' =>'text', |
38
|
|
|
'date' =>'datetime', |
39
|
|
|
'datetime' =>'datetime' |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
public function __construct($columnName, $optionString) |
43
|
|
|
{ |
44
|
|
|
$this->column = $columnName; |
45
|
|
|
$this->size = false; |
46
|
|
|
$this->optionArray = explode(':', $optionString); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getSchema() |
50
|
|
|
{ |
51
|
|
|
if ($this->hasSpecialSchema()) { |
52
|
|
|
$schema = $this->processSpecialSchema(); |
53
|
|
|
}else{ |
54
|
|
|
foreach ($this->optionArray as $option) { |
55
|
|
|
if ($option == self::COLUMN_UNIQUE) $this->hasUnique(); |
56
|
|
|
if (is_numeric($option) && $option <= 2048) $this->hasSize((int)$option); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$schema = '$table->' . $this->getType() . "('{$this->column}'"; |
60
|
|
|
$schema .= $this->hasSize() ? ", {$this->getSize()})" : ")"; |
61
|
|
|
$schema .= $this->isUnique() ? "->unique()" : ""; |
62
|
|
|
$schema .= ";"; |
63
|
|
|
} |
64
|
|
|
return $schema; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function getType(){ |
68
|
|
|
return $this->keyToType[array_shift($this->optionArray)]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function getSize(){ |
72
|
|
|
return $this->size; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function isUnique(){ |
76
|
|
|
return $this->uniqueFlag; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function hasUnique($set = true){ |
80
|
|
|
$this->uniqueFlag = ($set === true) ? true : false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function hasSize($size = null){ |
84
|
|
|
if ($size !== null) { |
85
|
|
|
$this->size = $size; |
86
|
|
|
} |
87
|
|
|
return $this->size; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getTabs($number) |
91
|
|
|
{ |
92
|
|
|
$schema = ""; |
93
|
|
|
for ($i=0; $i < $number; $i++) { |
94
|
|
|
$schema .= " "; |
95
|
|
|
} |
96
|
|
|
return $schema; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function processSpecialSchema(){ |
100
|
|
|
$specialMethod = 'process' . ucfirst($this->specialType); |
101
|
|
|
return $this->$specialMethod(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function processParent(){ |
105
|
|
|
$schema = ""; |
106
|
|
|
$parent = array_pop($this->optionArray); |
107
|
|
|
$schema .= "\$table->integer('" . str_singular($parent) . "_id')->unsigned()->nullable();"; |
108
|
|
|
$schema .= PHP_EOL . $this->getTabs(3); |
109
|
|
|
$schema .= "\$table->foreign('" . str_singular($parent) . "_id')->references('id')->on('$parent')->onDelete('set null');"; |
110
|
|
|
return $schema; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function processRelated(){ |
114
|
|
|
return ""; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function hasSpecialSchema(){ |
118
|
|
|
if ($this->optionArray[0] == self::TYPE_PARENT) { |
119
|
|
|
array_shift($this->optionArray); |
120
|
|
|
$this->specialType = self::TYPE_PARENT; |
121
|
|
|
return true; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ($this->optionArray[0] == self::TYPE_RELATED) { |
125
|
|
|
array_shift($this->optionArray); |
126
|
|
|
$this->specialType = self::TYPE_PARENT; |
127
|
|
|
return true; |
128
|
|
|
} |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|