|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Prateekkarki\Laragen\Generators; |
|
4
|
|
|
use Prateekkarki\Laragen\Models\Module; |
|
5
|
|
|
|
|
6
|
|
|
class Model extends BaseGenerator implements GeneratorInterface |
|
7
|
|
|
{ |
|
8
|
|
|
public function generate(Module $module) |
|
9
|
|
|
{ |
|
10
|
|
|
$this->setModule($module); |
|
11
|
|
|
|
|
12
|
|
|
$migrationTemplate = $this->buildTemplate('Model', [ |
|
13
|
|
|
'{{modelName}}' => $module->getModelName(), |
|
14
|
|
|
'{{massAssignables}}' => $this->getMassAssignables(), |
|
15
|
|
|
'{{foreignMethods}}' => $this->getForeignMethods() |
|
16
|
|
|
]); |
|
17
|
|
|
|
|
18
|
|
|
file_put_contents(base_path("app/Models/" . $module->getModelName() . ".php"), $migrationTemplate); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
protected function getMassAssignables() |
|
22
|
|
|
{ |
|
23
|
|
|
$massAssignables = []; |
|
24
|
|
|
|
|
25
|
|
|
foreach ($this->module->getData() as $column => $optionString) { |
|
26
|
|
|
$optionArray = explode(':', $optionString); |
|
27
|
|
|
if (in_array($optionArray[0], ['string', 'int', 'text', 'bool', 'date'])) { |
|
28
|
|
|
$massAssignables[] = "'" . $column . "'"; |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return implode(', ', $massAssignables); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function getForeignMethods() |
|
36
|
|
|
{ |
|
37
|
|
|
$foreignMethods = ""; |
|
38
|
|
|
|
|
39
|
|
|
foreach ($this->module->getData() as $column => $optionString) { |
|
40
|
|
|
$optionArray = explode(':', $optionString); |
|
41
|
|
|
if (in_array($optionArray[0], ['parent'])) { |
|
42
|
|
|
$foreignMethods = $this->buildTemplate('Model-parent', [ |
|
43
|
|
|
'{{parent}}' => str_singular($optionArray[1]), |
|
44
|
|
|
'{{parentModel}}' => ucfirst(camel_case(str_singular($optionArray[1]))), |
|
45
|
|
|
]); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $foreignMethods; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|