Skip to content

Commit

Permalink
Improve JavascriptConverter codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jan 20, 2024
1 parent 24f1a63 commit 6ef1899
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
50 changes: 28 additions & 22 deletions src/Enum/JavascriptConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@

final class JavascriptConverter
{
private const EXPORT_NONE = 'none';
private const EXPORT_SIMPLE = 'export';
private const EXPORT_DEFAULT = 'exportDefault';
private const EXPORT_NONE = '';
private const EXPORT_SIMPLE = 'export ';
private const EXPORT_DEFAULT = 'export default ';

private function __construct(
private readonly bool $useSymbol,
Expand All @@ -26,7 +26,13 @@ private function __construct(

public static function new(): self
{
return new self(false, true, null, 2, self::EXPORT_NONE);
return new self(
useSymbol: false,
useImmutability: true,
propertyNameCasing: null,
indentSize: 2,
useExport: self::EXPORT_NONE
);
}

public function propertyNameCase(Closure $casing = null): self
Expand Down Expand Up @@ -166,8 +172,13 @@ public function convertToObject(string $enumClass, ?string $objectName = null):
{
$this->filterBackedEnum($enumClass);

$space = $this->indentSize > 0 ? str_repeat(' ', $this->indentSize) : '';
$eol = $this->indentSize > 0 ? "\n" : '';
$space = '';
$eol = '';
if (0 < $this->indentSize) {
$space = str_repeat(' ', $this->indentSize);
$eol = "\n";
}

$output = array_reduce(
$enumClass::cases(),
fn (string $output, BackedEnum $enum): string => $output.$space.$this->formatPropertyName($enum).': '.$this->formatPropertyValue($enum).','.$eol,
Expand All @@ -185,7 +196,7 @@ public function convertToObject(string $enumClass, ?string $objectName = null):
$output = "const $objectName = $output";
}

return $this->formatOutput($output);
return $this->useExport.$output;
}

/**
Expand All @@ -200,18 +211,23 @@ public function convertToClass(string $enumClass, string $className = ''): strin
{
$this->filterBackedEnum($enumClass);

$space = '';
$eol = '';
if (0 < $this->indentSize) {
$space = str_repeat(' ', $this->indentSize);
$eol = "\n";
}

$className = $this->sanitizeName($className, $enumClass);
$space = str_repeat(' ', $this->indentSize);
$eol = $this->indentSize > 0 ? "\n" : '';
$classBody = array_reduce(
$output = array_reduce(
$enumClass::cases(),
fn (string $output, BackedEnum $enum): string => $output.$space."static {$this->formatPropertyName($enum)} = new $className({$this->formatPropertyValue($enum)})$eol",
''
);

$output = 'class '.$className.' {'.$eol.$classBody.$eol.$space.'constructor(name) {'.$eol.$space.$space.'this.name = name'.$eol.$space.'}'.$eol.'}'.$eol;
$output = 'class '.$className.' {'.$eol.$output.$eol.$space.'constructor(name) {'.$eol.$space.$space.'this.name = name'.$eol.$space.'}'.$eol.'}'.$eol;

return $this->formatOutput($output);
return $this->useExport.$output;
}

private function formatPropertyName(BackedEnum $enum): string
Expand Down Expand Up @@ -260,14 +276,4 @@ private function sanitizeName(?string $className, string $enumClass): ?string

return (string) array_pop($parts);
}


public function formatOutput(string $output): string
{
return match ($this->useExport) {
self::EXPORT_DEFAULT => "export default $output",
self::EXPORT_SIMPLE => "export $output",
default => $output
};
}
}
5 changes: 2 additions & 3 deletions src/Enum/JavascriptConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function it_can_convert_to_a_javascript_class(): void
));

$expected = <<<JS
export class Foobar {
export default class Foobar {
static Ok = new Foobar(200)
static Redirection = new Foobar(302)
static NotFound = new Foobar(404)
Expand All @@ -134,10 +134,9 @@ public function it_can_convert_to_a_javascript_class(): void
JS;
$converter = JavascriptConverter::new()
->useImmutability()
->ignoreExport()
->ignoreSymbol()
->intendSize(4)
->useExport()
->useExportDefault()
->propertyNameCase(fn (string $name) => $pascalCase(strtolower(str_replace('HTTP_', '', $name))));

self::assertSame(
Expand Down

0 comments on commit 6ef1899

Please sign in to comment.