Skip to content

Commit

Permalink
Improve JavascriptConverter documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Jan 20, 2024
1 parent c94d012 commit fe13253
Showing 1 changed file with 46 additions and 35 deletions.
81 changes: 46 additions & 35 deletions src/Enum/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

Expand All @@ -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

Expand Down

0 comments on commit fe13253

Please sign in to comment.