BaseGenerator::getPrimaryKey()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 3
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 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 24
    public function __construct(Describe $describe, array $data)
30
    {
31 24
        $this->describe = $describe;
32 24
        $this->data = $data;
33 24
    }
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 15
    protected function getPrimaryKey(array $data, $field, $referencedTable)
44
    {
45 15
        $accessor = 'get_' . $this->describe->getPrimaryKey($referencedTable);
46
47 15
        $data['primaryKeys'][$field] = $accessor;
48
49 15
        if ($data['isCamel']) {
50 6
            $camelized = camelize($data['primaryKeys'][$field]);
51
52 6
            $data['primaryKeys'][$field] = $camelized;
53 4
        }
54
55 15
        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 15
    protected function transformField($field, $type)
66
    {
67 15
        if ($type == 'camelize') {
68
            return [
69 15
                'field' => lcfirst(camelize($field)),
70 15
                'accessor' => lcfirst(camelize('get_' . $field)),
71 15
                'mutator' => lcfirst(camelize('set_' . $field))
72 10
            ];
73
        }
74
75
        return [
76 15
            'field' => lcfirst(underscore($field)),
77 15
            'accessor' => lcfirst(underscore('get_' . $field)),
78 15
            'mutator' => lcfirst(underscore('set_' . $field))
79 10
        ];
80
    }
81
}
82