1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Janisbiz\LightOrm\Dms\MySQL\Generator\Writer; |
4
|
|
|
|
5
|
|
|
use Janisbiz\LightOrm\Generator\Dms\DmsDatabaseInterface; |
6
|
|
|
use Janisbiz\LightOrm\Generator\Dms\DmsTableInterface; |
7
|
|
|
use Janisbiz\LightOrm\Generator\Writer\AbstractWriter; |
8
|
|
|
use Janisbiz\LightOrm\Dms\MySQL\Generator\Dms\DmsColumn; |
9
|
|
|
use Janisbiz\Heredoc\HeredocTrait; |
10
|
|
|
use Janisbiz\LightOrm\Generator\Writer\WriterConfigInterface; |
11
|
|
|
use Janisbiz\LightOrm\Generator\Writer\WriterInterface; |
12
|
|
|
|
13
|
|
|
class BaseEntityClassWriter extends AbstractWriter |
14
|
|
|
{ |
15
|
|
|
use HeredocTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param WriterConfigInterface $writerConfig |
19
|
|
|
*/ |
20
|
|
|
public function __construct(WriterConfigInterface $writerConfig) |
21
|
|
|
{ |
22
|
|
|
$this->writerConfig = $writerConfig; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param DmsDatabaseInterface $dmsDatabase |
27
|
|
|
* @param DmsTableInterface $dmsTable |
28
|
|
|
* @param array $existingFiles |
29
|
|
|
* |
30
|
|
|
* @return BaseEntityClassWriter |
31
|
|
|
*/ |
32
|
|
|
public function write( |
33
|
|
|
DmsDatabaseInterface $dmsDatabase, |
34
|
|
|
DmsTableInterface $dmsTable, |
35
|
|
|
array &$existingFiles |
36
|
|
|
) { |
37
|
|
|
$fileName = $this->generateFileName($dmsDatabase, $dmsTable); |
38
|
|
|
|
39
|
|
|
return $this |
40
|
|
|
->writeFile($fileName, $this->generateFileContents($dmsDatabase, $dmsTable)) |
41
|
|
|
->removeFileFromExistingFiles($fileName, $existingFiles) |
42
|
|
|
; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return WriterConfigInterface |
47
|
|
|
*/ |
48
|
|
|
protected function getWriterConfig(): WriterConfigInterface |
49
|
|
|
{ |
50
|
|
|
return $this->writerConfig; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param DmsDatabaseInterface $dmsDatabase |
55
|
|
|
* @param DmsTableInterface $dmsTable |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
protected function generateFileContents(DmsDatabaseInterface $dmsDatabase, DmsTableInterface $dmsTable): string |
60
|
|
|
{ |
61
|
|
|
$gettersAndSetters = $dmsTable->getDmsColumns(); |
62
|
|
|
$gettersAndSetters = \implode("\n\n", \array_map( |
63
|
|
|
function (DmsColumn $column) use ($dmsTable) { |
64
|
|
|
/** phpcs:disable */ |
65
|
|
|
return /** @lang PHP */ |
66
|
|
|
<<<PHP |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return {$this->heredoc($column->isNullable() || 'auto_increment' === $column->getExtra() ? 'null|' : '')}{$column->getPhpType()} |
70
|
|
|
*/ |
71
|
|
|
public function get{$column->getPhpName()}(): {$this->heredoc($column->isNullable() || 'auto_increment' === $column->getExtra() ? '?' : '')}{$column->getPhpType()} |
72
|
|
|
{ |
73
|
|
|
return \$this->data['{$column->getName()}']; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param {$this->heredoc($column->isNullable() || 'auto_increment' === $column->getExtra() ? 'null|' : '')}{$column->getPhpType()} \${$this->heredoc(\lcfirst($column->getPhpName()))} |
78
|
|
|
* |
79
|
|
|
* @return \$this |
80
|
|
|
*/ |
81
|
|
|
public function set{$column->getPhpName()}({$this->heredoc($column->isNullable() || 'auto_increment' === $column->getExtra() ? '?' : '')}{$column->getPhpType()} \${$this->heredoc(\lcfirst($column->getPhpName()))}): {$this->generateClassName($dmsTable)} |
82
|
|
|
{ |
83
|
|
|
\$this->data['{$column->getName()}'] = \${$this->heredoc(\lcfirst($column->getPhpName()))}; |
84
|
|
|
|
85
|
|
|
return \$this; |
86
|
|
|
} |
87
|
|
|
PHP; |
88
|
|
|
/** phpcs:enable */ |
89
|
|
|
}, |
90
|
|
|
$gettersAndSetters |
91
|
|
|
)); |
92
|
|
|
|
93
|
|
|
$columnsConstants = $dmsTable->getDmsColumns(); |
94
|
|
|
$columnsConstants = \implode( |
95
|
|
|
"\n ", |
96
|
|
|
\array_map( |
97
|
|
|
function (DmsColumn $column) { |
98
|
|
|
return \sprintf( |
99
|
|
|
'const COLUMN_%s = \'%s\';', |
100
|
|
|
\mb_strtoupper($column->getName()), |
101
|
|
|
$column->getName() |
102
|
|
|
); |
103
|
|
|
}, |
104
|
|
|
$columnsConstants |
105
|
|
|
) |
106
|
|
|
); |
107
|
|
|
|
108
|
|
|
$primaryKeys = $dmsTable->getDmsColumns(); |
109
|
|
|
$primaryKeys = \implode( |
110
|
|
|
"\n ", |
111
|
|
|
\array_filter(\array_map( |
112
|
|
|
function (DmsColumn $column) { |
113
|
|
|
if ($column->getKey() !== 'PRI') { |
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return \sprintf('static::COLUMN_%s,', \mb_strtoupper($column->getName())); |
118
|
|
|
}, |
119
|
|
|
$primaryKeys |
120
|
|
|
)) |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$primaryKeysAutoIncrement = $dmsTable->getDmsColumns(); |
124
|
|
|
$primaryKeysAutoIncrement = \implode( |
125
|
|
|
"\n ", |
126
|
|
|
\array_filter(\array_map( |
127
|
|
|
function (DmsColumn $column) { |
128
|
|
|
if ($column->getExtra() !== 'auto_increment') { |
129
|
|
|
return null; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return \sprintf('static::COLUMN_%s,', \mb_strtoupper($column->getName())); |
133
|
|
|
}, |
134
|
|
|
$primaryKeysAutoIncrement |
135
|
|
|
)) |
136
|
|
|
); |
137
|
|
|
|
138
|
|
|
$columns = $dmsTable->getDmsColumns(); |
139
|
|
|
$columns = \implode( |
140
|
|
|
"\n ", |
141
|
|
|
\array_map( |
142
|
|
|
function (DmsColumn $column) { |
143
|
|
|
return \sprintf('static::COLUMN_%s,', \mb_strtoupper($column->getName())); |
144
|
|
|
}, |
145
|
|
|
$columns |
146
|
|
|
) |
147
|
|
|
); |
148
|
|
|
|
149
|
|
|
/** phpcs:disable */ |
150
|
|
|
return /** @lang PHP */ |
151
|
|
|
<<<PHP |
152
|
|
|
<?php declare(strict_types=1); |
153
|
|
|
|
154
|
|
|
namespace {$this->generateNamespace($dmsDatabase)}; |
155
|
|
|
|
156
|
|
|
use Janisbiz\LightOrm\Entity\BaseEntity; |
157
|
|
|
|
158
|
|
|
class {$this->generateClassName($dmsTable)} extends BaseEntity |
159
|
|
|
{ |
160
|
|
|
const {$this->heredoc(WriterInterface::CLASS_CONSTANT_DATABASE_NAME)} = '{$dmsDatabase->getName()}'; |
161
|
|
|
const {$this->heredoc(WriterInterface::CLASS_CONSTANT_TABLE_NAME)} = '{$dmsDatabase->getName()}.{$dmsTable->getName()}'; |
162
|
|
|
|
163
|
|
|
{$columnsConstants} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param bool \$isNew |
167
|
|
|
*/ |
168
|
|
|
public function __construct(\$isNew = true) |
169
|
|
|
{ |
170
|
|
|
\$this->primaryKeys = [ |
171
|
|
|
{$primaryKeys} |
172
|
|
|
]; |
173
|
|
|
\$this->primaryKeysAutoIncrement = [ |
174
|
|
|
{$primaryKeysAutoIncrement} |
175
|
|
|
]; |
176
|
|
|
\$this->columns = [ |
177
|
|
|
{$columns} |
178
|
|
|
]; |
179
|
|
|
|
180
|
|
|
\$this->isNew = \$isNew; |
181
|
|
|
if (empty(\$this->data)) { |
182
|
|
|
\$this->isNew = true; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
{$gettersAndSetters} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
PHP; |
189
|
|
|
/** phpcs:enable */ |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|