Skip to content

Commit c92a5bd

Browse files
authored
Add uuid, json data types and index/foreign modifiers (#76)
1 parent b221989 commit c92a5bd

File tree

4 files changed

+41
-23
lines changed

4 files changed

+41
-23
lines changed

src/Generators/FactoryGenerator.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ public function output(array $tree): array
2727
/** @var \Blueprint\Models\Model $model */
2828
foreach ($tree['models'] as $model) {
2929
$path = $this->getPath($model);
30-
$this->files->put(
31-
$path,
32-
$this->populateStub($stub, $model)
33-
);
30+
$this->files->put($path, $this->populateStub($stub, $model));
3431

3532
$output['created'][] = $path;
3633
}
@@ -67,12 +64,12 @@ protected function buildDefinition(Model $model)
6764
continue;
6865
}
6966

70-
if ($column->dataType() === 'id') {
67+
if (in_array($column->dataType(), ['id', 'uuid'])) {
7168
$name = Str::beforeLast($column->name(), '_id');
7269
$class = Str::studly($column->attributes()[0] ?? $name);
7370

7471
$definition .= self::INDENT . "'{$column->name()}' => ";
75-
$definition .= sprintf("factory(%s::class)", '\\' . $model->fullyQualifiedNamespace() . '\\' . $class);
72+
$definition .= sprintf('factory(%s::class)', '\\' . $model->fullyQualifiedNamespace() . '\\' . $class);
7673
$definition .= ',' . PHP_EOL;
7774
} elseif (in_array($column->dataType(), ['enum', 'set']) and !empty($column->attributes())) {
7875
$definition .= self::INDENT . "'{$column->name()}' => ";
@@ -98,6 +95,8 @@ protected function buildDefinition(Model $model)
9895
implode(', ', [$scale, 0, (str_repeat(9, $precision - $scale) . '.' . str_repeat(9, $scale))]),
9996
$definition
10097
);
98+
} elseif ($column->dataType() == 'json') {
99+
$definition .= self::INDENT . "'{$column->name()}' => '[]'," . PHP_EOL;
101100
} else {
102101
$definition .= self::INDENT . "'{$column->name()}' => ";
103102
$faker = self::fakerData($column->name()) ?? self::fakerDataType($column->dataType());
@@ -158,11 +157,13 @@ public static function fakerDataType(string $type)
158157
'text' => 'text',
159158
'date' => 'date()',
160159
'time' => 'time()',
161-
'guid' => 'word',
160+
'guid' => 'uuid',
161+
'uuid' => 'uuid',
162162
'datetimetz' => 'dateTime()',
163163
'datetime' => 'dateTime()',
164164
'timestamp' => 'dateTime()',
165165
'integer' => 'randomNumber()',
166+
'unsignedsmallinteger' => 'randomNumber()',
166167
'bigint' => 'randomNumber()',
167168
'smallint' => 'randomNumber()',
168169
'decimal' => 'randomFloat(/** decimal_attributes **/)',

src/Generators/MigrationGenerator.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ public function output(array $tree): array
3030
/** @var \Blueprint\Models\Model $model */
3131
foreach ($tree['models'] as $model) {
3232
$path = $this->getPath($model, $sequential_timestamp->addSecond());
33-
$this->files->put(
34-
$path,
35-
$this->populateStub($stub, $model)
36-
);
33+
$this->files->put($path, $this->populateStub($stub, $model));
3734

3835
$output['created'][] = $path;
3936
}
@@ -57,15 +54,19 @@ protected function buildDefinition(Model $model)
5754
/** @var \Blueprint\Models\Column $column */
5855
foreach ($model->columns() as $column) {
5956
$dataType = $column->dataType();
60-
if ($column->name() === 'id') {
57+
if ($column->name() === 'id' && $dataType != 'uuid') {
6158
$dataType = 'bigIncrements';
6259
} elseif ($column->dataType() === 'id') {
6360
$dataType = 'unsignedBigInteger';
6461
}
6562

63+
if ($column->dataType() === 'uuid') {
64+
$dataType = 'uuid';
65+
}
66+
6667
$definition .= self::INDENT . '$table->' . $dataType . "('{$column->name()}'";
6768

68-
if (!empty($column->attributes()) && $column->dataType() !== 'id') {
69+
if (!empty($column->attributes()) && !in_array($column->dataType(), ['id', 'uuid'])) {
6970
$definition .= ', ';
7071
if (in_array($column->dataType(), ['set', 'enum'])) {
7172
$definition .= json_encode($column->attributes());
@@ -75,15 +76,27 @@ protected function buildDefinition(Model $model)
7576
}
7677
$definition .= ')';
7778

79+
$foreign = '';
80+
7881
foreach ($column->modifiers() as $modifier) {
7982
if (is_array($modifier)) {
80-
$definition .= "->" . key($modifier) . "(" . current($modifier) . ")";
83+
if (key($modifier) == 'foreign') {
84+
$foreign =
85+
self::INDENT .
86+
'$table->foreign(' .
87+
"'{$column->name()}')->references('id')->on('" .
88+
Str::lower(Str::plural(current($modifier))) .
89+
"')->onDelete('cascade');" .
90+
PHP_EOL;
91+
} else {
92+
$definition .= '->' . key($modifier) . '(' . current($modifier) . ')';
93+
}
8194
} else {
8295
$definition .= '->' . $modifier . '()';
8396
}
8497
}
8598

86-
$definition .= ';' . PHP_EOL;
99+
$definition .= ';' . PHP_EOL . $foreign;
87100
}
88101

89102
if ($model->usesSoftDeletes()) {

src/Generators/ModelGenerator.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ public function output(array $tree): array
2727
/** @var \Blueprint\Models\Model $model */
2828
foreach ($tree['models'] as $model) {
2929
$path = $this->getPath($model);
30-
$this->files->put(
31-
$path,
32-
$this->populateStub($stub, $model)
33-
);
30+
$this->files->put($path, $this->populateStub($stub, $model));
3431

3532
$output['created'][] = $path;
3633
}
@@ -194,14 +191,18 @@ private function castForColumn(Column $column)
194191
return 'decimal';
195192
}
196193

194+
if ($column->dataType() === 'json') {
195+
return 'array';
196+
}
197+
197198
return null;
198199
}
199200

200201
private function pretty_print_array(array $data, $assoc = true)
201202
{
202203
$output = var_export($data, true);
203204
$output = preg_replace('/^\s+/m', ' ', $output);
204-
$output = preg_replace(['/^array\s\(/', "/\)$/"], ['[', ' ]'], $output);
205+
$output = preg_replace(['/^array\s\(/', '/\)$/'], ['[', ' ]'], $output);
205206

206207
if (!$assoc) {
207208
$output = preg_replace('/^(\s+)[^=]+=>\s+/m', '$1', $output);
@@ -226,6 +227,7 @@ private function phpDataType(string $dataType)
226227
{
227228
static $php_data_types = [
228229
'id' => 'int',
230+
'uuid' => 'string',
229231
'bigincrements' => 'int',
230232
'biginteger' => 'int',
231233
'boolean' => 'bool',

src/Lexers/ModelLexer.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,16 @@ class ModelLexer implements Lexer
8686
'usecurrent' => 'useCurrent',
8787
'always' => 'always',
8888
'unique' => 'unique',
89+
'index' => 'index',
90+
'primary' => 'primary',
91+
'foreign' => 'foreign',
8992
];
9093

9194
public function analyze(array $tokens): array
9295
{
9396
$registry = [
9497
'models' => [],
95-
'cache' => []
98+
'cache' => [],
9699
];
97100

98101
if (!empty($tokens['models'])) {
@@ -107,7 +110,6 @@ public function analyze(array $tokens): array
107110
}
108111
}
109112

110-
111113
return $registry;
112114
}
113115

@@ -155,7 +157,7 @@ private function buildModel(string $name, array $columns)
155157
$column = $this->buildColumn($name, $definition);
156158
$model->addColumn($column);
157159

158-
if ($column->name() !== 'id' && $column->dataType() === 'id') {
160+
if ($column->name() !== 'id' && in_array($column->dataType(), ['id', 'uuid'])) {
159161
if ($column->attributes()) {
160162
$model->addRelationship('belongsTo', $column->attributes()[0] . ':' . $column->name());
161163
} else {

0 commit comments

Comments
 (0)