Skip to content

Commit e62e07b

Browse files
committed
Create ClusterStringifier
1 parent 15d3847 commit e62e07b

File tree

2 files changed

+236
-0
lines changed

2 files changed

+236
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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\Quoters\CodeQuoter;
17+
use Respect\Stringifier\Quoters\StringQuoter;
18+
use Respect\Stringifier\Stringifier;
19+
20+
/**
21+
* Converts a value into a string using the defined Stringifiers.
22+
*
23+
* @author Henrique Moody <[email protected]>
24+
*/
25+
final class ClusterStringifier implements Stringifier
26+
{
27+
/**
28+
* @var Stringifier[]
29+
*/
30+
private $stringifiers;
31+
32+
/**
33+
* Initializes the stringifier.
34+
*
35+
* @param Stringifier[] ...$stringifiers
36+
*/
37+
public function __construct(Stringifier ...$stringifiers)
38+
{
39+
$this->setStringifiers($stringifiers);
40+
}
41+
42+
/**
43+
* Create a default instance of the class.
44+
*
45+
* This instance includes all possible stringifiers.
46+
*
47+
* @return ClusterStringifier
48+
*/
49+
public static function createDefault(): self
50+
{
51+
$quoter = new CodeQuoter();
52+
53+
$stringifier = new self();
54+
$stringifier->setStringifiers([
55+
new TraversableStringifier($stringifier, $quoter),
56+
new DateTimeStringifier($stringifier, 'c'),
57+
new ThrowableStringifier($stringifier, $quoter),
58+
new StringableObjectStringifier($stringifier),
59+
new JsonSerializableStringifier($stringifier, $quoter),
60+
new ObjectStringifier($stringifier, $quoter),
61+
new ArrayStringifier($stringifier, 3, 5),
62+
new InfiniteStringifier($quoter),
63+
new NanStringifier($quoter),
64+
new ResourceStringifier($quoter),
65+
new BoolStringifier($quoter),
66+
new NullStringifier($quoter),
67+
new JsonParsableStringifier(),
68+
new PhpParsableStringifier(),
69+
]);
70+
71+
return $stringifier;
72+
}
73+
74+
/**
75+
* Set stringifiers.
76+
*
77+
* @param array $stringifiers
78+
*
79+
* @return void
80+
*/
81+
public function setStringifiers(array $stringifiers): void
82+
{
83+
$this->stringifiers = [];
84+
85+
foreach ($stringifiers as $stringifier) {
86+
$this->addStringifier($stringifier);
87+
}
88+
}
89+
90+
/**
91+
* Add a stringifier to the chain
92+
*
93+
* @param Stringifier $stringifier
94+
*
95+
* @return void
96+
*/
97+
public function addStringifier(Stringifier $stringifier): void
98+
{
99+
$this->stringifiers[] = $stringifier;
100+
}
101+
102+
/**
103+
* {@inheritdoc}
104+
*/
105+
public function stringify($value, int $depth): ?string
106+
{
107+
foreach ($this->stringifiers as $stringifier) {
108+
$string = $stringifier->stringify($value, $depth);
109+
if (null === $string) {
110+
continue;
111+
}
112+
113+
return $string;
114+
}
115+
116+
return null;
117+
}
118+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
namespace Respect\Stringifier\Test\Stringifiers;
17+
18+
use Respect\Stringifier\Stringifier;
19+
use Respect\Stringifier\Stringifiers\ClusterStringifier;
20+
use PHPUnit\Framework\TestCase;
21+
use stdClass;
22+
23+
/**
24+
* @covers \Respect\Stringifier\Stringifiers\ClusterStringifier
25+
*
26+
* @author Henrique Moody <[email protected]>
27+
*/
28+
final class ClusterStringifierTest extends TestCase
29+
{
30+
/**
31+
* @test
32+
*/
33+
public function shouldReturnNullWhenNoStringifiersAreDefined(): void
34+
{
35+
$raw = new stdClass();
36+
$depth = 0;
37+
38+
$clusterStringifier = new ClusterStringifier();
39+
40+
self::assertNull($clusterStringifier->stringify($raw, $depth));
41+
}
42+
43+
/**
44+
* @test
45+
*/
46+
public function shouldUseAllStringifierToStringifyUntilNonFails(): void
47+
{
48+
$raw = new stdClass();
49+
$depth = 0;
50+
51+
$expected = 'foo';
52+
53+
$stringifier1 = $this->createMock(Stringifier::class);
54+
$stringifier1
55+
->expects($this->once())
56+
->method('stringify')
57+
->with($raw, $depth)
58+
->willReturn(null);
59+
60+
$stringifier2 = $this->createMock(Stringifier::class);
61+
$stringifier2
62+
->expects($this->once())
63+
->method('stringify')
64+
->with($raw, $depth)
65+
->willReturn($expected);
66+
67+
$stringifier3 = $this->createMock(Stringifier::class);
68+
$stringifier3
69+
->expects($this->never())
70+
->method('stringify');
71+
72+
$clusterStringifier = new ClusterStringifier($stringifier1, $stringifier2, $stringifier3);
73+
74+
self::assertSame($expected, $clusterStringifier->stringify($raw, $depth));
75+
}
76+
77+
/**
78+
* @test
79+
*/
80+
public function shouldReturnNullWhenAllStringifierCannotConvertToString(): void
81+
{
82+
$raw = new stdClass();
83+
$depth = 0;
84+
85+
$stringifier1 = $this->createMock(Stringifier::class);
86+
$stringifier1
87+
->expects($this->once())
88+
->method('stringify')
89+
->with($raw, $depth)
90+
->willReturn(null);
91+
92+
$stringifier2 = $this->createMock(Stringifier::class);
93+
$stringifier2
94+
->expects($this->once())
95+
->method('stringify')
96+
->with($raw, $depth)
97+
->willReturn(null);
98+
99+
$stringifier3 = $this->createMock(Stringifier::class);
100+
$stringifier3
101+
->expects($this->once())
102+
->method('stringify')
103+
->with($raw, $depth)
104+
->willReturn(null);
105+
106+
$clusterStringifier = new ClusterStringifier($stringifier1, $stringifier2, $stringifier3);
107+
108+
self::assertNull($clusterStringifier->stringify($raw, $depth));
109+
}
110+
111+
/**
112+
* @test
113+
*/
114+
public function shouldCreateADefaultClusterStringifier(): void
115+
{
116+
self::assertInstanceOf(ClusterStringifier::class, ClusterStringifier::createDefault());
117+
}
118+
}

0 commit comments

Comments
 (0)