Skip to content

Commit

Permalink
bug #152 handle readable enums that contain values which need escapin…
Browse files Browse the repository at this point in the history
…g in javascript (bendavies)

This PR was merged into the 1.x-dev branch.

Discussion
----------

handle readable enums that contain values which need escaping in javascript

Fixes #151, and see discussion there.

Commits
-------

fea6c0d handle readable enums that contain values which need escaping in javascript
  • Loading branch information
ogizanagi committed Aug 26, 2021
2 parents 345586f + fea6c0d commit 075003f
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 9 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"prefer-stable": true,
"require": {
"php": ">=7.3",
"ext-json": "*",
"symfony/polyfill-php80": "^1.15"
},
"require-dev": {
Expand Down
18 changes: 16 additions & 2 deletions src/JsDumper/JsDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 .=
<<<JS
[{$shortName}.{$constant}]: '{$readable}',
[{$shortName}.{$constant}]: {$readable},
JS;
}

Expand All @@ -177,10 +184,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 .=
<<<JS
[{$shortName}.{$constant}]: '{$readable}',
[{$shortName}.{$constant}]: {$readable},
JS;
}
}
Expand Down
39 changes: 39 additions & 0 deletions tests/Fixtures/Enum/NeedsEscapingEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the "elao/enum" package.
*
* Copyright (C) Elao
*
* @author Elao <contact@elao.com>
*/

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 => '/',
];
}
}
8 changes: 4 additions & 4 deletions tests/Fixtures/JsDumper/expected/flagged_enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};
}
}
11 changes: 11 additions & 0 deletions tests/Fixtures/JsDumper/expected/needs_escaping_readable_enum.js
Original file line number Diff line number Diff line change
@@ -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]: "/",
};
}
}
6 changes: 3 additions & 3 deletions tests/Fixtures/JsDumper/expected/readable_enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
};
}
}
6 changes: 6 additions & 0 deletions tests/Unit/JsDumper/JsDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
Expand Down

0 comments on commit 075003f

Please sign in to comment.