Skip to content

Commit

Permalink
Added 'Customizing key format' section to ReadMe
Browse files Browse the repository at this point in the history
  • Loading branch information
EXayer committed Mar 1, 2024
1 parent 22d1c46 commit d496ec7
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ $result = iterator_to_array($planets);

Some VDFs are known to contain duplicate keys.
To keep the result structure the same as the VDF and since the parser is generator based (not keeping in memory pairs)
the duplicated key will be modified - the counter will be concatenated to the end (e.g. 'key__2').

the duplicated key will be modified - the counter will be concatenated to the end (e.g. `key__[2]`).

```php
$vdf = '{
Expand All @@ -114,16 +113,45 @@ $result = iterator_to_array(VdfConverter::fromString($vdf));
// "mercury" => [
// "distance" => "58"
// "velocity" => "35"
// "distance__2" => "92"
// "distance__[2]" => "92"
// ]
// "mercury__2" => [
// "mercury__[2]" => [
// "distance" => "108"
// ]
// "earth" => [
// "distance" => "149"
// ]
// ]
```

#### Customizing key format

If you want to customize the formatting key process, you can create your own custom formatter. A formatter is any class that implements `EXayer\VdfConverter\UniqueKey\Formatter`.

This is what that interface looks like.

```php
namespace EXayer\VdfConverter\UniqueKey;

interface Formatter
{
public function buildKeyName(string $key, int $index): string;
}
```

After creating your formatter, you can specify its class name in the `uniqueKeyFormatter` method of the `EXayer\VdfConverter\VdfConverterConfig` object. The config can be passed as second argument to any `from` builder method. Your formatter will then be used by default for all duplicate key handling calls.

You can also specify a signer for a specific webhook call:

```php
$config = (new VdfConverterConfig())
->uniqueKeyFormatter(YourCustomFormatter::class);

$iterator = VdfConverter::fromString($vdf, $config);
```

**Warning**: Do not create formatters that create a key like `__2`, as some VDFs may have keys in this format.

## Testing

```bash
Expand Down

0 comments on commit d496ec7

Please sign in to comment.