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

Skip to content

Commit 77c9637

Browse files
committed
[ValueExporter] added TraversableToStringFormatter
1 parent a70d8aa commit 77c9637

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
/**
15+
* Returns a string representation of an instance implementing \Traversable.
16+
*
17+
* @author Jules Pietri <jules@heahprod.com>
18+
*/
19+
class TraversableToStringFormatter extends ExpandedFormatter implements StringFormatterInterface
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function supports($value)
25+
{
26+
return $value instanceof \Traversable;
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function formatToString($value)
33+
{
34+
$nested = array();
35+
foreach ($value as $k => $v) {
36+
$nested[] = sprintf('%s => %s', is_string($k) ? sprintf("'%s'", $k) : $k, $this->export($v));
37+
}
38+
39+
return sprintf("Traversable:\"%s\"(\n %s\n)", get_class($value), implode(",\n ", $nested));
40+
}
41+
}
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