Skip to content

Commit

Permalink
✨ make sure to add any remaining interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed Aug 22, 2021
1 parent 052775a commit ca301fa
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
45 changes: 43 additions & 2 deletions src/ModelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ModelInterface
'bool' => 'boolean',
'boolean' => 'boolean',
'json' => '[]',
'array' => 'string[]',
'point' => 'Point',
];

Expand Down Expand Up @@ -93,14 +94,17 @@ private function getInterface(Model $model): TypescriptInterface
$columns = $this->getColumns($model);
$mutators = $this->getMutators($model);
$relations = $this->getRelations($model);
$interfaces = $this->getInterfaces($model, $columns, $mutators, $relations);
return new TypescriptInterface(
name: (new ReflectionClass($model))->getShortName(),
columns: $columns,
mutators: $mutators,
relations: $relations,
interfaces: $interfaces,
);
}


/**
* Build TS code from an interface
* @param TypescriptInterface $interface
Expand All @@ -127,6 +131,12 @@ private function getCode(TypescriptInterface $interface): string
$code .= " $key: $value\n";
}
}
if (count($interface->interfaces) > 0) {
$code .= " // interfaces\n";
foreach ($interface->interfaces as $key => $value) {
$code .= " $key: $value\n";
}
}
$code .= "}\n";
$plural = Str::plural($interface->name);
$code .= "export type $plural = Array<{$interface->name}>\n\n";
Expand Down Expand Up @@ -192,6 +202,37 @@ public function getRelations(Model $model): array
return $relations;
}

/**
* Return any other remaining interfaces
*
* @param Model $model
* @param array $columns
* @param array $mutators
* @param array $relations
* @return array
*/
private function getInterfaces(Model $model, array $columns, array $mutators, array $relations): array
{
if (!isset($model->interfaces)) {
return [];
}
$interfaces = [];
foreach ($model->interfaces as $key=>$interface) {
if (array_key_exists($key, $columns)) {
continue;
}
if (array_key_exists($key, $mutators)) {
continue;
}
if (array_key_exists($key, $relations)) {
continue;
}
$interfaces[$key] = $interface['name'];
}
return $interfaces;
}


/**
* Find and map our get mutators
* @param Model $model
Expand Down Expand Up @@ -285,8 +326,8 @@ private function getColumnList(Model $model): array
return $model->getConnection()->getSchemaBuilder()->getColumnListing($model->getTable());
}

/**
* Get column details
/**
* Get column details
* @param Model $model
* @param string $column
* @return Column
Expand Down
1 change: 1 addition & 0 deletions src/TypescriptInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public function __construct(
public array $columns,
public array $mutators,
public array $relations,
public array $interfaces,
) {
}
}

0 comments on commit ca301fa

Please sign in to comment.