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\Virtual; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Virtual entity class generator. |
13
|
|
|
* |
14
|
|
|
* @package samsoncms\api\generator |
15
|
|
|
*/ |
16
|
|
|
class VirtualEntity extends Generic |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Class definition generation part. |
20
|
|
|
* |
21
|
|
|
* @param Virtual $metadata Entity metadata |
22
|
|
|
*/ |
23
|
|
|
protected function createDefinition($metadata) |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* TODO: Parent problem |
27
|
|
|
* Should be changed to merging fields instead of extending with OOP for structure_relation support |
28
|
|
|
* or creating traits and using them on shared parent entities. |
29
|
|
|
*/ |
30
|
|
|
$parentClass = null !== $metadata->parent |
31
|
|
|
? $metadata->parent->entityClassName |
32
|
|
|
: '\\'.\samsoncms\api\Entity::class; |
33
|
|
|
|
34
|
|
|
$this->generator |
35
|
|
|
->multiComment(array('"' . $metadata->entityRealName . '" database entity class')) |
36
|
|
|
->defClass($this->className, $parentClass) |
37
|
|
|
->newLine('use TableTrait;') |
38
|
|
|
->newLine();; |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Class constants generation part. |
43
|
|
|
* |
44
|
|
|
* @param Virtual $metadata Entity metadata |
45
|
|
|
*/ |
46
|
|
|
protected function createConstants($metadata) |
47
|
|
|
{ |
48
|
|
|
$this->generator |
49
|
|
|
->commentVar('string', 'Entity full class name, use ::class instead') |
50
|
|
|
->defClassConst('ENTITY', $metadata->entityClassName) |
51
|
|
|
->commentVar('string', 'Entity manager full class name') |
52
|
|
|
->defClassConst('MANAGER', $metadata->entityClassName . 'Query') |
53
|
|
|
->commentVar('string', 'Entity database identifier') |
54
|
|
|
->defClassConst('IDENTIFIER', $metadata->entityID) |
55
|
|
|
->commentVar('string', 'Not transliterated entity name') |
56
|
|
|
->defClassConst('NAME', $metadata->entityRealName); |
57
|
|
|
|
58
|
|
|
// Create all entity fields constants storing each additional field metadata |
59
|
|
|
foreach ($metadata->fields as $fieldID => $fieldName) { |
60
|
|
|
$this->generator |
61
|
|
|
->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' variable name') |
62
|
|
|
->defClassConst('F_' . $fieldName, $fieldName) |
63
|
|
|
->commentVar('string', $metadata->fieldDescriptions[$fieldID] . ' additional field identifier') |
64
|
|
|
->defClassConst('F_' . $fieldName . '_ID', $fieldID); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Class fields generation part. |
70
|
|
|
* |
71
|
|
|
* @param Virtual $metadata Entity metadata |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
protected function createFields($metadata) |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
foreach ($metadata->fields as $fieldID => $fieldName) { |
76
|
|
|
$this->generator |
77
|
|
|
->commentVar($metadata->types[$fieldID], $metadata->fieldDescriptions[$fieldID]) |
78
|
|
|
->defClassVar('$' . $fieldName, 'public'); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
//[PHPCOMPRESSOR(remove,end)] |
Let’s take a look at an example: