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 66f8b8c
Showing 1 changed file with 28 additions and 22 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 defauit ';

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
};
}
}

0 comments on commit 66f8b8c

Please sign in to comment.