Completed
Push — master ( 607296...35be3a )
by Vitaly
05:35
created

Table::createStaticFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
//[PHPCOMPRESSOR(remove,start)]
3
/**
4
 * Created by PhpStorm.
5
 * User: nazarenko
6
 * Date: 29.03.2016
7
 * Time: 11:11
8
 */
9
namespace samsoncms\api\generator;
10
11
use samsoncms\api\generator\metadata\Virtual;
12
use samsonphp\generator\Generator;
13
14
/**
15
 * Table class generator.
16
 *
17
 * @package samsoncms\api\generator
18
 */
19
class Table extends Generic
20
{
21
    /**
22
     * Query constructor.
23
     *
24
     * @param Generator $generator
25
     * @param           $metadata
26
     */
27
    public function __construct(Generator $generator, $metadata)
28
    {
29
        parent::__construct($generator, $metadata);
30
31
        $this->className = rtrim($this->className, 'Table').'Table';
32
    }
33
34
    /**
35
     * Class uses generation part.
36
     *
37
     * @param \samsoncms\api\generator\metadata\Gallery $metadata Entity metadata
38
     */
39
    protected function createUses($metadata)
40
    {
41
        $this->generator
42
            ->newLine('use samsonframework\core\ViewInterface;')
43
            ->newLine('use samsonframework\orm\QueryInterface;')
44
            ->newLine('use samson\activerecord\dbQuery;')
45
            ->newLine();
46
    }
47
48
    /**
49
     * Class definition generation part.
50
     *
51
     * @param Virtual $metadata Entity metadata
52
     */
53
    protected function createDefinition($metadata)
54
    {
55
        $this->generator
56
            ->multiComment(array(
57
                'Class for rendering "' . $metadata->entityRealName . '" table',
58
            ))
59
            ->defClass($this->className, '\\'.\samsoncms\api\field\Table::class)
60
            ->newLine('use \\'.\samsoncms\api\Renderable::class.';')
61
            ->newLine();
62
    }
63
64
    /**
65
     * Class constants generation part.
66
     *
67
     * @param Virtual $metadata Entity metadata
68
     */
69
    protected function createConstants($metadata)
70
    {
71
        $this->generator
72
            ->commentVar('string', 'Entity full class name, use ::class instead')
73
            ->defClassConst('ENTITY', $metadata->entityClassName)
74
            ->commentVar('string', 'Entity database identifier')
75
            ->defClassConst('IDENTIFIER', $metadata->entityID)
76
            ->commentVar('string', 'Not transliterated entity name')
77
            ->defClassConst('NAME', $metadata->entityRealName);
78
    }
79
80
    /**
81
     * Class static fields generation part.
82
     *
83
     * @param Virtual $metadata Entity metadata
84
     */
85
    protected function createStaticFields($metadata)
86
    {
87
        $this->generator
88
            ->commentVar('array', 'Collection of real additional field names')
89
            ->defClassVar('$fieldsRealNames', 'public static', $metadata->realNames);
90
    }
91
92
    /**
93
     * Class methods generation part.
94
     *
95
     * @param Virtual $metadata Entity metadata
96
     */
97 View Code Duplication
    protected function createMethods($metadata)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        $methods = [];
100
        // TODO: Add different method generation depending on their field type
101
        // Generate Query::where() analog for specific field.
102
        foreach ($metadata->allFieldIDs as $fieldID => $fieldName) {
103
            $code = "\n\t" . '/**';
104
            $code .= "\n\t" . ' * Get collection of ' . $fieldName . '(#' . $fieldID . ') table column values.';
105
            $code .= "\n\t" . ' * @see \samsoncms\api\field\Table::values($fieldID)';
106
            $code .= "\n\t" . ' * @param string $relation Field to value condition relation';
107
            $code .= "\n\t" . ' *';
108
            $code .= "\n\t" . ' * @return ' . $metadata->allFieldTypes[$fieldID] . '[] ' . $fieldName . ' values collection';
109
            $code .= "\n\t" . ' */';
110
            $code .= "\n\t" . 'public function ' . $fieldName . '()';
111
            $code .= "\n\t" . '{';
112
            $code .= "\n\t\t" . 'return $this->values(' . $fieldID . ');';
113
            $code .= "\n\t" . '}';
114
115
            $methods[] = $code;
116
        }
117
118
        // Add method text to generator
119
        $this->generator->text(implode("\n", $methods));
120
    }
121
122
    /**
123
     * Class constructor generation part.
124
     *
125
     * @param Virtual $metadata Entity metadata
126
     */
127
    protected function createConstructor($metadata)
128
    {
129
        $class = "\n\t" . '/**';
130
        $class .= "\n\t" . ' * @param ViewInterface $renderer Rendering instance';
131
        $class .= "\n\t" . ' * @param int $materialID material identifier';
132
        $class .= "\n\t" . ' * @param QueryInterface $query Database query instance';
133
        $class .= "\n\t" . ' * @param string $locale locale';
134
        $class .= "\n\t" . ' */';
135
        $class .= "\n\t" . 'public function __construct(ViewInterface $renderer, $materialID, QueryInterface $query = null, $locale = null)';
136
        $class .= "\n\t" . '{';
137
        $class .= "\n\t\t" . '$this->renderer = $renderer;';
138
        $class .= "\n\t\t" . 'parent::__construct(null !== $query ? $query : new dbQuery(), array('. $metadata->entityID .'), $materialID, $locale);';
139
        $class .= "\n\t" . '}' . "\n";
140
141
        $this->generator->text($class);
142
    }
143
}
144
//[PHPCOMPRESSOR(remove,end)]
145