-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
642f32f
commit 36cb676
Showing
4 changed files
with
110 additions
and
6 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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Cache\Db; | ||
|
||
use function serialize; | ||
use function unserialize; | ||
|
||
final class PhpSerializer implements SerializerInterface | ||
{ | ||
public function serialize(mixed $value): string | ||
{ | ||
return serialize($value); | ||
} | ||
|
||
/** | ||
* @param string $value | ||
* @return mixed | ||
* | ||
* @psalm-suppress MixedArgumentTypeCoercion | ||
*/ | ||
public function restore(string $value): mixed | ||
{ | ||
return unserialize($value); | ||
} | ||
} |
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 Yiisoft\Cache\Db; | ||
|
||
interface SerializerInterface | ||
{ | ||
public function serialize(mixed $value): string; | ||
|
||
public function restore(string $value): 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Cache\Db\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use stdClass; | ||
use Yiisoft\Cache\Db\PhpSerializer; | ||
|
||
use function pi; | ||
|
||
use const PHP_INT_MAX; | ||
|
||
final class PhpSerializerTest extends TestCase | ||
{ | ||
public static function serializeDataProvider(): array | ||
{ | ||
$object = new stdClass(); | ||
$object->foo = 'bar'; | ||
$object->bar = 'foo'; | ||
|
||
return [ | ||
[ | ||
true, | ||
], | ||
[ | ||
PHP_INT_MAX, | ||
], | ||
[ | ||
pi(), | ||
], | ||
[ | ||
'string', | ||
], | ||
[ | ||
[ | ||
'key' => 'value', | ||
'foo' => 'bar', | ||
], | ||
], | ||
[ | ||
$object, | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider serializeDataProvider | ||
* @param mixed $data | ||
* @return void | ||
*/ | ||
public function testSerialize(mixed $data): void | ||
{ | ||
$serializer = new PhpSerializer(); | ||
$result = $serializer->serialize($data); | ||
|
||
if (is_object($data)) { | ||
self::assertEquals($data, $serializer->restore($result)); | ||
} else { | ||
self::assertSame($data, $serializer->restore($result)); | ||
} | ||
} | ||
} |