diff --git a/composer.json b/composer.json index 48abb305..b67f2d14 100644 --- a/composer.json +++ b/composer.json @@ -40,6 +40,7 @@ "prefer-stable": true, "require": { "php": ">=7.3", + "ext-json": "*", "symfony/polyfill-php80": "^1.15" }, "require-dev": { diff --git a/src/JsDumper/JsDumper.php b/src/JsDumper/JsDumper.php index 9703f052..d870ca64 100644 --- a/src/JsDumper/JsDumper.php +++ b/src/JsDumper/JsDumper.php @@ -163,10 +163,17 @@ private function generateReadables(string $enumFqcn): string continue; } + $flags = JSON_UNESCAPED_UNICODE + | JSON_UNESCAPED_SLASHES + | JSON_PRESERVE_ZERO_FRACTION + | JSON_THROW_ON_ERROR; + + $readable = json_encode($readable, $flags); + $readablesCode .= << + */ + +namespace Elao\Enum\Tests\Fixtures\Enum; + +use Elao\Enum\ReadableEnum; + +/** + * @method static NeedsEscapingEnum APOSTROPHE() + * @method static NeedsEscapingEnum FORWARD_SLASH() + */ +class NeedsEscapingEnum extends ReadableEnum +{ + public const APOSTROPHE = 'apostrophe'; + public const FORWARD_SLASH = 'forward_slash'; + + public static function values(): array + { + return [ + self::APOSTROPHE, + self::FORWARD_SLASH, + ]; + } + + public static function readables(): array + { + return [ + self::APOSTROPHE => '\'', + self::FORWARD_SLASH => '/', + ]; + } +} diff --git a/tests/Fixtures/JsDumper/expected/flagged_enum.js b/tests/Fixtures/JsDumper/expected/flagged_enum.js index 36988ec1..2c342ddb 100644 --- a/tests/Fixtures/JsDumper/expected/flagged_enum.js +++ b/tests/Fixtures/JsDumper/expected/flagged_enum.js @@ -8,12 +8,12 @@ class Permissions extends FlaggedEnum { static get readables() { return { - [Permissions.EXECUTE]: 'Execute', - [Permissions.WRITE]: 'Write', - [Permissions.READ]: 'Read', + [Permissions.EXECUTE]: "Execute", + [Permissions.WRITE]: "Write", + [Permissions.READ]: "Read", // Named masks - [Permissions.ALL]: 'All permissions', + [Permissions.ALL]: "All permissions", }; } } diff --git a/tests/Fixtures/JsDumper/expected/needs_escaping_readable_enum.js b/tests/Fixtures/JsDumper/expected/needs_escaping_readable_enum.js new file mode 100644 index 00000000..18408c9d --- /dev/null +++ b/tests/Fixtures/JsDumper/expected/needs_escaping_readable_enum.js @@ -0,0 +1,11 @@ +class NeedsEscapingEnum extends ReadableEnum { + static APOSTROPHE = 'apostrophe' + static FORWARD_SLASH = 'forward_slash' + + static get readables() { + return { + [NeedsEscapingEnum.APOSTROPHE]: "'", + [NeedsEscapingEnum.FORWARD_SLASH]: "/", + }; + } +} diff --git a/tests/Fixtures/JsDumper/expected/readable_enum.js b/tests/Fixtures/JsDumper/expected/readable_enum.js index fe60b54b..aca7282a 100644 --- a/tests/Fixtures/JsDumper/expected/readable_enum.js +++ b/tests/Fixtures/JsDumper/expected/readable_enum.js @@ -5,9 +5,9 @@ class Gender extends ReadableEnum { static get readables() { return { - [Gender.UNKNOW]: 'Unknown', - [Gender.MALE]: 'Male', - [Gender.FEMALE]: 'Female', + [Gender.UNKNOW]: "Unknown", + [Gender.MALE]: "Male", + [Gender.FEMALE]: "Female", }; } } diff --git a/tests/Unit/JsDumper/JsDumperTest.php b/tests/Unit/JsDumper/JsDumperTest.php index 9d01c937..4bc8a616 100644 --- a/tests/Unit/JsDumper/JsDumperTest.php +++ b/tests/Unit/JsDumper/JsDumperTest.php @@ -12,6 +12,7 @@ use Elao\Enum\JsDumper\JsDumper; use Elao\Enum\Tests\Fixtures\Enum\Gender; +use Elao\Enum\Tests\Fixtures\Enum\NeedsEscapingEnum; use Elao\Enum\Tests\Fixtures\Enum\Permissions; use Elao\Enum\Tests\Fixtures\Enum\SimpleEnum; use Elao\Enum\Tests\TestCase; @@ -148,6 +149,11 @@ public function provide testDumpEnumClass data(): iterable 'expectationFilePath' => 'readable_enum.js', ]; + yield 'readable enum which requires escaping' => [ + 'enumClass' => NeedsEscapingEnum::class, + 'expectationFilePath' => 'needs_escaping_readable_enum.js', + ]; + yield 'flagged enum' => [ 'enumClass' => Permissions::class, 'expectationFilePath' => 'flagged_enum.js',