Skip to content

Commit

Permalink
Remove old ts transformer stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Aug 1, 2023
1 parent 3e887dd commit 4eea39e
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 257 deletions.
22 changes: 0 additions & 22 deletions src/Support/TypeScriptTransformer/DataTypeScriptCollector.php

This file was deleted.

144 changes: 3 additions & 141 deletions src/Support/TypeScriptTransformer/DataTypeScriptTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,151 +20,13 @@
use Spatie\LaravelData\Support\Lazy\ClosureLazy;
use Spatie\LaravelTypeScriptTransformer\Transformers\DtoTransformer;
use Spatie\TypeScriptTransformer\Attributes\Optional as TypeScriptOptional;
use Spatie\TypeScriptTransformer\Laravel\Transformers\DataClassTransformer;
use Spatie\TypeScriptTransformer\Structures\MissingSymbolsCollection;
use Spatie\TypeScriptTransformer\TypeProcessors\DtoCollectionTypeProcessor;
use Spatie\TypeScriptTransformer\TypeProcessors\ReplaceDefaultsTypeProcessor;
use Spatie\TypeScriptTransformer\Types\StructType;

class DataTypeScriptTransformer extends DtoTransformer
class DataTypeScriptTransformer extends DataClassTransformer
{
public function canTransform(ReflectionClass $class): bool
{
return $class->isSubclassOf(BaseData::class);
}

protected function typeProcessors(): array
{
return [
new ReplaceDefaultsTypeProcessor(
$this->config->getDefaultTypeReplacements()
),
new RemoveLazyTypeProcessor(),
new RemoveOptionalTypeProcessor(),
new DtoCollectionTypeProcessor(),
];
}


protected function transformProperties(
ReflectionClass $class,
MissingSymbolsCollection $missingSymbols
): string {
$dataClass = app(DataConfig::class)->getDataClass($class->getName());

$isOptional = $dataClass->attributes->contains(
fn (object $attribute) => $attribute instanceof TypeScriptOptional
);

return array_reduce(
$this->resolveProperties($class),
function (string $carry, ReflectionProperty $property) use ($isOptional, $dataClass, $missingSymbols) {
/** @var \Spatie\LaravelData\Support\DataProperty $dataProperty */
$dataProperty = $dataClass->properties[$property->getName()];

$type = $this->resolveTypeForProperty($property, $dataProperty, $missingSymbols);

if ($type === null) {
return $carry;
}

$isOptional = $isOptional
|| $dataProperty->attributes->contains(
fn (object $attribute) => $attribute instanceof TypeScriptOptional
)
|| ($dataProperty->type->lazyType && $dataProperty->type->lazyType !== ClosureLazy::class)
|| $dataProperty->type->isOptional;

$transformed = $this->typeToTypeScript(
$type,
$missingSymbols,
$property->getDeclaringClass()->getName(),
);

$propertyName = $dataProperty->outputMappedName ?? $dataProperty->name;

if (! preg_match('/^[$_a-zA-Z][$_a-zA-Z0-9]*$/', $propertyName)) {
$propertyName = "'{$propertyName}'";
}

return $isOptional
? "{$carry}{$propertyName}?: {$transformed};" . PHP_EOL
: "{$carry}{$propertyName}: {$transformed};" . PHP_EOL;
},
''
);
}

protected function resolveTypeForProperty(
ReflectionProperty $property,
DataProperty $dataProperty,
MissingSymbolsCollection $missingSymbols,
): ?Type {
if (! $dataProperty->type->kind->isDataCollectable()) {
return $this->reflectionToType(
$property,
$missingSymbols,
...$this->typeProcessors()
);
}

$collectionType = match ($dataProperty->type->kind) {
DataTypeKind::Enumerable, DataTypeKind::Array, DataTypeKind::DataCollection => $this->defaultCollectionType($dataProperty->type->dataClass),
DataTypeKind::Paginator, DataTypeKind::DataPaginatedCollection => $this->paginatedCollectionType($dataProperty->type->dataClass),
DataTypeKind::CursorPaginator, DataTypeKind::DataCursorPaginatedCollection => $this->cursorPaginatedCollectionType($dataProperty->type->dataClass),
default => throw new RuntimeException('Cannot end up here since the type is dataCollectable')
};

if ($dataProperty->type->isNullable()) {
return new Nullable($collectionType);
}

return $collectionType;
}

protected function defaultCollectionType(string $class): Type
{
return new Array_(new Object_(new Fqsen("\\{$class}")));
}

protected function paginatedCollectionType(string $class): Type
{
return new StructType([
'data' => $this->defaultCollectionType($class),
'links' => new Array_(new StructType([
'url' => new Nullable(new String_()),
'label' => new String_(),
'active' => new Boolean(),
])),
'meta' => new StructType([
'current_page' => new Integer(),
'first_page_url' => new String_(),
'from' => new Nullable(new Integer()),
'last_page' => new Integer(),
'last_page_url' => new String_(),
'next_page_url' => new Nullable(new String_()),
'path' => new String_(),
'per_page' => new Integer(),
'prev_page_url' => new Nullable(new String_()),
'to' => new Nullable(new Integer()),
'total' => new Integer(),

]),
]);
}

protected function cursorPaginatedCollectionType(string $class): Type
{
return new StructType([
'data' => $this->defaultCollectionType($class),
'links' => new Array_(),
'meta' => new StructType([
'path' => new String_(),
'per_page' => new Integer(),
'next_cursor' => new Nullable(new String_()),
'next_cursor_url' => new Nullable(new String_()),
'prev_cursor' => new Nullable(new String_()),
'prev_cursor_url' => new Nullable(new String_()),
]),
]);
}
// TODO implement this ourselves
}
47 changes: 0 additions & 47 deletions src/Support/TypeScriptTransformer/RemoveLazyTypeProcessor.php

This file was deleted.

47 changes: 0 additions & 47 deletions src/Support/TypeScriptTransformer/RemoveOptionalTypeProcessor.php

This file was deleted.

0 comments on commit 4eea39e

Please sign in to comment.