From fe13253b6cbc5bc03e413076c5f22aff9ef8464f Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Sat, 20 Jan 2024 07:54:19 +0100 Subject: [PATCH] Improve JavascriptConverter documentation --- src/Enum/README.md | 81 ++++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 35 deletions(-) diff --git a/src/Enum/README.md b/src/Enum/README.md index 0008c1f..03da627 100644 --- a/src/Enum/README.md +++ b/src/Enum/README.md @@ -211,38 +211,44 @@ enum HttpStatusCode: int } ``` -It can be converted into an object: +It can be converted into an object using the `convertToObject` method: ```php use Bakame\Aide\Enum\JavascriptConverter; echo JavascriptConverter::new()->convertToObject(HttpStatusCode::class); -// will return the following - -// Object.freeze({ -// HTTP_OK: 200, -// HTTP_REDIRECTION: 302, -// HTTP_NOT_FOUND: 404, -// HTTP_SERVER_ERROR: 500, -// }) ``` -or into a class: +will produce the following javascript code snippet: + +```javascript +Object.freeze({ + HTTP_OK: 200, + HTTP_REDIRECTION: 302, + HTTP_NOT_FOUND: 404, + HTTP_SERVER_ERROR: 500, +}) +``` + +conversely using `convertToClass` as follows: ```php echo JavascriptConverter::new()->convertToClass(HttpStatusCode::class); -// will return the following - -// class HttpStatusCode { -// static HTTP_OK = new HttpStatusCode(200) -// static HTTP_REDIRECTION = new HttpStatusCode(302) -// static HTTP_NOT_FOUND = new HttpStatusCode(404) -// static HTTP_SERVER_ERROR = new HttpStatusCode(500) -// -// constructor(name) { -// this.name = name -// } -// } +``` + +will produce the following javascript code snippet: + +```javascript +class HttpStatusCode { + static HTTP_OK = new HttpStatusCode(200) + static HTTP_REDIRECTION = new HttpStatusCode(302) + static HTTP_NOT_FOUND = new HttpStatusCode(404) + static HTTP_SERVER_ERROR = new HttpStatusCode(500) + + constructor(name) { + this.name = name + } +} ``` Of course there are ways to improve the output depending on your use case you can @@ -251,7 +257,7 @@ Of course there are ways to improve the output depending on your use case you ca - ignore or use Javascript `export` or `export default`; - change the class name or add and/or change the object variable name; - use `Symbol` when declaring the object property value; -- define indentation spaces; +- define indentation spaces and thus end of line; Here's a more advance usage of the converter to highlight how you can configure it. @@ -260,29 +266,34 @@ Here's a more advance usage of the converter to highlight how you can configure use Bakame\Aide\Enum\JavascriptConverter; use Illuminate\Support\Str; -$pascalCase = fn (string $word): string => Str::ucfirst(Str::camel($word))); - $converter = JavascriptConverter::new() ->useImmutability() ->useExportDefault() ->useSymbol() ->intendSize(4) - ->propertyNameCase(fn (string $name) => $pascalCase(str_replace('HTTP_', '', $name))); + ->propertyNameCase( + fn (string $name) => Str::of($name)->replace('HTTP_', '')->lower()->studly()->toString() + ); echo $converter->convertToObject(HttpStatusCode::class); +``` -// will return the following -// export default Object.freeze({ -// Ok: Symbol(200), -// Redirection: Symbol(302), -// NotFound: Symbol(404), -// ServerError: Symbol(500), -// }) +will return the following Javascript code: + +```javascript +export default Object.freeze({ + Ok: Symbol(200), + Redirection: Symbol(302), + NotFound: Symbol(404), + ServerError: Symbol(500), +}) ``` The converter will not store the resulting string into a Javascriot file as this part is -left to the discretion of the implementor. There are several ways to do so using vanilla -PHP or more robust and battle tested packages. +left to the discretion of the implementor. There are several ways to do so: + +- using vanilla PHP with `file_put_contents` or `SplFileObject` +- using more robust and battle tested packages you can find on packagist for instance. ## Credits