Skip to content

Commit

Permalink
Better naming for column name (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Oct 19, 2023
1 parent 44f9ac4 commit 024c175
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion resources/views/_addColumns.php
Expand Up @@ -10,7 +10,7 @@
*/

foreach ($columns as $column) {
echo " \$b->addColumn('$table', '{$column->getProperty()}', \$b->{$column->getDecoratorsString()});\n";
echo " \$b->addColumn('$table', '{$column->getName()}', \$b->{$column->getDecoratorsString()});\n";
}

echo $this->render(__DIR__ . '/_addForeignKeys.php', [
Expand Down
4 changes: 2 additions & 2 deletions resources/views/_createTable.php
Expand Up @@ -14,9 +14,9 @@
echo " \$b->createTable('$table', [\n";
foreach ($columns as $column) {
if (!$column->hasDecorators()) {
echo " '{$column->getProperty()}',\n";
echo " '{$column->getName()}',\n";
} else {
echo " '{$column->getProperty()}' => \$b->{$column->getDecoratorsString()},\n";
echo " '{$column->getName()}' => \$b->{$column->getDecoratorsString()},\n";
}
}
echo " ]);\n";
Expand Down
2 changes: 1 addition & 1 deletion resources/views/_dropColumns.php
Expand Up @@ -15,5 +15,5 @@
]);

foreach ($columns as $column) {
echo " \$b->dropColumn('$table', '{$column->getProperty()}');\n";
echo " \$b->dropColumn('$table', '{$column->getName()}');\n";
}
6 changes: 3 additions & 3 deletions src/Service/Generate/Column.php
Expand Up @@ -10,14 +10,14 @@ final class Column
* @param string[] $decorators
*/
public function __construct(
private string $property,
private string $name,
private array $decorators = []
) {
}

public function getProperty(): string
public function getName(): string
{
return $this->property;
return $this->name;
}

public function getDecorators(): array
Expand Down
8 changes: 4 additions & 4 deletions src/Service/Generate/FieldsParser.php
Expand Up @@ -40,16 +40,16 @@ public function parse(
/** @psalm-var string[] $fields */
foreach ($fields as $field) {
$chunks = $this->splitFieldIntoChunks($field);
$column = (string) array_shift($chunks);
$columnName = (string) array_shift($chunks);

/** @psalm-var string[] $chunks */
foreach ($chunks as $i => $chunk) {
if (str_starts_with($chunk, 'foreignKey')) {
preg_match('/foreignKey\((\w*)\s?(\w*)\)/', $chunk, $matches);
$foreignKeys[] = $this->foreignKeyFactory->create(
$table,
$column,
$matches[1] ?? preg_replace('/_id$/', '', $column),
$columnName,
$matches[1] ?? preg_replace('/_id$/', '', $columnName),
empty($matches[2]) ? null : $matches[2]
);

Expand All @@ -63,7 +63,7 @@ public function parse(
}

/** @psalm-var string[] $chunks */
$columns[] = new Column($column, $chunks);
$columns[] = new Column($columnName, $chunks);
}
}

Expand Down

0 comments on commit 024c175

Please sign in to comment.