diff --git a/src/flextype/core/Serializers/PhpArray.php b/src/flextype/core/Serializers/PhpArray.php new file mode 100644 index 00000000..0be68d83 --- /dev/null +++ b/src/flextype/core/Serializers/PhpArray.php @@ -0,0 +1,88 @@ +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(); + } +} diff --git a/src/flextype/core/Serializers/Serializers.php b/src/flextype/core/Serializers/Serializers.php index 3105383d..ac0f6b47 100644 --- a/src/flextype/core/Serializers/Serializers.php +++ b/src/flextype/core/Serializers/Serializers.php @@ -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(); + } } diff --git a/src/flextype/settings.yaml b/src/flextype/settings.yaml index ee4e9b30..3b8d05fb 100644 --- a/src/flextype/settings.yaml +++ b/src/flextype/settings.yaml @@ -455,6 +455,9 @@ serializers: cache: true encode: flags: 1 + phparray: + decode: + cache: true # Parsers # diff --git a/tests/fixtures/serializers/phparray.php b/tests/fixtures/serializers/phparray.php new file mode 100644 index 00000000..aa10812b --- /dev/null +++ b/tests/fixtures/serializers/phparray.php @@ -0,0 +1,5 @@ + 'Foo', + 'content' => 'Bar', +); \ No newline at end of file diff --git a/tests/src/flextype/core/Serializers/PhpArrayTest.php b/tests/src/flextype/core/Serializers/PhpArrayTest.php new file mode 100644 index 00000000..c46042e0 --- /dev/null +++ b/tests/src/flextype/core/Serializers/PhpArrayTest.php @@ -0,0 +1,19 @@ +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); +}); \ No newline at end of file