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\VirtualMetadata; |
10
|
|
|
use samsonphp\generator\Generator; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Virtual entity query class generator. |
14
|
|
|
* |
15
|
|
|
* @package samsoncms\api\generator |
16
|
|
|
*/ |
17
|
|
|
class VirtualQuery extends RealQuery |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Class uses generation part. |
21
|
|
|
* |
22
|
|
|
* @param VirtualMetadata $metadata Entity metadata |
23
|
|
|
*/ |
24
|
|
|
protected function createUses($metadata) |
25
|
|
|
{ |
26
|
|
|
$this->generator |
27
|
|
|
->newLine('use samsonframework\orm\QueryInterface;') |
28
|
|
|
->newLine('use samsonframework\orm\ArgumentInterface;') |
29
|
|
|
->newLine('use samson\activerecord\dbQuery;') |
30
|
|
|
->newLine(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Query constructor. |
35
|
|
|
* |
36
|
|
|
* @param Generator $generator |
37
|
|
|
* @param $metadata |
38
|
|
|
*/ |
39
|
|
|
public function __construct(Generator $generator, $metadata) |
40
|
|
|
{ |
41
|
|
|
parent::__construct($generator, $metadata); |
42
|
|
|
|
43
|
|
|
$this->parentClass = '\\' . \samsoncms\api\query\Entity::class; |
44
|
|
|
$this->entityClass = '\samsoncms\api\generated\\' . $metadata->entity; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Class static fields generation part. |
49
|
|
|
* |
50
|
|
|
* @param VirtualMetadata $metadata Entity metadata |
51
|
|
|
*/ |
52
|
|
|
protected function createStaticFields($metadata) |
53
|
|
|
{ |
54
|
|
|
$this->generator |
55
|
|
|
->commentVar('array', 'Collection of real additional field names') |
56
|
|
|
->defClassVar('$fieldRealNames', 'public static', $metadata->realNames) |
57
|
|
|
->commentVar('array', 'Collection of additional field names') |
58
|
|
|
->defClassVar('$fieldNames', 'public static', $metadata->fieldNames) |
59
|
|
|
// TODO: two above fields should be protected |
60
|
|
|
->commentVar('array', 'Collection of navigation identifiers') |
61
|
|
|
->defClassVar('$navigationIDs', 'protected static', array($metadata->entityID)) |
62
|
|
|
->commentVar('string', 'Entity full class name') |
63
|
|
|
->defClassVar('$identifier', 'protected static', $this->entityClass) |
64
|
|
|
->commentVar('array', 'Collection of localized additional fields identifiers') |
65
|
|
|
->defClassVar('$localizedFieldIDs', 'protected static', $metadata->localizedFieldIDs) |
66
|
|
|
->commentVar('array', 'Collection of NOT localized additional fields identifiers') |
67
|
|
|
->defClassVar('$notLocalizedFieldIDs', 'protected static', $metadata->notLocalizedFieldIDs) |
68
|
|
|
->commentVar('array', 'Collection of localized additional fields identifiers') |
69
|
|
|
->defClassVar('$fieldIDs', 'protected static', $metadata->fields) |
70
|
|
|
->commentVar('array', 'Collection of additional fields value column names') |
71
|
|
|
->defClassVar('$fieldValueColumns', 'protected static', $metadata->allFieldValueColumns); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Class constructor generation part. |
76
|
|
|
* |
77
|
|
|
* @param \samsoncms\api\generator\metadata\VirtualMetadata $metadata Entity metadata |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
protected function createConstructor($metadata) |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$class = "\n\t" . '/**'; |
82
|
|
|
$class .= "\n\t" . ' * @param string $locale Localization identifier'; |
83
|
|
|
$class .= "\n\t" . ' * @param QueryInterface $query Database query instance'; |
84
|
|
|
$class .= "\n\t" . ' */'; |
85
|
|
|
$class .= "\n\t" . 'public function __construct($locale = null, QueryInterface $query = null)'; |
86
|
|
|
$class .= "\n\t" . '{'; |
87
|
|
|
$class .= "\n\t\t" . 'parent::__construct(isset($query) ? $query : new dbQuery(), $locale);'; |
88
|
|
|
$class .= "\n\t" . '}'; |
89
|
|
|
|
90
|
|
|
$this->generator->text($class); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
//[PHPCOMPRESSOR(remove,end)] |
94
|
|
|
|
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.