From e8cd673f983b520c980a498e82e2916c8b234e9c Mon Sep 17 00:00:00 2001 From: Enea Scerba Date: Sun, 7 Jul 2024 15:44:07 +0200 Subject: [PATCH] Implements \Jsonserialize interface --- src/Config.php | 24 +++++++++--------- src/ConfigThemeMods.php | 2 ++ tests/unit/ConfigTest.php | 52 +++++++++++++++++++++++++-------------- 3 files changed, 47 insertions(+), 31 deletions(-) diff --git a/src/Config.php b/src/Config.php index 0357aba..05afbea 100644 --- a/src/Config.php +++ b/src/Config.php @@ -9,12 +9,6 @@ use ItalyStrap\Storage\SetMultipleStoreTrait; /** - * @todo Immutable: https://github.com/jkoudys/immutable.php - * @todo Maybe some ideas iterator: https://github.com/clean/data/blob/master/src/Collection.php - * @todo Maybe some ideas json to array: https://github.com/Radiergummi/libconfig/blob/master/src/Libconfig/Config.php - * @todo Maybe some ideas: https://www.simonholywell.com/post/2017/04/php-and-immutability-part-two/ - * @todo Maybe add recursion? https://www.php.net/manual/en/class.arrayobject.php#123572 - * * @template TKey as array-key * @template TValue * @@ -22,7 +16,7 @@ * @template-extends \ArrayObject * @psalm-suppress DeprecatedInterface */ -class Config extends ArrayObject implements ConfigInterface +class Config extends ArrayObject implements ConfigInterface, \JsonSerializable { /** * @use \ItalyStrap\Config\ArrayObjectTrait @@ -142,21 +136,25 @@ public function merge(...$array_to_merge): Config return $this; } - /** - * @inheritDoc - */ public function toArray(): array { return $this->getArrayCopy(); } /** - * @inheritDoc - * @throws \JsonException + * @deprecated This is a soft deprecation, I'm working on a different solution to dump a Json format, + * in the meantime you can use: + * (string)\json_encode(mew Config(), JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); + * @psalm-suppress RedundantCastGivenDocblockType */ public function toJson(): string { - return \strval(\json_encode($this->toArray(), JSON_THROW_ON_ERROR)); + return (string)\json_encode($this->toArray(), JSON_THROW_ON_ERROR); + } + + public function jsonSerialize(): array + { + return $this->toArray(); } /** diff --git a/src/ConfigThemeMods.php b/src/ConfigThemeMods.php index d4c6b2b..5411567 100644 --- a/src/ConfigThemeMods.php +++ b/src/ConfigThemeMods.php @@ -26,7 +26,9 @@ * * @template-implements \ItalyStrap\Config\ConfigInterface * @psalm-suppress DeprecatedInterface + * @deprecated No alternative available yet */ +#[\Deprecated] class ConfigThemeMods implements ConfigInterface { /** diff --git a/tests/unit/ConfigTest.php b/tests/unit/ConfigTest.php index 867d330..1c15f32 100644 --- a/tests/unit/ConfigTest.php +++ b/tests/unit/ConfigTest.php @@ -662,22 +662,38 @@ public function itShouldBeDelimiterOk(): void $this->assertSame('default-value', $sut->get('testsub-test', 'default-value'), ''); } - public function testSubArrayAccess(): void - { - $sut = $this->makeInstance( - [ - 'key' => [ - 'sub-key' => 'value', - ], - ] - ); - - $this->assertTrue($sut->has('key.sub-key')); - $this->assertSame('value', $sut->get('key.sub-key')); - $this->assertSame('value', $sut['key.sub-key']); - $this->assertSame('value', $sut['key']['sub-key']); -// $this->assertSame(null, $sut['key']['not-exists']); - $this->assertSame(null, $sut['key.not-exists']); - $this->assertSame(null, $sut['key.not-exists.not-exists']); - } + public function testSubArrayAccess(): void + { + $sut = $this->makeInstance( + [ + 'key' => [ + 'sub-key' => 'value', + ], + ] + ); + + $this->assertTrue($sut->has('key.sub-key')); + $this->assertSame('value', $sut->get('key.sub-key')); + $this->assertSame('value', $sut['key.sub-key']); + $this->assertSame('value', $sut['key']['sub-key']); +// $this->assertSame(null, $sut['key']['not-exists']); + $this->assertSame(null, $sut['key.not-exists']); + $this->assertSame(null, $sut['key.not-exists.not-exists']); + } + + public function testJsonSerialize(): void + { + $sut = $this->makeInstance( + [ + 'key' => [ + 'sub-key' => 'value', + ], + ] + ); + + $this->assertJsonStringEqualsJsonString( + \json_encode($sut->toArray()), + \json_encode($sut) + ); + } }