generated from rawilk/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom value serializer support (#31)
- Loading branch information
Showing
9 changed files
with
137 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rawilk\Settings\Contracts; | ||
|
||
interface ValueSerializer | ||
{ | ||
public function serialize($value): string; | ||
|
||
public function unserialize(string $serialized): mixed; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rawilk\Settings\Support\ValueSerializers; | ||
|
||
use Rawilk\Settings\Contracts\ValueSerializer as ValueSerializerContract; | ||
|
||
class JsonValueSerializer implements ValueSerializerContract | ||
{ | ||
public function serialize($value): string | ||
{ | ||
return json_encode($value, JSON_THROW_ON_ERROR); | ||
} | ||
|
||
public function unserialize(string $serialized): mixed | ||
{ | ||
return json_decode($serialized, true, 512, JSON_THROW_ON_ERROR); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rawilk\Settings\Support\ValueSerializers\JsonValueSerializer; | ||
|
||
beforeEach(function () { | ||
$this->serializer = new JsonValueSerializer; | ||
}); | ||
|
||
it('serializes values as json', function (mixed $value, string $expected) { | ||
expect($this->serializer->serialize($value))->toBe($expected); | ||
})->with([ | ||
[1, '1'], | ||
['1', '"1"'], | ||
[true, 'true'], | ||
[false, 'false'], | ||
[0, '0'], | ||
[null, 'null'], | ||
[1.1, '1.1'], | ||
[['array' => 'array'], '{"array":"array"}'], | ||
[[1, '2', 3], '[1,"2",3]'], | ||
[(object) ['a' => 'b'], '{"a":"b"}'], | ||
]); | ||
|
||
it('unserializes json values', function (string $value, mixed $expected) { | ||
expect($this->serializer->unserialize($value))->toBe($expected); | ||
})->with([ | ||
['1', 1], | ||
['"1"', '1'], | ||
['true', true], | ||
['false', false], | ||
['0', 0], | ||
['null', null], | ||
['1.1', 1.1], | ||
['{"array":"array"}', ['array' => 'array']], | ||
['{"a":"b"}', ['a' => 'b']], | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters