Skip to content

Commit d20cb83

Browse files
committed
Create TraversableStringifier
1 parent df912e4 commit d20cb83

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 iterator_to_array;
18+
use Respect\Stringifier\Quoter;
19+
use Respect\Stringifier\Stringifier;
20+
use Traversable;
21+
22+
/**
23+
* Converts an instance of Traversable into a string.
24+
*
25+
* @author Henrique Moody <[email protected]>
26+
*/
27+
final class TraversableStringifier implements Stringifier
28+
{
29+
/**
30+
* @var Stringifier
31+
*/
32+
private $stringifier;
33+
34+
/**
35+
* @var Quoter
36+
*/
37+
private $quoter;
38+
39+
/**
40+
* Initializes the stringifier.
41+
*
42+
* @param Stringifier $stringifier
43+
* @param Quoter $quoter
44+
*/
45+
public function __construct(Stringifier $stringifier, Quoter $quoter)
46+
{
47+
$this->stringifier = $stringifier;
48+
$this->quoter = $quoter;
49+
}
50+
51+
/**
52+
* {@inheritdoc}
53+
*/
54+
public function stringify($raw, int $depth): ?string
55+
{
56+
if (!$raw instanceof Traversable) {
57+
return null;
58+
}
59+
60+
return $this->quoter->quote(
61+
sprintf(
62+
'[traversable] (%s: %s)',
63+
get_class($raw),
64+
$this->stringifier->stringify(iterator_to_array($raw), $depth + 1)
65+
),
66+
$depth
67+
);
68+
}
69+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 ArrayIterator;
17+
use PHPUnit\Framework\TestCase;
18+
use Respect\Stringifier\Quoter;
19+
use Respect\Stringifier\Stringifier;
20+
use Respect\Stringifier\Stringifiers\TraversableStringifier;
21+
22+
/**
23+
* @covers \Respect\Stringifier\Stringifiers\TraversableStringifier
24+
*
25+
* @author Henrique Moody <[email protected]>
26+
*/
27+
final class TraversableStringifierTest extends TestCase
28+
{
29+
/**
30+
* @test
31+
*/
32+
public function shouldConvertToStringWhenValueIsTraversable(): void
33+
{
34+
$raw = new ArrayIterator([1, 2, 3]);
35+
$depth = 0;
36+
37+
$stringifiedData = '-stringified-';
38+
39+
$expectedValue = '[traversable] (ArrayIterator: '.$stringifiedData.')';
40+
41+
$stringifierMock = $this->createMock(Stringifier::class);
42+
$stringifierMock
43+
->expects($this->once())
44+
->method('stringify')
45+
->with($raw->getArrayCopy(), $depth + 1)
46+
->willReturn($stringifiedData);
47+
48+
$quoterMock = $this->createMock(Quoter::class);
49+
$quoterMock
50+
->expects($this->once())
51+
->method('quote')
52+
->with($expectedValue, $depth)
53+
->willReturn($expectedValue);
54+
55+
$traversableStringifier = new TraversableStringifier($stringifierMock, $quoterMock);
56+
57+
self::assertSame($expectedValue, $traversableStringifier->stringify($raw, $depth));
58+
}
59+
60+
/**
61+
* @test
62+
*/
63+
public function shouldNotConvertToStringWhenRawValueIsNotTraversable(): void
64+
{
65+
$raw = [1, 2, 3, 4];
66+
$depth = 123;
67+
68+
$stringifierMock = $this->createMock(Stringifier::class);
69+
$stringifierMock
70+
->expects($this->never())
71+
->method('stringify');
72+
73+
$quoterMock = $this->createMock(Quoter::class);
74+
$quoterMock
75+
->expects($this->never())
76+
->method('quote');
77+
78+
$traversableStringifier = new TraversableStringifier($stringifierMock, $quoterMock);
79+
80+
self::assertNull($traversableStringifier->stringify($raw, $depth));
81+
}
82+
}

0 commit comments

Comments
 (0)