Completed
Push — master ( 4d49f2...b9481e )
by Prateek
05:48 queued 03:41
created

Model::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 11
rs 10
c 0
b 0
f 0
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