Skip to content

Commit eee7eb4

Browse files
committed
Create JsonSerializableStringifier
1 parent d20cb83 commit eee7eb4

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Respect/Stringifier.
5+
*
6+
* (c) Henrique Moody <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the "LICENSE.md"
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Respect\Stringifier\Stringifiers;
15+
16+
use Respect\Stringifier\Quoter;
17+
use Respect\Stringifier\Stringifier;
18+
use JsonSerializable;
19+
20+
/**
21+
* Converts an instance of JsonSerializable into a string.
22+
*
23+
* @author Henrique Moody <[email protected]>
24+
*/
25+
final class JsonSerializableStringifier implements Stringifier
26+
{
27+
/**
28+
* @var Stringifier
29+
*/
30+
private $stringifier;
31+
32+
/**
33+
* @var Quoter
34+
*/
35+
private $quoter;
36+
37+
/**
38+
* Initializes the stringifier.
39+
*
40+
* @param Stringifier $stringifier
41+
* @param Quoter $quoter
42+
*/
43+
public function __construct(Stringifier $stringifier, Quoter $quoter)
44+
{
45+
$this->stringifier = $stringifier;
46+
$this->quoter = $quoter;
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function stringify($raw, int $depth): ?string
53+
{
54+
if (!$raw instanceof JsonSerializable) {
55+
return null;
56+
}
57+
58+
return $this->quoter->quote(
59+
sprintf(
60+
'[json-serializable] (%s: %s)',
61+
get_class($raw),
62+
$this->stringifier->stringify($raw->jsonSerialize(), $depth + 1)
63+
),
64+
$depth
65+
);
66+
}
67+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Respect/Stringifier.
5+
*
6+
* (c) Henrique Moody <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the "LICENSE.md"
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Respect\Stringifier\Test\Stringifiers;
15+
16+
use JsonSerializable;
17+
use Respect\Stringifier\Quoter;
18+
use Respect\Stringifier\Stringifier;
19+
use Respect\Stringifier\Stringifiers\JsonSerializableStringifier;
20+
use PHPUnit\Framework\TestCase;
21+
use stdClass;
22+
23+
/**
24+
* @covers \Respect\Stringifier\Stringifiers\JsonSerializableStringifier
25+
*
26+
* @author Henrique Moody <[email protected]>
27+
*/
28+
final class JsonSerializableStringifierTest extends TestCase
29+
{
30+
/**
31+
* @test
32+
*/
33+
public function shouldConvertToStringWhenRawValueIsAJsonSerializableObject(): void
34+
{
35+
$raw = new JsonSerializableObject();
36+
$depth = 0;
37+
38+
$stringifiedData = '-stringified-';
39+
40+
$expectedValue = '[json-serializable] ('.JsonSerializableObject::class.': '.$stringifiedData.')';
41+
42+
$stringifierMock = $this->createMock(Stringifier::class);
43+
$stringifierMock
44+
->expects($this->once())
45+
->method('stringify')
46+
->with(JsonSerializableObject::JSON_VALUE, $depth + 1)
47+
->willReturn($stringifiedData);
48+
49+
$quoterMock = $this->createMock(Quoter::class);
50+
$quoterMock
51+
->expects($this->once())
52+
->method('quote')
53+
->with($expectedValue, $depth)
54+
->willReturn($expectedValue);
55+
56+
$jsonSerializableStringifier = new JsonSerializableStringifier($stringifierMock, $quoterMock);
57+
58+
self::assertSame($expectedValue, $jsonSerializableStringifier->stringify($raw, $depth));
59+
}
60+
61+
/**
62+
* @test
63+
*/
64+
public function shouldNotConvertToStringWhenRawValueIsNotTraversable(): void
65+
{
66+
$raw = new stdClass();
67+
$depth = 1;
68+
69+
$stringifierMock = $this->createMock(Stringifier::class);
70+
$stringifierMock
71+
->expects($this->never())
72+
->method('stringify');
73+
74+
$quoterMock = $this->createMock(Quoter::class);
75+
$quoterMock
76+
->expects($this->never())
77+
->method('quote');
78+
79+
$jsonSerializableStringifier = new JsonSerializableStringifier($stringifierMock, $quoterMock);
80+
81+
self::assertNull($jsonSerializableStringifier->stringify($raw, $depth));
82+
}
83+
}
84+
85+
final class JsonSerializableObject implements JsonSerializable
86+
{
87+
public const JSON_VALUE = [1, true, 'foo', null];
88+
89+
public function jsonSerialize(): array
90+
{
91+
return self::JSON_VALUE;
92+
}
93+
}

0 commit comments

Comments
 (0)