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

BaseGenerator::getPrimaryKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 9.4285
cc 2
eloc 7
nc 2
nop 3
crap 6
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 18
    public function __construct(Describe $describe, array $data)
30
    {
31 18
        $this->describe = $describe;
32 18
        $this->data = $data;
33 18
    }
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 12
    protected function transformField($field, $type)
66
    {
67 12
        if ($type == 'camelize') {
68
            return [
69 12
                'field' => lcfirst(camelize($field)),
70 12
                'accessor' => lcfirst(camelize('set_' . $field)),
71 12
                'mutator' => lcfirst(camelize('get_' . $field))
72 12
            ];
73
        }
74
75
        return [
76 12
            'field' => lcfirst(underscore($field)),
77 12
            'accessor' => lcfirst(underscore('set_' . $field)),
78 12
            'mutator' => lcfirst(underscore('get_' . $field))
79 12
        ];
80
    }
81
}
82