Completed
Push — master ( ea1059...a4c072 )
by Rougin
04:55
created

BaseGenerator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
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 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 6
        }
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 15
            ];
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 15
        ];
80
    }
81
}
82