Completed
Push — master ( f3af33...267f53 )
by Rougin
03:52
created

ViewGenerator::prepareData()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 50
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 5.2526

Importance

Changes 7
Bugs 1 Features 0
Metric Value
c 7
b 1
f 0
dl 0
loc 50
ccs 29
cts 37
cp 0.7838
rs 8.6315
cc 5
eloc 29
nc 16
nop 1
crap 5.2526
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 6
    public function prepareData(array &$data)
24
    {
25
        $bootstrap = [
26 6
            'button' => 'btn btn-default',
27 6
            'buttonPrimary' => 'btn btn-primary',
28 6
            'formControl' => 'form-control',
29 6
            'formGroup' => 'form-group col-lg-12 col-md-12 col-sm-12 col-xs-12',
30 6
            'label' => 'control-label',
31 6
            'table' => 'table table table-striped table-hover',
32
            'textRight' => 'text-right'
33 6
        ];
34
35 6
        if ($data['isBootstrap']) {
36 6
            $data['bootstrap'] = $bootstrap;
37 6
        }
38
39 6
        $data['camel'] = [];
40 6
        $data['underscore'] = [];
41 6
        $data['foreignKeys'] = [];
42 6
        $data['primaryKeys'] = [];
43
44 6
        $data['plural'] = plural($data['name']);
45 6
        $data['singular'] = singular($data['name']);
46
47 6
        $data['primaryKey'] = 'get_' . $this->describe->getPrimaryKey(
48 6
            $data['name']
49 6
        );
50
51
        // Workaround...
52 6
        if ($data['primaryKey'] == 'get_') {
53
            $data['primaryKey'] = 'get_' . $this->describe->getPrimaryKey(
54
                singular($data['name'])
55
            );
56
        }
57
58 6
        if ($this->data['isCamel']) {
59 3
            $data['primaryKey'] = camelize($data['primaryKey']);
60 3
        }
61
62 6
        $data['columns'] = $this->describe->getTable(
63 6
            $data['name']
64 6
        );
65
66
        // Workaround...
67 6
        if (empty($data['columns'])) {
68
            $data['columns'] = $this->describe->getTable(
69
                singular($data['name'])
70
            );
71
        }
72 6
    }
73
74
    /**
75
     * Generates set of code based on data.
76
     * 
77
     * @return array
78
     */
79 6
    public function generate()
80
    {
81 6
        $this->prepareData($this->data);
82
83 6
        foreach ($this->data['columns'] as $column) {
84 6
            $field = strtolower($column->getField());
85
86 6
            $this->data['camel'][$field] = $this->transformField(
87 6
                $field,
88
                'camelize'
89 6
            );
90
91 6
            $this->data['underscore'][$field] = $this->transformField(
92 6
                $field,
93
                'underscore'
94 6
            );
95
96 6
            if ($column->isForeignKey()) {
97
                $referencedTable = Tools::stripTableSchema(
98
                    $column->getReferencedTable()
99
                );
100
101
                $this->data['foreignKeys'][$field] = plural($referencedTable);
102
103
                $singular = $field . '_singular';
104
105
                $this->data['foreignKeys'][$singular] = singular(
106
                    $referencedTable
107
                );
108
109
                $this->data = $this->getPrimaryKey(
110
                    $this->data,
111
                    $field,
112
                    $referencedTable
113
                );
114
            }
115 6
        }
116
117 6
        return $this->data;
118
    }
119
}
120