Passed
Push — master ( 2ab272...5c38aa )
by Rougin
02:42
created

ViewGenerator::prepareData()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 22
nc 4
nop 1
dl 0
loc 33
ccs 0
cts 27
cp 0
crap 12
rs 9.568
c 0
b 0
f 0
1
<?php
2
3
namespace Rougin\Combustor\Generator;
4
5
use Rougin\Combustor\Common\Tools;
6
7
/**
8
 * View Generator
9
 *
10
 * Generates CodeIgniter-based views.
11
 *
12
 * @package Combustor
13
 * @author  Rougin Royce Gutib <[email protected]>
14
 */
15
class ViewGenerator extends BaseGenerator implements GeneratorInterface
16
{
17
    /**
18
     * Prepares the data before generation.
19
     *
20
     * @param  array $data
21
     * @return void
22
     */
23
    public function prepareData(array &$data)
24
    {
25
        $bootstrap = [
26
            'button' => 'btn btn-default',
27
            'buttonPrimary' => 'btn btn-primary',
28
            'formControl' => 'form-control',
29
            'formGroup' => 'form-group col-lg-12 col-md-12 col-sm-12 col-xs-12',
30
            'label' => 'control-label',
31
            'table' => 'table table table-striped table-hover',
32
            'textRight' => 'text-right'
33
        ];
34
35
        if ($data['isBootstrap']) {
36
            $data['bootstrap'] = $bootstrap;
37
        }
38
39
        $data['camel'] = [];
40
        $data['underscore'] = [];
41
        $data['foreignKeys'] = [];
42
        $data['primaryKeys'] = [];
43
44
        $data['plural'] = plural($data['name']);
45
        $data['singular'] = singular($data['name']);
46
47
        $primaryKey = 'get_' . $this->describe->getPrimaryKey($data['name']);
48
        $data['primaryKey'] = $primaryKey;
49
50
        if ($this->data['isCamel']) {
51
            $data['primaryKey'] = camelize($data['primaryKey']);
52
        }
53
54
        $data['columns'] = $this->describe->getTable(
55
            $data['name']
56
        );
57
    }
58
59
    /**
60
     * Generates set of code based on data.
61
     *
62
     * @return array
63
     */
64
    public function generate()
65
    {
66
        $this->prepareData($this->data);
67
68
        foreach ($this->data['columns'] as $column) {
69
            $field = strtolower($column->getField());
70
71
            $this->data['camel'][$field] = $this->transformField(
72
                $field,
73
                'camelize'
74
            );
75
76
            $this->data['underscore'][$field] = $this->transformField(
77
                $field,
78
                'underscore'
79
            );
80
81
            if ($column->isForeignKey()) {
82
                $referencedTable = Tools::stripTableSchema(
83
                    $column->getReferencedTable()
84
                );
85
86
                $this->data['foreignKeys'][$field] = plural($referencedTable);
87
88
                $singular = $field . '_singular';
89
90
                $this->data['foreignKeys'][$singular] = singular(
91
                    $referencedTable
92
                );
93
94
                $this->data = $this->getPrimaryKey(
95
                    $this->data,
96
                    $field,
97
                    $referencedTable
98
                );
99
            }
100
        }
101
102
        return $this->data;
103
    }
104
}
105