Skip to content

Commit b47acad

Browse files
authored
Generate model relationships (#72)
1 parent 02cbbd2 commit b47acad

File tree

4 files changed

+69
-26
lines changed

4 files changed

+69
-26
lines changed

src/Generators/FactoryGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected function buildDefinition(Model $model)
6363

6464
/** @var \Blueprint\Models\Column $column */
6565
foreach ($model->columns() as $column) {
66-
if ($column->name() === 'id') {
66+
if ($column->name() === 'id' || $column->name() === 'relationships') {
6767
continue;
6868
}
6969

src/Generators/MigrationGenerator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ protected function buildDefinition(Model $model)
6161
$dataType = 'bigIncrements';
6262
} elseif ($column->dataType() === 'id') {
6363
$dataType = 'unsignedBigInteger';
64+
} elseif ($column->name() === 'relationships') {
65+
continue;
6466
}
6567

6668
$definition .= self::INDENT . '$table->' . $dataType . "('{$column->name()}'";

src/Generators/ModelGenerator.php

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ protected function populateStub(string $stub, Model $model)
4646

4747
$body = $this->buildProperties($model);
4848
$body .= PHP_EOL . PHP_EOL;
49+
$body .= $this->buildBelongsTo($model);
4950
$body .= $this->buildRelationships($model);
5051

5152
$stub = str_replace('// ...', trim($body), $stub);
@@ -110,7 +111,7 @@ private function buildProperties(Model $model)
110111
return trim($properties);
111112
}
112113

113-
private function buildRelationships(Model $model)
114+
private function buildBelongsTo(Model $model)
114115
{
115116
$columns = array_filter($model->columns(), function (Column $column) {
116117
return $column->name() !== 'id' && $column->dataType() === 'id';
@@ -138,6 +139,41 @@ private function buildRelationships(Model $model)
138139
return $methods;
139140
}
140141

142+
private function buildRelationships(Model $model)
143+
{
144+
$columns = array_filter($model->columns(), function (Column $column) {
145+
return $column->name() === 'relationships';
146+
});
147+
148+
if (empty($columns)) {
149+
return '';
150+
}
151+
152+
$methods = '';
153+
$template = $this->files->stub('model/method.stub');
154+
155+
/** @var Column $column */
156+
foreach ($columns as $column) {
157+
foreach ($column->attributes() as $methodName => $modelName) {
158+
if ('belongsTo' === $methodName) {
159+
throw new \Exception('The belongsTo relationship for the '.$modelName.' model on the '.$model->name().' model should be defined using the '.$modelName.'_id: id syntax');
160+
}
161+
$class = Str::studly($column->attributes()[0] ?? $modelName);
162+
$relationship = sprintf("\$this->%s(%s::class)",
163+
$methodName,
164+
'\\' . $model->fullyQualifiedNamespace() . '\\' . $class);
165+
166+
$modelNameForMethod = Str::contains($methodName, 'Many') ? Str::plural($modelName) : $modelName;
167+
$method = str_replace('DummyName', Str::camel($modelNameForMethod), $template);
168+
$method = str_replace('null', $relationship, $method);
169+
170+
$methods .= PHP_EOL . $method;
171+
}
172+
}
173+
174+
return $methods;
175+
}
176+
141177
protected function getPath(Model $model)
142178
{
143179
$path = str_replace('\\', '/', Blueprint::relativeNamespace($model->fullyQualifiedClassName()));
@@ -152,7 +188,8 @@ private function fillableColumns(array $columns)
152188
'password',
153189
'deleted_at',
154190
'created_at',
155-
'updated_at'
191+
'updated_at',
192+
'relationships',
156193
]);
157194
}
158195

src/Lexers/ModelLexer.php

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -135,35 +135,39 @@ private function buildModel(string $name, array $columns)
135135
return $model;
136136
}
137137

138-
private function buildColumn(string $name, string $definition)
138+
private function buildColumn(string $name, $definition)
139139
{
140140
$data_type = 'string';
141141
$modifiers = [];
142142

143-
$tokens = explode(' ', $definition);
144-
foreach ($tokens as $token) {
145-
$parts = explode(':', $token);
146-
$value = $parts[0];
147-
148-
if ($value === 'id') {
149-
$data_type = 'id';
150-
if (isset($parts[1])) {
151-
$attributes = [$parts[1]];
152-
}
153-
} elseif (isset(self::$dataTypes[strtolower($value)])) {
154-
$attributes = $parts[1] ?? null;
155-
$data_type = self::$dataTypes[strtolower($value)];
156-
if (!empty($attributes)) {
157-
$attributes = explode(',', $attributes);
143+
if ($name === 'relationships' && is_array($definition)) {
144+
$attributes = $definition;
145+
} else {
146+
$tokens = explode(' ', $definition);
147+
foreach ($tokens as $token) {
148+
$parts = explode(':', $token);
149+
$value = $parts[0];
150+
151+
if ($value === 'id') {
152+
$data_type = 'id';
153+
if (isset($parts[1])) {
154+
$attributes = [$parts[1]];
155+
}
156+
} elseif (isset(self::$dataTypes[strtolower($value)])) {
157+
$attributes = $parts[1] ?? null;
158+
$data_type = self::$dataTypes[strtolower($value)];
159+
if (!empty($attributes)) {
160+
$attributes = explode(',', $attributes);
161+
}
158162
}
159-
}
160163

161-
if (isset(self::$modifiers[strtolower($value)])) {
162-
$modifierAttributes = $parts[1] ?? null;
163-
if (empty($modifierAttributes)) {
164-
$modifiers[] = self::$modifiers[strtolower($value)];
165-
} else {
166-
$modifiers[] = [self::$modifiers[strtolower($value)] => $modifierAttributes];
164+
if (isset(self::$modifiers[strtolower($value)])) {
165+
$modifierAttributes = $parts[1] ?? null;
166+
if (empty($modifierAttributes)) {
167+
$modifiers[] = self::$modifiers[strtolower($value)];
168+
} else {
169+
$modifiers[] = [self::$modifiers[strtolower($value)] => $modifierAttributes];
170+
}
167171
}
168172
}
169173
}

0 commit comments

Comments
 (0)