8000 [ValueExporter] added TraversableToStringFormatter · symfony/symfony@ef747a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit ef747a3

Browse files
committed
[ValueExporter] added TraversableToStringFormatter
1 parent d62d280 commit ef747a3

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\ValueExporter\Formatter;
13+
14+
use Symfony\Component\ValueExporter\Exporter\ValueExporterInterface;
15+
use Symfony\Component\ValueExporter\Exporter\ValueToStringExporter;
16+
17+
/**
18+
* Returns a string representation of an instance implementing \Traversable.
19+
*
20+
* @author Jules Pietri <jules@heahprod.com>
21+
*/
22+
class TraversableToStringFormatter implements StringFormatterInterface, ExpandedFormatterInterface
23+
{
24+
/**
25+
* @var ValueToStringExporter
26+
*/
27+
private $exporter;
28+
29+
public function setExporter(ValueExporterInterface $exporter)
30+
{
31+
$this->exporter = $exporter;
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function supports($value)
38+
{
39+
return $value instanceof \Traversable;
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function formatToString($value)
46+
{
47+
$nested = array();
48+
foreach ($value as $k => $v) {
49+
$nested[] = sprintf('%s => %s', is_string($k) ? sprintf("'%s'", $k) : $k, $this->exporter->exportValue($v));
50+
}
51+
52+
return sprintf("Traversable:\"%s\"(\n %s\n)", get_class($value), implode(",\n ", $nested));
53+
}
54+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\ValueExporter\Tests\Fixtures;
13+
14+
/**
15+
* TraversableInstance.
16+
*
17+
* @author Jules Pietri <jules@heahprod.com>
18+
*/
19+
class TraversableInstance implements \IteratorAggregate
20+
{
21+
public $property1 = 'value1';
22+
public $property2 = 'value2';
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
public function getIterator()
28+
{
29+
return new \ArrayIterator($this);
30+
}
31+
}

src/Symfony/Component/ValueExporter/Tests/ValueExporterTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\ValueExporter\Tests;
1313

14+
use Symfony\Component\ValueExporter\Formatter\TraversableToStringFormatter;
15+
use Symfony\Component\ValueExporter\Tests\Fixtures\TraversableInstance;
1416
use Symfony\Component\ValueExporter\ValueExporter;
1517

1618
class ValueExporterTest extends \PHPUnit_Framework_TestCase
@@ -46,6 +48,21 @@ public function testExportValueExpanded()
4648
$this->assertSame($exportedValue, ValueExporter::export($value, 1, true));
4749
}
4850

51+
public function testExportTraversable()
52+
{
53+
ValueExporter::appendFormatter(new TraversableToStringFormatter());
54+
55+
$value = new TraversableInstance();
56+
$exportedValue = <<<EOT
57+
Traversable:"Symfony\Component\ValueExporter\Tests\Fixtures\TraversableInstance"(
58+
'property1' => "value1",
59+
'property2' => "value2"
60+
)
61+
EOT;
62+
63+
$this->assertSame($exportedValue, ValueExporter::export($value));
64+
}
65+
4966
public function valueProvider()
5067
{
5168
$foo = new \__PHP_Incomplete_Class();

0 commit comments

Comments
 (0)
0