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

BaseGenerator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 66
ccs 0
cts 28
cp 0
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getPrimaryKey() 0 13 2
A transformField() 0 14 2
1
<?php
2
3
namespace Rougin\Combustor\Generator;
4
5
use Rougin\Describe\Describe;
6
7
/**
8
 * Base Generator
9
 *
10
 * @package Combustor
11
 * @author  Rougin Royce Gutib <[email protected]>
12
 */
13
class BaseGenerator
14
{
15
    /**
16
     * @var \Rougin\Describe\Describe
17
     */
18
    protected $describe;
19
20
    /**
21
     * @var array
22
     */
23
    protected $data = [];
24
25
    /**
26
     * @param \Rougin\Describe\Describe $describe
27
     * @param array $data
28
     */
29
    public function __construct(Describe $describe, array $data)
30
    {
31
        $this->describe = $describe;
32
        $this->data = $data;
33
    }
34
35
    /**
36
     * Gets the primary keys based on specified field.
37
     *
38
     * @param  array  $data
39
     * @param  string $field
40
     * @param  string $referencedTable
41
     * @return array
42
     */
43
    protected function getPrimaryKey(array $data, $field, $referencedTable)
44
    {
45
        $accessor = 'get_' . $this->describe->getPrimaryKey($referencedTable);
46
47
        $data['primaryKeys'][$field] = $accessor;
48
49
        if ($data['isCamel']) {
50
            $camelized = camelize($data['primaryKeys'][$field]);
51
52
            $data['primaryKeys'][$field] = $camelized;
53
        }
54
55
        return $data;
56
    }
57
58
    /**
59
     * Transforms the field into the template.
60
     *
61
     * @param  string $field
62
     * @param  string $type
63
     * @return array
64
     */
65
    protected function transformField($field, $type)
66
    {
67
        if ($type == 'camelize') {
68
            return [
69
                'field' => lcfirst(camelize($field)),
70
                'accessor' => lcfirst(camelize('get_' . $field)),
71
                'mutator' => lcfirst(camelize('set_' . $field))
72
            ];
73
        }
74
75
        return [
76
            'field' => lcfirst(underscore($field)),
77
            'accessor' => lcfirst(underscore('get_' . $field)),
78
            'mutator' => lcfirst(underscore('set_' . $field))
79
        ];
80
    }
81
}
82