Skip to content

Commit

Permalink
Merge branch 'naxvog-custom-encoder-decoder'
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenvanassche committed Feb 19, 2024
2 parents add3623 + 1ec9ada commit 19ab2dc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ return [
],
],

/*
* The encoder and decoder will determine how settings are stored and
* retrieved in the database. By default, `json_encode` and `json_decode`
* are used.
*/
'encoder' => null,
'decoder' => null,

/*
* The contents of settings classes can be cached through your application,
* settings will be stored within a provided Laravel store and can have an
Expand Down Expand Up @@ -734,6 +742,18 @@ public function up(): void

Of course, you can use these methods when using `inGroup` migration operations.

### Custom encoders and decoders

It is possible to define custom encoders and decoders instead of the built-in `json_encode` and `json_decode` ones by
changing the package configuration like so:

```php
...
'encoder' => fn($value): string => str_rot13(json_encode($value)),
'decoder' => fn(string $payload, bool $associative) => json_decode(str_rot13($payload), $associative),
...
```

### Faking settings classes

In tests, it is sometimes desired that some settings classes can be quickly used with values different from the default ones you've written in your migrations. That's why you can fake settings. Faked settings classes will be registered in the container. And you can overwrite some or all the properties in the settings class:
Expand Down
8 changes: 8 additions & 0 deletions config/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@
],
],

/*
* The encoder and decoder will determine how settings are stored and
* retrieved in the database. By default, `json_encode` and `json_decode`
* are used.
*/
'encoder' => null,
'decoder' => null,

/*
* The contents of settings classes can be cached through your application,
* settings will be stored within a provided Laravel store and can have an
Expand Down
22 changes: 18 additions & 4 deletions src/SettingsRepositories/DatabaseSettingsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getPropertiesInGroup(string $group): array
->where('group', $group)
->get(['name', 'payload'])
->mapWithKeys(function (object $object) {
return [$object->name => json_decode($object->payload, true)];
return [$object->name => $this->decode($object->payload, true)];
})
->toArray();
}
Expand All @@ -48,15 +48,15 @@ public function getPropertyPayload(string $group, string $name)
->first('payload')
->toArray();

return json_decode($setting['payload']);
return $this->decode($setting['payload']);
}

public function createProperty(string $group, string $name, $payload): void
{
$this->getBuilder()->create([
'group' => $group,
'name' => $name,
'payload' => json_encode($payload),
'payload' => $this->encode($payload),
'locked' => false,
]);
}
Expand All @@ -67,7 +67,7 @@ public function updatePropertiesPayload(string $group, array $properties): void
return [
'group' => $group,
'name' => $name,
'payload' => json_encode($payload),
'payload' => $this->encode($payload),
];
})->values()->toArray();

Expand Down Expand Up @@ -123,4 +123,18 @@ public function getBuilder(): Builder

return $model->newQuery();
}

protected function encode(mixed $value): mixed
{
$encoder = config('settings.encoder') ?? fn ($value) => json_encode($value);

return $encoder($value);
}

protected function decode(string $payload, bool $associative = false): mixed
{
$decoder = config('settings.decoder') ?? fn ($payload, $associative) => json_decode($payload, $associative);

return $decoder($payload, $associative);
}
}
16 changes: 16 additions & 0 deletions tests/SettingsRepositories/DatabaseSettingsRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,22 @@
expect($this->repository->getPropertyPayload('test', 'e'))->toEqual(69);
});

it('can utilize custom encoders', function() {
config()->set('settings.encoder', fn($value) => str_rot13(json_encode($value)));

$this->repository->createProperty('test', 'a', 'Alpha');

expect(SettingsProperty::all()->first()->payload)->toEqual('"Nycun"');
});

it('can utilize custom decoders', function() {
config()->set('settings.decoder', fn($payload, $assoc) => json_decode(str_rot13($payload), $assoc));

$this->repository->createProperty('test', 'a', 'Nycun');

expect($this->repository->getPropertyPayload('test', 'a'))->toEqual('Alpha');
});

it('can delete a property', function () {
$this->repository->createProperty('test', 'a', 'Alpha');

Expand Down

0 comments on commit 19ab2dc

Please sign in to comment.