Skip to content

Commit

Permalink
Remove namespaces and change interfaces to types
Browse files Browse the repository at this point in the history
  • Loading branch information
alexthekiwi committed Feb 22, 2023
1 parent ddca731 commit 0cd4845
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
9 changes: 4 additions & 5 deletions src/Generators/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ public function generate(ReflectionClass $reflection): ?string
$this->boot();

if (empty(trim($definition = $this->getDefinition()))) {
return " export interface {$this->tsClassName()} {}" . PHP_EOL;
return " {$this->tsClassName()}: {};" . PHP_EOL;
}

return <<<TS
export interface {$this->tsClassName()} {
$definition
}
{$this->tsClassName()}: {
$definition
};
TS;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function getDefinition(): ?string
$this->getAccessors(),
])
->filter(fn (string $part) => !empty($part))
->join(PHP_EOL . ' ');
->join(PHP_EOL . ' ');
}

/**
Expand All @@ -72,7 +72,7 @@ protected function getProperties(): string
nullable: !$column->getNotnull()
);
})
->join(PHP_EOL . ' ');
->join(PHP_EOL . ' ');
}

protected function getAccessors(): string
Expand All @@ -98,7 +98,7 @@ protected function getAccessors(): string
readonly: true
);
})
->join(PHP_EOL . ' ');
->join(PHP_EOL . ' ');
}

protected function getRelations(): string
Expand All @@ -112,7 +112,7 @@ protected function getRelations(): string
nullable: true
);
})
->join(PHP_EOL . ' ');
->join(PHP_EOL . ' ');
}

protected function getManyRelations(): string
Expand All @@ -127,7 +127,7 @@ protected function getManyRelations(): string
nullable: true
);
})
->join(PHP_EOL . ' ');
->join(PHP_EOL . ' ');
}

protected function getRelationMethods(): Collection
Expand Down
14 changes: 11 additions & 3 deletions src/TypeScriptGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,25 @@ public function execute()

protected function makeNamespace(string $namespace, Collection $reflections): string
{
return $reflections->map(fn (ReflectionClass $reflection) => $this->makeInterface($reflection))
return $reflections->map(fn (ReflectionClass $reflection) => $this->makeType($reflection))
->whereNotNull()
->whenNotEmpty(function (Collection $definitions) use ($namespace) {
$tsNamespace = str_replace('\\', '.', $namespace);
$parts = explode('.', $tsNamespace);
$rootType = array_shift($parts);

return $definitions->prepend("declare namespace {$tsNamespace} {")->push('}' . PHP_EOL);
foreach ($parts as $type) {
$definitions->prepend(" {$type}: {")->push('};' . PHP_EOL);
}

$definitions->prepend("export type {$rootType} = {")->push('};' . PHP_EOL);

return $definitions;
})
->join(PHP_EOL);
}

protected function makeInterface(ReflectionClass $reflection): ?string
protected function makeType(ReflectionClass $reflection): ?string
{
$generator = collect($this->generators)
->filter(fn (string $generator, string $baseClass) => $reflection->isSubclassOf($baseClass))
Expand Down

0 comments on commit 0cd4845

Please sign in to comment.