Skip to content

Commit

Permalink
Implements \Jsonserialize interface
Browse files Browse the repository at this point in the history
  • Loading branch information
overclokk authored Jul 7, 2024
1 parent 0396824 commit e8cd673
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 31 deletions.
24 changes: 11 additions & 13 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,14 @@
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
*
* @template-implements \ItalyStrap\Config\ConfigInterface<TKey,TValue>
* @template-extends \ArrayObject<TKey,TValue>
* @psalm-suppress DeprecatedInterface
*/
class Config extends ArrayObject implements ConfigInterface
class Config extends ArrayObject implements ConfigInterface, \JsonSerializable
{
/**
* @use \ItalyStrap\Config\ArrayObjectTrait<TKey,TValue>
Expand Down Expand Up @@ -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);

Check warning on line 152 in src/Config.php

View workflow job for this annotation

GitHub Actions / 🐘 Test on PHP 8.0

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ */ public function toJson() : string { - return (string) \json_encode($this->toArray(), JSON_THROW_ON_ERROR); + return \json_encode($this->toArray(), JSON_THROW_ON_ERROR); } public function jsonSerialize() : array {

Check warning on line 152 in src/Config.php

View workflow job for this annotation

GitHub Actions / 🐘 Test on PHP 8.1

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ */ public function toJson() : string { - return (string) \json_encode($this->toArray(), JSON_THROW_ON_ERROR); + return \json_encode($this->toArray(), JSON_THROW_ON_ERROR); } public function jsonSerialize() : array {
}

public function jsonSerialize(): array
{
return $this->toArray();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/ConfigThemeMods.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
*
* @template-implements \ItalyStrap\Config\ConfigInterface<TKey,TValue>
* @psalm-suppress DeprecatedInterface
* @deprecated No alternative available yet
*/
#[\Deprecated]
class ConfigThemeMods implements ConfigInterface
{
/**
Expand Down
52 changes: 34 additions & 18 deletions tests/unit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
}

0 comments on commit e8cd673

Please sign in to comment.