Skip to content

Commit df912e4

Browse files
committed
Create StringableObjectStringifier
1 parent de876a9 commit df912e4

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 is_object;
17+
use function method_exists;
18+
use Respect\Stringifier\Stringifier;
19+
20+
/**
21+
* Converts a object that implements the __toString() magic method into a string.
22+
*
23+
* @author Henrique Moody <[email protected]>
24+
*/
25+
final class StringableObjectStringifier implements Stringifier
26+
{
27+
/**
28+
* @var Stringifier
29+
*/
30+
private $stringifier;
31+
32+
/**
33+
* Initializes the stringifier.
34+
*
35+
* @param Stringifier $stringifier
36+
*/
37+
public function __construct(Stringifier $stringifier)
38+
{
39+
$this->stringifier = $stringifier;
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function stringify($raw, int $depth): ?string
46+
{
47+
if (!is_object($raw)) {
48+
return null;
49+
}
50+
51+
if (!method_exists($raw, '__toString')) {
52+
return null;
53+
}
54+
55+
return $this->stringifier->stringify($raw->__toString(), $depth);
56+
}
57+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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\Stringifier;
17+
use Respect\Stringifier\Stringifiers\StringableObjectStringifier;
18+
use PHPUnit\Framework\TestCase;
19+
use stdClass;
20+
21+
/**
22+
* @covers \Respect\Stringifier\Stringifiers\StringableObjectStringifier
23+
*
24+
* @author Henrique Moody <[email protected]>
25+
*/
26+
final class StringableObjectStringifierTest extends TestCase
27+
{
28+
/**
29+
* @test
30+
*/
31+
public function shouldNotConvertToStringWhenValueIsNotAnObject(): void
32+
{
33+
$raw = 'not-an-object';
34+
$depth = 1;
35+
36+
$stringifierMock = $this->createMock(Stringifier::class);
37+
$stringifierMock
38+
->expects($this->never())
39+
->method('stringify');
40+
41+
$stringableObjectStringifier = new StringableObjectStringifier($stringifierMock);
42+
43+
self::assertNull($stringableObjectStringifier->stringify($raw, $depth));
44+
}
45+
46+
/**
47+
* @test
48+
*/
49+
public function shouldNotConvertToStringWhenValueIsANonStringableObject(): void
50+
{
51+
$raw = new stdClass();
52+
$depth = 1;
53+
54+
$stringifierMock = $this->createMock(Stringifier::class);
55+
$stringifierMock
56+
->expects($this->never())
57+
->method('stringify');
58+
59+
$stringableObjectStringifier = new StringableObjectStringifier($stringifierMock);
60+
61+
self::assertNull($stringableObjectStringifier->stringify($raw, $depth));
62+
}
63+
64+
/**
65+
* @test
66+
*/
67+
public function shouldConvertToStringWhenValueIsAnStringableObject(): void
68+
{
69+
$raw = new StringableObject();
70+
$depth = 0;
71+
72+
$expectedValue = StringableObject::STRING_VALUE;
73+
74+
$stringifierMock = $this->createMock(Stringifier::class);
75+
$stringifierMock
76+
->expects($this->once())
77+
->method('stringify')
78+
->with(StringableObject::STRING_VALUE, $depth)
79+
->willReturn($expectedValue);
80+
81+
$stringableObjectStringifier = new StringableObjectStringifier($stringifierMock);
82+
83+
self::assertSame($expectedValue, $stringableObjectStringifier->stringify($raw, $depth));
84+
}
85+
}
86+
87+
final class StringableObject
88+
{
89+
public const STRING_VALUE = 'String';
90+
91+
public function __toString()
92+
{
93+
return self::STRING_VALUE;
94+
}
95+
}

0 commit comments

Comments
 (0)