Skip to content

Commit e96b3fa

Browse files
committed
Added settings driven custom encoder/decoder
1 parent 81d4bdc commit e96b3fa

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,14 @@ return [
140140
],
141141
],
142142

143+
/*
144+
* The encoder and decoder will determine how settings are stored and
145+
* retrieved in the database. By default, `json_encode` and `json_decode`
146+
* are used.
147+
*/
148+
'encoder' => null,
149+
'decoder' => null,
150+
143151
/*
144152
* The contents of settings classes can be cached through your application,
145153
* settings will be stored within a provided Laravel store and can have an
@@ -734,6 +742,18 @@ public function up(): void
734742

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

745+
### Custom encoders and decoders
746+
747+
It is possible to define custom encoders and decoders instead of the built-in `json_encode` and `json_decode` ones by
748+
changing the package configuration like so:
749+
750+
```php
751+
...
752+
'encoder' => fn($value): string => str_rot13(json_encode($value)),
753+
'decoder' => fn(string $payload, bool $associative) => json_decode(str_rot13($payload), $associative),
754+
...
755+
```
756+
737757
### Faking settings classes
738758

739759
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:

config/settings.php

+8
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@
4747
],
4848
],
4949

50+
/*
51+
* The encoder and decoder will determine how settings are stored and
52+
* retrieved in the database. By default, `json_encode` and `json_decode`
53+
* are used.
54+
*/
55+
'encoder' => null,
56+
'decoder' => null,
57+
5058
/*
5159
* The contents of settings classes can be cached through your application,
5260
* settings will be stored within a provided Laravel store and can have an

src/SettingsRepositories/DatabaseSettingsRepository.php

+18-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function getPropertiesInGroup(string $group): array
2727
->where('group', $group)
2828
->get(['name', 'payload'])
2929
->mapWithKeys(function (object $object) {
30-
return [$object->name => json_decode($object->payload, true)];
30+
return [$object->name => $this->decode($object->payload, true)];
3131
})
3232
->toArray();
3333
}
@@ -48,15 +48,15 @@ public function getPropertyPayload(string $group, string $name)
4848
->first('payload')
4949
->toArray();
5050

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

5454
public function createProperty(string $group, string $name, $payload): void
5555
{
5656
$this->getBuilder()->create([
5757
'group' => $group,
5858
'name' => $name,
59-
'payload' => json_encode($payload),
59+
'payload' => $this->encode($payload),
6060
'locked' => false,
6161
]);
6262
}
@@ -67,7 +67,7 @@ public function updatePropertiesPayload(string $group, array $properties): void
6767
return [
6868
'group' => $group,
6969
'name' => $name,
70-
'payload' => json_encode($payload),
70+
'payload' => $this->encode($payload),
7171
];
7272
})->values()->toArray();
7373

@@ -123,4 +123,18 @@ public function getBuilder(): Builder
123123

124124
return $model->newQuery();
125125
}
126+
127+
protected function encode(mixed $value): mixed
128+
{
129+
$encoder = config('settings.encoder') ?? fn ($value) => json_encode($value);
130+
131+
return $encoder($value);
132+
}
133+
134+
protected function decode(string $payload, bool $associative = false): mixed
135+
{
136+
$decoder = config('settings.decoder') ?? fn ($payload, $associative) => json_decode($payload, $associative);
137+
138+
return $decoder($payload, $associative);
139+
}
126140
}

tests/SettingsRepositories/DatabaseSettingsRepositoryTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,22 @@
139139
expect($this->repository->getPropertyPayload('test', 'e'))->toEqual(69);
140140
});
141141

142+
it('can utilize custom encoders', function() {
143+
config()->set('settings.encoder', fn($value) => str_rot13(json_encode($value)));
144+
145+
$this->repository->createProperty('test', 'a', 'Alpha');
146+
147+
expect(SettingsProperty::all()->first()->payload)->toEqual('"Nycun"');
148+
});
149+
150+
it('can utilize custom decoders', function() {
151+
config()->set('settings.decoder', fn($payload, $assoc) => json_decode(str_rot13($payload), $assoc));
152+
153+
$this->repository->createProperty('test', 'a', 'Nycun');
154+
155+
expect($this->repository->getPropertyPayload('test', 'a'))->toEqual('Alpha');
156+
});
157+
142158
it('can delete a property', function () {
143159
$this->repository->createProperty('test', 'a', 'Alpha');
144160

0 commit comments

Comments
 (0)