1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-07 05:36:54 +02:00

feat(serializers): add PhpArray Serialization #568

This commit is contained in:
Awilum
2021-08-13 23:10:47 +03:00
parent a15a5963cd
commit 83dc2781a1
5 changed files with 124 additions and 0 deletions

View File

@@ -0,0 +1,88 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype\Serializers;
use RuntimeException;
use function cache;
use function registry;
use function strings;
class PhpArray
{
/**
* Returns the PhpArray representation of a value.
*
* @param mixed $input The PHP value.
*
* @return string A PhpArray string representing the original PHP value.
*/
public function encode($input): string
{
try {
$data = "<?php\n return " . var_export($input, true) . ";\n";
} catch (Exception $e) {
throw new RuntimeException('Encoding PhpArray failed');
}
return $data;
}
/**
* Takes a PhpArray encoded string and converts it into a PHP variable.
*
* @param string $input A string containing PhpArray.
*
* @return mixed The PhpArray converted to a PHP value.
*/
public function decode(string $input)
{
$cache = registry()->get('flextype.settings.serializers.phparray.decode.cache');
$decode = static function (string $input) {
try {
$value = include $input;
} catch (Exception $e) {
throw new RuntimeException('Decoding PhpArray failed');
}
return $value;
};
if ($cache === true && registry()->get('flextype.settings.cache.enabled') === true) {
$key = $this->getCacheID($input);
if ($dataFromCache = cache()->get($key)) {
return $dataFromCache;
}
$data = $decode($input);
cache()->set($key, $data);
return $data;
}
return $decode($input);
}
/**
* Get Cache ID for phparray.
*
* @param string $input Input.
*
* @return string Cache ID.
*
* @access public
*/
public function getCacheID(string $input): string
{
return strings('phparray' . $input)->hash()->toString();
}
}

View File

@@ -14,6 +14,7 @@ use Flextype\Serializers\Json;
use Flextype\Serializers\Yaml;
use Flextype\Serializers\Frontmatter;
use Flextype\Serializers\Neon;
use Flextype\Serializers\PhpArray;
class Serializers
{
@@ -50,4 +51,12 @@ class Serializers
{
return new Neon();
}
/**
* Create a PhpArray instance.
*/
public function phparray(): PhpArray
{
return new PhpArray();
}
}

View File

@@ -455,6 +455,9 @@ serializers:
cache: true
encode:
flags: 1
phparray:
decode:
cache: true
# Parsers
#

View File

@@ -0,0 +1,5 @@
<?php
return array (
'title' => 'Foo',
'content' => 'Bar',
);

View File

@@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
test('test encode() method', function () {
$this->assertEquals(67, strings(serializers()->phparray()->encode(['title' => 'Foo', 'content' => 'Bar']))->length());
});
test('test decode() method', function () {
$this->assertEquals(['title' => 'Foo', 'content' => 'Bar'], serializers()->phparray()->decode(ROOT_DIR . '/tests/fixtures/serializers/phparray.php'));
});
test('test getCacheID() method', function () {
$string = strings(serializers()->phparray()->encode(['title' => 'Foo','content' => 'Bar']))->toString();
$cache_id = serializers()->phparray()
->getCacheID($string);
$this->assertEquals(32, strlen($cache_id));
$this->assertNotEquals($string, $cache_id);
});