Skip to content

Commit e9de43c

Browse files
committed
Create ObjectStringifier
1 parent eee7eb4 commit e9de43c

File tree

2 files changed

+153
-0
lines changed

2 files changed

+153
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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 function get_class;
17+
use function get_object_vars;
18+
use function is_object;
19+
use function sprintf;
20+
use Respect\Stringifier\Quoter;
21+
use Respect\Stringifier\Stringifier;
22+
23+
/**
24+
* Converts an object into a string.
25+
*
26+
* @author Henrique Moody <[email protected]>
27+
*/
28+
final class ObjectStringifier implements Stringifier
29+
{
30+
/**
31+
* @var Stringifier
32+
*/
33+
private $stringifier;
34+
35+
/**
36+
* @var Quoter
37+
*/
38+
private $quoter;
39+
40+
/**
41+
* Initializes the stringifier.
42+
*
43+
* @param Stringifier $stringifier
44+
* @param Quoter $quoter
45+
*/
46+
public function __construct(Stringifier $stringifier, Quoter $quoter)
47+
{
48+
$this->stringifier = $stringifier;
49+
$this->quoter = $quoter;
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function stringify($raw, int $depth): ?string
56+
{
57+
if (!is_object($raw)) {
58+
return null;
59+
}
60+
61+
return $this->quoter->quote(
62+
sprintf(
63+
'[object] (%s: %s)',
64+
get_class($raw),
65+
$this->stringifier->stringify(get_object_vars($raw), $depth + 1)
66+
),
67+
$depth
68+
);
69+
}
70+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 Respect\Stringifier\Quoter;
17+
use Respect\Stringifier\Stringifier;
18+
use Respect\Stringifier\Stringifiers\ObjectStringifier;
19+
use PHPUnit\Framework\TestCase;
20+
21+
/**
22+
* @covers \Respect\Stringifier\Stringifiers\ObjectStringifier
23+
*
24+
* @author Henrique Moody <[email protected]>
25+
*/
26+
final class ObjectStringifierTest extends TestCase
27+
{
28+
/**
29+
* @test
30+
*/
31+
public function shouldConvertToStringWhenRawValueIsAnObject(): void
32+
{
33+
$data = ['foo' => 1, 'bar' => false];
34+
35+
$raw = (object) $data;
36+
$depth = 0;
37+
38+
$stringifiedData = '-stringified-';
39+
40+
$expectedValue = '[object] (stdClass: '.$stringifiedData.')';
41+
42+
$stringifierMock = $this->createMock(Stringifier::class);
43+
$stringifierMock
44+
->expects($this->once())
45+
->method('stringify')
46+
->with($data, $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+
$objectStringifier = new ObjectStringifier($stringifierMock, $quoterMock);
57+
58+
self::assertSame($expectedValue, $objectStringifier->stringify($raw, $depth));
59+
}
60+
61+
/**
62+
* @test
63+
*/
64+
public function shouldNotConvertToStringWhenRawValueIsNotTraversable(): void
65+
{
66+
$raw = true;
67+
$depth = 123;
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+
$objectStringifier = new ObjectStringifier($stringifierMock, $quoterMock);
80+
81+
self::assertNull($objectStringifier->stringify($raw, $depth));
82+
}
83+
}

0 commit comments

Comments
 (0)