Skip to content

Commit

Permalink
Merge pull request #42 from innocenzi/feat/enums-in-default-transformer
Browse files Browse the repository at this point in the history
feat: add `EnumCollector`
  • Loading branch information
rubenvanassche authored Feb 1, 2023
2 parents ff15cb5 + f5e1b72 commit b88bfe6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Collectors/DefaultCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function resolveAlreadyTransformedType(ClassTypeReflector $reflector):

$transpiler = new TranspileTypeToTypeScriptAction(
$missingSymbols,
$name,
$name
);

return TransformedType::create(
Expand Down
38 changes: 38 additions & 0 deletions src/Collectors/EnumCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Spatie\TypeScriptTransformer\Collectors;

use BackedEnum;
use ReflectionClass;
use Spatie\TypeScriptTransformer\Structures\TransformedType;
use Spatie\TypeScriptTransformer\Transformers\EnumTransformer;
use Spatie\TypeScriptTransformer\TypeReflectors\ClassTypeReflector;

class EnumCollector extends DefaultCollector
{
public function getTransformedType(ReflectionClass $class): ?TransformedType
{
$transformers = array_map('get_class', $this->config->getTransformers());

if (! \in_array(EnumTransformer::class, $transformers, true)) {
return null;
}

$reflector = ClassTypeReflector::create($class);

if (! $reflector->getReflectionClass()->implementsInterface(BackedEnum::class)) {
return null;
}

$transformedType = $reflector->getType()
? $this->resolveAlreadyTransformedType($reflector)
: $this->resolveTypeViaTransformer($reflector);

if ($reflector->isInline()) {
$transformedType->name = null;
$transformedType->isInline = true;
}

return $transformedType;
}
}
26 changes: 26 additions & 0 deletions tests/Collectors/EnumCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use function PHPUnit\Framework\assertEquals;
use function PHPUnit\Framework\assertNotNull;
use Spatie\TypeScriptTransformer\Collectors\EnumCollector;
use Spatie\TypeScriptTransformer\Tests\FakeClasses\BackedEnumWithoutAnnotation;

use Spatie\TypeScriptTransformer\Transformers\EnumTransformer;
use Spatie\TypeScriptTransformer\TypeScriptTransformerConfig;

it('collects backed enums', function () {
$collector = new EnumCollector(
TypeScriptTransformerConfig::create()->transformers([
EnumTransformer::class,
])
);

$reflection = new ReflectionClass(BackedEnumWithoutAnnotation::class);
$transformedType = $collector->getTransformedType($reflection);

assertNotNull($transformedType);
assertEquals(
"'foo' | 'bar'",
$transformedType->transformed,
);
})->skip(version_compare(PHP_VERSION, '8.1', '<'), 'Enums are a PHP 8.1+ feature.');
9 changes: 9 additions & 0 deletions tests/FakeClasses/BackedEnumWithoutAnnotation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Spatie\TypeScriptTransformer\Tests\FakeClasses;

enum BackedEnumWithoutAnnotation: string
{
case FOO = 'foo';
case BAR = 'bar';
}

0 comments on commit b88bfe6

Please sign in to comment.