|
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\RealMetadata; |
|
10
|
|
|
use samsonphp\generator\Generator; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Real entity query class generator. |
|
14
|
|
|
* |
|
15
|
|
|
* @package samsoncms\api\generator |
|
16
|
|
|
*/ |
|
17
|
|
|
class RealQuery extends Generic |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var string Query returned entity class name */ |
|
20
|
|
|
protected $entityClass; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Query constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param Generator $generator |
|
26
|
|
|
* @param $metadata |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(Generator $generator, $metadata) |
|
29
|
|
|
{ |
|
30
|
|
|
parent::__construct($generator, $metadata); |
|
31
|
|
|
|
|
32
|
|
|
$this->className = $metadata->entity.'Query'; |
|
33
|
|
|
$this->parentClass = '\\' . \samsoncms\api\query\Record::class; |
|
34
|
|
|
$this->entityClass = '\samsoncms\api\generated\\' . $metadata->entity; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Class uses generation part. |
|
39
|
|
|
* |
|
40
|
|
|
* @param RealMetadata $metadata Entity metadata |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function createUses($metadata) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->generator |
|
45
|
|
|
->newLine('use samsonframework\orm\ArgumentInterface;') |
|
46
|
|
|
->newLine(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Class definition generation part. |
|
51
|
|
|
* |
|
52
|
|
|
* @param RealMetadata $metadata Entity metadata |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function createDefinition($metadata) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->generator |
|
57
|
|
|
->multiComment(array( |
|
58
|
|
|
'Class for querying and fetching "' . $metadata->entity . '" instances from database', |
|
59
|
|
|
'@method ' . $this->entityClass . ' first();', |
|
60
|
|
|
'@method ' . $this->entityClass . '[] find();', |
|
61
|
|
|
)) |
|
62
|
|
|
->defClass($this->className, $this->parentClass); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Class static fields generation part. |
|
67
|
|
|
* |
|
68
|
|
|
* @param RealMetadata $metadata Entity metadata |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function createStaticFields($metadata) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->generator |
|
73
|
|
|
->commentVar('string', 'Entity table primary field name') |
|
74
|
|
|
->defClassVar('$primaryFieldName', 'public static', $metadata->primaryField) |
|
75
|
|
|
->commentVar('string', 'Entity full class name') |
|
76
|
|
|
->defClassVar('$identifier', 'public static', $this->entityClass) |
|
77
|
|
|
->commentVar('array', 'Collection of localized additional fields identifiers') |
|
78
|
|
|
->defClassVar('$fieldIDs', 'public static', $metadata->fields) |
|
79
|
|
|
|
|
80
|
|
|
->commentVar('array', 'Collection of data types') |
|
81
|
|
|
->defClassVar('$fieldDataTypes', 'public static', $metadata->internalTypes) |
|
82
|
|
|
->commentVar('string', 'Table name') |
|
83
|
|
|
->defClassVar('$tableName', 'public static', $metadata->entityName) |
|
84
|
|
|
->commentVar('array', 'Collection of default') |
|
85
|
|
|
->defClassVar('$fieldDefaults', 'public static', $metadata->defaults) |
|
86
|
|
|
|
|
87
|
|
|
->commentVar('array', 'Collection of nullable fields') |
|
88
|
|
|
->defClassVar('$fieldNullable', 'public static', $metadata->nullable) |
|
89
|
|
|
|
|
90
|
|
|
->commentVar('array', 'Collection of additional field names') |
|
91
|
|
|
->defClassVar('$fieldNames', 'public static', $metadata->fieldNames); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Class methods generation part. |
|
96
|
|
|
* |
|
97
|
|
|
* @param RealMetadata $metadata Entity metadata |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function createMethods($metadata) |
|
100
|
|
|
{ |
|
101
|
|
|
$methods = []; |
|
102
|
|
|
// TODO: Add different method generation depending on their field type |
|
103
|
|
|
// Generate Query::where() analog for specific field. |
|
104
|
|
|
foreach ($metadata->fields as $fieldID => $fieldName) { |
|
105
|
|
|
$code = "\n\t" . '/**'; |
|
106
|
|
|
$code .= "\n\t" . ' * Add ' . $fieldName . '(#' . $fieldID . ') field query condition.'; |
|
107
|
|
|
$code .= "\n\t" . ' * @see Generic::where()'; |
|
108
|
|
|
$code .= "\n\t" . ' * @param ' . $metadata->types[$fieldID] . ' $value Field value'; |
|
109
|
|
|
$code .= "\n\t" . ' * @param string $relation Field to value condition relation'; |
|
110
|
|
|
$code .= "\n\t" . ' *'; |
|
111
|
|
|
$code .= "\n\t" . ' * @return $this Chaining'; |
|
112
|
|
|
$code .= "\n\t" . ' */'; |
|
113
|
|
|
$code .= "\n\t" . 'public function ' . $fieldName . '($value, $relation = ArgumentInterface::EQUAL)'; |
|
114
|
|
|
$code .= "\n\t" . '{'; |
|
115
|
|
|
$code .= "\n\t\t" . 'return $this->where(' . $this->entityClass.'::F_'.strtoupper($fieldName). ', $value, $relation);'; |
|
116
|
|
|
|
|
117
|
|
|
$code .= "\n\t" . '}'; |
|
118
|
|
|
|
|
119
|
|
|
$methods[] = $code; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
// Add method text to generator |
|
123
|
|
|
$this->generator->text(implode("\n", $methods)); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
//[PHPCOMPRESSOR(remove,end)] |
|
127
|
|
|
|