Skip to content

Commit

Permalink
rename
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Nov 4, 2024
1 parent 4d850e3 commit 75f1a73
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/Contracts/TableSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ public function getColumns(): Collection;
/**
* @return Collection<int, string>
*/
public function getPrimaryKey(): Collection;
public function getPrimaryKeys(): Collection;
}
4 changes: 2 additions & 2 deletions src/ErdFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ private function findByModels(Collection $models, array $excludes = []): Collect
->map(function (Collection $relations) use ($excludes) {
return $relations
->reject(fn (Relation $relation) => $relation->excludes($excludes))
->sortBy(fn (Relation $relation) => $relation->unique())
->unique(fn (Relation $relation) => $relation->unique())
->sortBy(fn (Relation $relation) => $relation->sortByKeys())
->unique(fn (Relation $relation) => $relation->sortByKeys())
->groupBy(fn (Relation $relation) => $relation->localTable());
});

Expand Down
26 changes: 13 additions & 13 deletions src/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,29 @@ public function relatedRelation(): Relation
]));
}

public function order(): int
public function sortByRelation(): int
{
$orders = [
$relationGroups = [
[BelongsTo::class, HasOne::class, MorphOne::class],
[HasMany::class, MorphMany::class],
];

$type = $this->type();
foreach ($orders as $index => $order) {
if (in_array($type, $order, true)) {
foreach ($relationGroups as $index => $relations) {
if (in_array($type, $relations, true)) {
return $index + 1;
}
}

return count($orders);
return count($relationGroups);
}

/**
* @return string[]
*/
public function sortByKeys(): array
{
return [$this->type(), $this->localKey(), $this->foreignKey()];
}

public function uniqueId(): string
Expand All @@ -154,12 +162,4 @@ public function uniqueId(): string

return implode('::', $sortBy);
}

/**
* @return string[]
*/
public function unique(): array
{
return [$this->type(), $this->localKey(), $this->foreignKey()];
}
}
2 changes: 1 addition & 1 deletion src/Schema/DBAL/TableSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function getColumns(): Collection
return collect($this->table->getColumns())->map(fn (DBALColumn $column) => new ColumnSchema($column));
}

public function getPrimaryKey(): Collection
public function getPrimaryKeys(): Collection
{
return collect($this->table->getIndexes())
->filter(fn (Index $index) => $index->isPrimary())
Expand Down
2 changes: 1 addition & 1 deletion src/Schema/Laravel/TableSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function getColumns(): Collection
return collect($this->builder->getColumns($this->name))->map(fn (array $column) => new ColumnSchema($column));
}

public function getPrimaryKey(): Collection
public function getPrimaryKeys(): Collection
{
return collect($this->builder->getIndexes($this->name))
->filter(fn (array $column) => $column['primary'] === true)
Expand Down
4 changes: 2 additions & 2 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public function getColumns(): Collection
return $this->schema->getColumns();
}

public function getPrimaryKey(): Collection
public function getPrimaryKeys(): Collection
{
return $this->schema->getPrimaryKey();
return $this->schema->getPrimaryKeys();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Template/DDL.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private function renderColumn(Table $table): string
*/
private function renderPrimaryKeys(Table $table): array
{
$primaryKeys = $table->getPrimaryKey()->implode(', ');
$primaryKeys = $table->getPrimaryKeys()->implode(', ');

return $primaryKeys ? ["PRIMARY KEY({$primaryKeys})"] : [];
}
Expand Down
6 changes: 3 additions & 3 deletions src/Template/Er.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class Er implements Template
{
/** @var string[] */
private static array $relations = [
private static array $relationships = [
BelongsTo::class => '1--*',
MorphTo::class => '1--*',
HasOne::class => '1--1',
Expand Down Expand Up @@ -74,7 +74,7 @@ public function save(Collection $tables, string $path, array $options = []): int

private function renderTable(Table $table): string
{
$primaryKeys = $table->getPrimaryKey();
$primaryKeys = $table->getPrimaryKeys();
$indexes = $table
->getRelations()
->flatMap(fn (Relation $relation) => [$relation->localColumn(), $relation->morphColumn()])
Expand Down Expand Up @@ -105,7 +105,7 @@ private function renderRelation(Relation $relation): string
return sprintf(
'%s %s %s',
$relation->localTable(),
self::$relations[$relation->type()],
self::$relationships[$relation->type()],
$relation->foreignTable()
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/RelationFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ private function draw(Collection $relations, $method): array
{
return $relations
->get($method)
->sortBy(fn (Relation $relation) => $relation->order())
->sortBy(fn (Relation $relation) => $relation->sortByRelation())
->map(fn (Relation $relation) => $this->renderRelationship($relation))
->toArray();
}
Expand Down

0 comments on commit 75f1a73

Please sign in to comment.