Skip to content

Commit

Permalink
Merge pull request #7 from WyriHaximus/json_serializable
Browse files Browse the repository at this point in the history
Any JsonSerializable object will now be turned into an array
  • Loading branch information
WyriHaximus committed Dec 1, 2018
2 parents 6936b89 + b55701d commit ca1924d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 35 deletions.
8 changes: 8 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ function formatValue($value): string
function normalizeContext(array $context): array
{
foreach ($context as $index => $value) {
if ($value instanceof \JsonSerializable) {
$value = $value->jsonSerialize();
}

if (\is_array($value)) {
$context[$index] = normalizeContext($value);
continue;
Expand All @@ -78,6 +82,10 @@ function normalizeContext(array $context): array
function normalizeContextWithFormatValue(array $context): array
{
foreach ($context as $index => $value) {
if ($value instanceof \JsonSerializable) {
$value = $value->jsonSerialize();
}

if (\is_array($value)) {
$context[$index] = normalizeContextWithFormatValue($value);
continue;
Expand Down
22 changes: 22 additions & 0 deletions tests/NormalizeContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ public function provideContexts()
],
],
];

yield [
[
[
'json_serializable' => new class() implements \JsonSerializable {
public function jsonSerialize()
{
return [
'foo' => 'bar',
];
}
},
],
],
[
[
'json_serializable' => [
'foo' => 'bar',
],
],
],
];
}

/**
Expand Down
36 changes: 1 addition & 35 deletions tests/NormalizeContextWithFormatValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,7 @@ final class NormalizeContextWithFormatValueTest extends TestCase
{
public function provideContexts()
{
yield [
[],
[],
];

yield [
[
[],
],
[
[],
],
];

yield [
[
'stdout' => STDOUT,
],
[
'stdout' => '[resource] (stream)',
],
];

yield [
[
[
'stdout' => STDOUT,
],
],
[
[
'stdout' => '[resource] (stream)',
],
],
];
yield from (new NormalizeContextTest())->provideContexts();

yield [
[
Expand Down

0 comments on commit ca1924d

Please sign in to comment.