Skip to content

Commit

Permalink
Merge pull request #33 from nurdism/master
Browse files Browse the repository at this point in the history
Add more options
  • Loading branch information
acidjazz authored Mar 8, 2023
2 parents a4aa7fb + 3912791 commit a7d1f47
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 33 deletions.
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ public function getFirstNameAttribute(): string // <- this
}
```

### Additional Options

```
--no-relations : Do not include relations
--optional-relations : Make relations optional fields on the model type
--no-hidden : Do not include hidden model attributes
--timestamps-date : Output timestamps as a Date object type
--optional-nullables : Output nullable attributes as optional fields
```

### Custom Interfaces

If you have custom interfaces you are using for your models you can specify them in a reserved `interfaces` array
Expand Down
38 changes: 21 additions & 17 deletions src/Actions/GenerateCliOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class GenerateCliOutput
*
* @param Collection<int, SplFileInfo> $models
*/
public function __invoke(Collection $models, bool $global = false, bool $plurals = false, bool $apiResources = false): string
public function __invoke(Collection $models, bool $global = false, bool $plurals = false, bool $apiResources = false, bool $optionalRelations = false, bool $noRelations = false, bool $noHidden = false, bool $timestampsDate = false, bool $optionalNullables = false): string
{
$modelBuilder = app(BuildModelDetails::class);
$colAttrWriter = app(WriteColumnAttribute::class);
Expand All @@ -38,7 +38,7 @@ public function __invoke(Collection $models, bool $global = false, bool $plurals
$this->indent = ' ';
}

$models->each(function (SplFileInfo $model) use ($modelBuilder, $colAttrWriter, $relationWriter, $plurals, $apiResources) {
$models->each(function (SplFileInfo $model) use ($modelBuilder, $colAttrWriter, $relationWriter, $plurals, $apiResources, $optionalRelations, $noRelations, $noHidden, $timestampsDate, $optionalNullables) {
$entry = '';

[
Expand All @@ -57,38 +57,42 @@ public function __invoke(Collection $models, bool $global = false, bool $plurals

if ($columns->isNotEmpty()) {
$entry .= "{$this->indent} // columns\n";
$columns->each(function ($att) use (&$entry, $reflectionModel, $colAttrWriter) {
[$line, $enum] = $colAttrWriter($reflectionModel, $att, $this->indent);
$entry .= $line;
if ($enum) {
$this->enumReflectors[] = $enum;
$columns->each(function ($att) use (&$entry, $reflectionModel, $colAttrWriter, $noHidden, $timestampsDate, $optionalNullables) {
[$line, $enum] = $colAttrWriter(reflectionModel: $reflectionModel, attribute: $att, indent: $this->indent, noHidden: $noHidden, timestampsDate: $timestampsDate, optionalNullables: $optionalNullables);
if (!empty($line)) {
$entry .= $line;
if ($enum) {
$this->enumReflectors[] = $enum;
}
}
});
}

if ($nonColumns->isNotEmpty()) {
$entry .= "{$this->indent} // mutators\n";
$nonColumns->each(function ($att) use (&$entry, $reflectionModel, $colAttrWriter) {
[$line, $enum] = $colAttrWriter($reflectionModel, $att, $this->indent);
$entry .= $line;
if ($enum) {
$this->enumReflectors[] = $enum;
$nonColumns->each(function ($att) use (&$entry, $reflectionModel, $colAttrWriter, $noHidden, $timestampsDate, $optionalNullables) {
[$line, $enum] = $colAttrWriter(reflectionModel: $reflectionModel, attribute: $att, indent: $this->indent, noHidden: $noHidden, timestampsDate: $timestampsDate, optionalNullables: $optionalNullables);
if (!empty($line)) {
$entry .= $line;
if ($enum) {
$this->enumReflectors[] = $enum;
}
}
});
}

if ($interfaces->isNotEmpty()) {
$entry .= "{$this->indent} // overrides\n";
$interfaces->each(function ($interface) use (&$entry, $reflectionModel, $colAttrWriter) {
[$line] = $colAttrWriter($reflectionModel, $interface, $this->indent);
$interfaces->each(function ($interface) use (&$entry, $reflectionModel, $colAttrWriter, $timestampsDate) {
[$line] = $colAttrWriter(reflectionModel: $reflectionModel, attribute: $interface, indent: $this->indent, timestampsDate: $timestampsDate);
$entry .= $line;
});
}

if ($relations->isNotEmpty()) {
if ($relations->isNotEmpty() && !$noRelations) {
$entry .= "{$this->indent} // relations\n";
$relations->each(function ($rel) use (&$entry, $relationWriter) {
$entry .= $relationWriter($rel, $this->indent);
$relations->each(function ($rel) use (&$entry, $relationWriter, $optionalRelations) {
$entry .= $relationWriter(relation: $rel, indent: $this->indent, optionalRelation: $optionalRelations);
});
}

Expand Down
8 changes: 4 additions & 4 deletions src/Actions/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class Generator
*
* @return string
*/
public function __invoke(?string $specificModel = null, bool $global = false, bool $json = false, bool $plurals = false, bool $apiResources = false)
public function __invoke(?string $specificModel = null, bool $global = false, bool $json = false, bool $plurals = false, bool $apiResources = false, bool $optionalRelations = false, bool $noRelations = false, bool $noHidden = false, bool $timestampsDate = false, bool $optionalNullables = false)
{
$models = $this->getModels($specificModel);

return $this->display($models, $global, $json, $plurals, $apiResources);
return $this->display($models, $global, $json, $plurals, $apiResources, $optionalRelations, $noRelations, $noHidden, $timestampsDate, $optionalNullables);
}

/**
Expand All @@ -41,7 +41,7 @@ protected function getModels(?string $model = null): Collection
*
* @param Collection<int, SplFileInfo> $models
*/
protected function display(Collection $models, bool $global = false, bool $json = false, bool $plurals = false, bool $apiResources = false): string
protected function display(Collection $models, bool $global = false, bool $json = false, bool $plurals = false, bool $apiResources = false, bool $optionalRelations = false, bool $noRelations = false, bool $noHidden = false, bool $timestampsDate = false, bool $optionalNullables = false): string
{
if ($models->isEmpty()) {
return 'ERROR: No models found.';
Expand All @@ -51,6 +51,6 @@ protected function display(Collection $models, bool $global = false, bool $json
return app(GenerateJsonOutput::class)($models);
}

return app(GenerateCliOutput::class)($models, $global, $plurals, $apiResources);
return app(GenerateCliOutput::class)($models, $global, $plurals, $apiResources, $optionalRelations, $noRelations, $noHidden, $timestampsDate, $optionalNullables);
}
}
6 changes: 5 additions & 1 deletion src/Actions/MapReturnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ class MapReturnType
* @param string $returnType
* @return string
*/
public function __invoke(string $returnType): string
public function __invoke(string $returnType, bool $timestampsDate = false): string
{
$mappings = TypescriptMappings::$mappings;
if ($timestampsDate) {
$mappings['datetime'] = 'Date';
$mappings['date'] = 'Date';
}

$returnType = explode(' ', $returnType)[0];
$returnType = explode('(', $returnType)[0];
Expand Down
16 changes: 10 additions & 6 deletions src/Actions/WriteColumnAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,25 @@ class WriteColumnAttribute
* @param bool $jsonOutput
* @return array
*/
public function __invoke(ReflectionClass $reflectionModel, array $attribute, string $indent = '', bool $jsonOutput = false): array
public function __invoke(ReflectionClass $reflectionModel, array $attribute, string $indent = '', bool $jsonOutput = false, bool $noHidden = false, bool $timestampsDate = false, bool $optionalNullables = false): array
{
$enumRef = null;
$returnType = app(MapReturnType::class);

$name = Str::snake($attribute['name']);
$type = 'unknown';

if ($noHidden && isset($attribute['hidden']) && $attribute['hidden']) {
return [null, null];
}

if (isset($attribute['forceType'])) {
$name = $attribute['name'];
$type = $attribute['type'];
} else {
if (! is_null($attribute['cast']) && $attribute['cast'] !== $attribute['type']) {
if (isset(TypescriptMappings::$mappings[$attribute['cast']])) {
$type = $returnType($attribute['cast']);
$type = $returnType($attribute['cast'], $timestampsDate);
} else {
if ($attribute['type'] === 'json' || $this->getClassName($attribute['cast']) === 'AsCollection' || $this->getClassName($attribute['cast']) === 'AsArrayObject') {
$type = $returnType('json');
Expand All @@ -50,7 +54,7 @@ public function __invoke(ReflectionClass $reflectionModel, array $attribute, str
if (! is_null($closure->get)) {
$rf = new ReflectionFunction($closure->get);
if ($rf->hasReturnType()) {
$type = $returnType($rf->getReturnType()->getName());
$type = $returnType($rf->getReturnType()->getName(), $timestampsDate);
}
}
} else {
Expand All @@ -68,7 +72,7 @@ public function __invoke(ReflectionClass $reflectionModel, array $attribute, str
$cleanStr = Str::of($attribute['cast'])->before(':')->__toString();

if (isset(TypescriptMappings::$mappings[$cleanStr])) {
$type = $returnType($cleanStr);
$type = $returnType($cleanStr, $timestampsDate);
} else {
dump('Unknown cast type: ' . $attribute['cast']);
}
Expand All @@ -77,15 +81,15 @@ public function __invoke(ReflectionClass $reflectionModel, array $attribute, str
}
}
} else {
$type = $returnType($attribute['type']);
$type = $returnType($attribute['type'], $timestampsDate);
}
}

if ($attribute['nullable']) {
$type .= '|null';
}

if (isset($attribute['hidden']) && $attribute['hidden']) {
if ((isset($attribute['hidden']) && $attribute['hidden']) || ($optionalNullables && $attribute['nullable'])) {
$name = "{$name}?";
}

Expand Down
5 changes: 3 additions & 2 deletions src/Actions/WriteRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ class WriteRelationship
* @param bool $jsonOutput
* @return array|string
*/
public function __invoke(array $relation, string $indent = '', bool $jsonOutput = false): array|string
public function __invoke(array $relation, string $indent = '', bool $jsonOutput = false, bool $optionalRelation = false): array|string
{
$name = Str::snake($relation['name']);
$relatedModel = $this->getClassName($relation['related']);
$optional = $optionalRelation ? '?' : '';

$relationType = match ($relation['type']) {
'BelongsToMany', 'HasMany', 'HasManyThrough', 'MorphToMany', 'MorphMany', 'MorphedByMany' => Str::plural($relatedModel),
Expand All @@ -43,6 +44,6 @@ public function __invoke(array $relation, string $indent = '', bool $jsonOutput
];
}

return "{$indent} {$name}: {$relationType}\n";
return "{$indent} {$name}{$optional}: {$relationType}\n";
}
}
7 changes: 6 additions & 1 deletion src/Commands/ModelTyperCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class ModelTyperCommand extends Command
{--global : Generate your interfaces in a global namespace named models}
{--json : Output the result as json}
{--plurals : Output model plurals}
{--no-relations : Do not include relations}
{--optional-relations : Make relations optional fields on the model type}
{--no-hidden : Do not include hidden model attributes}
{--timestamps-date : Output timestamps as a Date object type}
{--optional-nullables : Output nullable attributes as optional fields}
{--api-resources : Output api.MetApi interfaces}
{--all : Enable all output options (equivalent to --plurals --api-resources)}';

Expand Down Expand Up @@ -61,7 +66,7 @@ public function handle(Generator $generator): int
$plurals = $this->option('plurals') || $this->option('all');
$apiResources = $this->option('api-resources') || $this->option('all');

echo $generator($this->option('model'), $this->option('global'), $this->option('json'), $plurals, $apiResources);
echo $generator($this->option('model'), $this->option('global'), $this->option('json'), $plurals, $apiResources, $this->option('optional-relations'), $this->option('no-relations'), $this->option('no-hidden'), $this->option('timestamps-date'), $this->option('optional-nullables'));

return Command::SUCCESS;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Constants/TypescriptMappings.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class TypescriptMappings
'float' => 'number',
'string' => 'string',
'decimal' => 'number',
'datetime' => 'Date',
'date' => 'Date',
'datetime' => 'string',
'date' => 'string',
'bool' => 'boolean',
'boolean' => 'boolean',
'json' => 'Record<string, unknown>',
Expand Down

0 comments on commit a7d1f47

Please sign in to comment.