Completed
Push — master ( b13412...8f2212 )
by Vitaly
04:23
created

Entity::createFields()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
//[PHPCOMPRESSOR(remove,start)]
3
/**
4
 * Created by Vitaly Iegorov <[email protected]>.
5
 * on 22.03.16 at 15:46
6
 */
7
namespace samsoncms\api\generator;
8
9
use samsoncms\api\generator\metadata\GenericMetadata;
10
use samsoncms\api\generator\metadata\Virtual;
11
12
/**
13
 * Entity class generator.
14
 *
15
 * @package samsoncms\api\generator
16
 */
17
class Entity extends Generic
18
{
19
    /**
20
     * Class definition generation part.
21
     *
22
     * @param Virtual $metadata Entity metadata
23
     */
24
    protected function createDefinition($metadata)
25
    {
26
        /**
27
         * TODO: Parent problem
28
         * Should be changed to merging fields instead of extending with OOP for structure_relation support
29
         * or creating traits and using them on shared parent entities.
30
         */
31
        $parentClass = null !== $metadata->parent
32
            ? $metadata->parent->entityClassName
33
            : '\\'.\samsoncms\api\Entity::class;
34
35
        $this->generator
36
            ->multiComment(array('"' . $metadata->entityRealName . '" database entity class'))
37
            ->defClass($metadata->entity, $parentClass);
38
    }
39
40
    /**
41
     * Class constants generation part.
42
     *
43
     * @param Virtual $metadata Entity metadata
44
     */
45
    protected function createConstants($metadata)
46
    {
47
        $this->generator
48
            ->commentVar('string', 'Entity full class name, use ::class instead')
49
            ->defClassConst('ENTITY', $metadata->entityClassName)
50
            ->commentVar('string', 'Entity manager full class name')
51
            ->defClassConst('MANAGER', $metadata->entityClassName . 'Query')
52
            ->commentVar('string', 'Entity database identifier')
53
            ->defClassConst('IDENTIFIER', $metadata->entityID)
54
            ->commentVar('string', 'Not transliterated entity name')
55
            ->defClassConst('NAME', $metadata->entityRealName);
56
57
        // Create all entity fields constants storing each additional field metadata
58
        foreach ($metadata->allFieldIDs as $fieldID => $fieldName) {
59
            $this->generator
60
                ->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' variable name')
61
                ->defClassConst('F_' . $fieldName, $fieldName)
62
                ->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' additional field identifier')
63
                ->defClassConst('F_' . $fieldName . '_ID', $fieldID);
64
        }
65
    }
66
67
    /**
68
     * Class fields generation part.
69
     *
70
     * @param Virtual $metadata Entity metadata
71
     */
72
    protected function createFields($metadata)
73
    {
74
        foreach ($metadata->allFieldIDs as $fieldID => $fieldName) {
75
            $this->generator
76
                ->commentVar($metadata->allFieldTypes[$fieldID], $metadata->fieldDescriptions[$fieldID])
77
                ->defClassVar('$' . $fieldName, 'public');
78
        }
79
    }
80
81
    /**
82
     * Class methods generation part.
83
     *
84
     * @param Virtual $metadata Entity metadata
85
     */
86
    protected function createMethods($metadata)
87
    {
88
        $methods = [];
89
        /** @var Virtual $metadataInstance Iterate all metadata entities */
90
        foreach (GenericMetadata::$instances as $metadataInstance) {
91
            if ($metadataInstance->type === Virtual::TYPE_TABLE) {
92
                // Create table virtual entity with correct name ending
93
                $tableEntity = rtrim($metadataInstance->entity, 'Table') . 'Table';
94
95
                $code = "\n\t" . '/**';
96
                $code .= "\n\t" . ' * Create virtual ' . $metadataInstance->entityRealName . ' table instance.';
97
                $code .= "\n\t" . ' * @param ViewInterface $renderer Renderer instance';
98
                $code .= "\n\t" . ' *';
99
                $code .= "\n\t" . ' * @return $this Chaining';
100
                $code .= "\n\t" . ' */';
101
                $code .= "\n\t" . 'public function ' . $tableEntity . '(ViewInterface $renderer)';
102
                $code .= "\n\t" . '{';
103
                $code .= "\n\t\t" . 'return new ' . $tableEntity . '($renderer);';
104
                $code .= "\n\t" . '}';
105
106
                $methods[] = $code;
107
            }
108
        }
109
110
        // Add method text to generator
111
        $this->generator->text(implode("\n", $methods));
112
    }
113
114
    /**
115
     * Class static fields generation part.
116
     *
117
     * @param Virtual $metadata Entity metadata
118
     */
119
    protected function createStaticFields($metadata)
120
    {
121
        return $this->generator
122
            ->commentVar('array', 'Collection of navigation identifiers')
123
            ->defClassVar('$navigationIDs', 'protected static', array($metadata->entityID))
124
            ->commentVar('array', '@deprecated Old ActiveRecord data')
125
            ->defClassVar('$_sql_select', 'public static ', $metadata->arSelect)
126
            ->commentVar('array', '@deprecated Old ActiveRecord data')
127
            ->defClassVar('$_attributes', 'public static ', $metadata->arAttributes)
128
            ->commentVar('array', '@deprecated Old ActiveRecord data')
129
            ->defClassVar('$_map', 'public static ', $metadata->arMap)
130
            ->commentVar('array', '@deprecated Old ActiveRecord data')
131
            ->defClassVar('$_sql_from', 'public static ', $metadata->arFrom)
132
            ->commentVar('array', '@deprecated Old ActiveRecord data')
133
            ->defClassVar('$_own_group', 'public static ', $metadata->arGroup)
134
            ->commentVar('array', '@deprecated Old ActiveRecord data')
135
            ->defClassVar('$_relation_alias', 'public static ', $metadata->arRelationAlias)
136
            ->commentVar('array', '@deprecated Old ActiveRecord data')
137
            ->defClassVar('$_relation_type', 'public static ', $metadata->arRelationType)
138
            ->commentVar('array', '@deprecated Old ActiveRecord data')
139
            ->defClassVar('$_relations', 'public static ', $metadata->arRelations)
140
            ->commentVar('array', '@deprecated Old ActiveRecord data')
141
            ->defClassVar('$fieldIDs', 'protected static ', $metadata->allFieldIDs)
142
            ->commentVar('array', '@deprecated Old ActiveRecord data')
143
            ->defClassVar('$fieldValueColumns', 'protected static ', $metadata->allFieldValueColumns);
144
    }
145
}
146
//[PHPCOMPRESSOR(remove,end)]