Skip to content

Commit

Permalink
Improve Info Test
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jan 22, 2024
1 parent 6980155 commit 5e84dee
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/Enum/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,13 @@ public static function nameOf(string|int $value): ?string
default => $res,
};
}

/**
* Convert the Enum into a Javascript structure.
*/
public static function toJavascript(): string
{
return JavascriptConverter::new()
->convertToObject(static::class); /* @phpstan-ignore-line */
}
}
20 changes: 20 additions & 0 deletions src/Enum/InfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ public function it_can_get_information_from_a_pure_enumeration(): void
self::assertSame(['Top', 'Down', 'Left', 'Right'], Direction::names());
self::assertSame([], Direction::values());
self::assertNull(Direction::nameOf('Up'));
$expected = <<<JS
const Direction = Object.freeze({
Top: Symbol(0),
Down: Symbol(1),
Left: Symbol(2),
Right: Symbol(3),
})
JS;
self::assertSame($expected, Direction::toJavascript());
}

#[Test]
Expand All @@ -31,6 +41,16 @@ public function it_can_get_information_from_a_backed_enumeration(): void
self::assertSame(['North', 'South', 'East', 'West'], Cardinal::names());
self::assertSame(['north', 'south', 'east', 'west'], Cardinal::values());
self::assertSame('West', Cardinal::nameOf('west'));
$expected = <<<JS
const Cardinal = Object.freeze({
North: "north",
South: "south",
East: "east",
West: "west",
})
JS;
self::assertSame($expected, Cardinal::toJavascript());
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/Enum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,19 @@ Gather information regarding the current Enum via **public static methods**. Thi
- the possible values for the Enum with the `values` method;
- the `associative` method which returns an associative array contains the string name and their respective values;
- the `nameOf` which returns the name associated with a specific `value`
- the `toJavascript` which returns a Javascript structure equivalent to the current Enum.(see `JavascriptConverter` for more details)

```php
<?php

HttpMethod::size(); //returns the number of cases
HttpMethod::isBacked();
HttpMethod::isPure(); // returns the inverse of the `isBacked` method
HttpMethod::isPure(); // returns the inverse of the `isBacked` method
HttpMethod::names(); // returns a list of all the names in the enumeration
HttpMethod::values(); // returns a list of all the names in the enumeration
HttpMethod::nameOf(404); // returns the name associated with the given value
// or null if it does not exist for the submitted value.
HttpMethod::toJavascript(); // returns a Javascript structure equivalent
```

You need the `Bakame\Aide\Enum\Info` trait to expose the new API.
Expand Down Expand Up @@ -198,8 +200,11 @@ enum HttpMethod: string
### Converting the Enum into a Javascript structure

The `JavascriptConverter` enables converting your PHP Enum into an equivalent structure in Javascript.
This is the mechanism used to implement the `Info::toJavascript` method. You can still override
the method and return a more fine-tuned representation that suite your constraints better.

Because there are two (2) ways to create an Enum like structure in Javascript, the class provides
two (2) methods to allow the conversion.
two (2) methods to allow the conversion.

In both cases, the conversion is configurable via wither methods to control the formatting and the
Javascript structure properties.
Expand Down

0 comments on commit 5e84dee

Please sign in to comment.