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
|
|
|
/** |
10
|
|
|
* Entity class generator. |
11
|
|
|
* |
12
|
|
|
* @package samsoncms\api\generator |
13
|
|
|
*/ |
14
|
|
|
class Entity extends OOP |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Class definition generation part. |
18
|
|
|
* |
19
|
|
|
* @param Metadata $metadata Entity metadata |
20
|
|
|
*/ |
21
|
|
|
protected function createDefinition(Metadata $metadata) |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* TODO: Parent problem |
25
|
|
|
* Should be changed to merging fields instead of extending with OOP for structure_relation support |
26
|
|
|
* or creating traits and using them on shared parent entities. |
27
|
|
|
*/ |
28
|
|
|
$parentClass = null !== $metadata->parent |
29
|
|
|
? $metadata->parent->entityClassName |
30
|
|
|
: '\\'.\samsoncms\api\Entity::class; |
31
|
|
|
|
32
|
|
|
$this->generator |
33
|
|
|
->multiComment(array('"' . $metadata->entityRealName . '" database entity class')) |
34
|
|
|
->defClass($metadata->entity, $parentClass); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Class constants generation part. |
39
|
|
|
* |
40
|
|
|
* @param Metadata $metadata Entity metadata |
41
|
|
|
*/ |
42
|
|
|
protected function createConstants(Metadata $metadata) |
43
|
|
|
{ |
44
|
|
|
$this->generator |
45
|
|
|
->commentVar('string', 'Entity full class name, use ::class instead') |
46
|
|
|
->defClassConst('ENTITY', $metadata->entityClassName) |
47
|
|
|
->commentVar('string', 'Entity manager full class name') |
48
|
|
|
->defClassConst('MANAGER', $metadata->entityClassName . 'Query') |
49
|
|
|
->commentVar('string', 'Entity database identifier') |
50
|
|
|
->defClassConst('IDENTIFIER', $metadata->entityID) |
51
|
|
|
->commentVar('string', 'Not transliterated entity name') |
52
|
|
|
->defClassConst('NAME', $metadata->entityRealName); |
53
|
|
|
|
54
|
|
|
// Create all entity fields constants storing each additional field metadata |
55
|
|
|
foreach ($metadata->allFieldIDs as $fieldID => $fieldName) { |
56
|
|
|
$this->generator |
57
|
|
|
->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' variable name') |
58
|
|
|
->defClassConst('F_' . $fieldName, $fieldName) |
59
|
|
|
->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' additional field identifier') |
60
|
|
|
->defClassConst('F_' . $fieldName . '_ID', $fieldID); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Class fields generation part. |
66
|
|
|
* |
67
|
|
|
* @param Metadata $metadata Entity metadata |
68
|
|
|
*/ |
69
|
|
|
protected function createFields(Metadata $metadata) |
70
|
|
|
{ |
71
|
|
|
foreach ($metadata->allFieldIDs as $fieldID => $fieldName) { |
72
|
|
|
$this->generator |
73
|
|
|
->commentVar($metadata->allFieldTypes[$fieldID], $metadata->fieldDescriptions[$fieldID]) |
74
|
|
|
->defClassVar('$' . $fieldName, 'public'); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Class static fields generation part. |
80
|
|
|
* |
81
|
|
|
* @param Metadata $metadata Entity metadata |
82
|
|
|
*/ |
83
|
|
|
protected function createStaticFields(Metadata $metadata) |
84
|
|
|
{ |
85
|
|
|
return $this->generator |
86
|
|
|
->commentVar('array', 'Collection of navigation identifiers') |
87
|
|
|
->defClassVar('$navigationIDs', 'protected static', array($metadata->entityID)) |
88
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
89
|
|
|
->defClassVar('$_sql_select', 'public static ', $metadata->arSelect) |
90
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
91
|
|
|
->defClassVar('$_attributes', 'public static ', $metadata->arAttributes) |
92
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
93
|
|
|
->defClassVar('$_map', 'public static ', $metadata->arMap) |
94
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
95
|
|
|
->defClassVar('$_sql_from', 'public static ', $metadata->arFrom) |
96
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
97
|
|
|
->defClassVar('$_own_group', 'public static ', $metadata->arGroup) |
98
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
99
|
|
|
->defClassVar('$_relation_alias', 'public static ', $metadata->arRelationAlias) |
100
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
101
|
|
|
->defClassVar('$_relation_type', 'public static ', $metadata->arRelationType) |
102
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
103
|
|
|
->defClassVar('$_relations', 'public static ', $metadata->arRelations) |
104
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
105
|
|
|
->defClassVar('$fieldIDs', 'protected static ', $metadata->allFieldIDs) |
106
|
|
|
->commentVar('array', '@deprecated Old ActiveRecord data') |
107
|
|
|
->defClassVar('$fieldValueColumns', 'protected static ', $metadata->allFieldValueColumns); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
//[PHPCOMPRESSOR(remove,end)] |