Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitef747a3

Browse files
committed
[ValueExporter] added TraversableToStringFormatter
1 parentd62d280 commitef747a3

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+
namespaceSymfony\Component\ValueExporter\Formatter;
13+
14+
useSymfony\Component\ValueExporter\Exporter\ValueExporterInterface;
15+
useSymfony\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 TraversableToStringFormatterimplements StringFormatterInterface, ExpandedFormatterInterface
23+
{
24+
/**
25+
* @var ValueToStringExporter
26+
*/
27+
private$exporter;
28+
29+
publicfunctionsetExporter(ValueExporterInterface$exporter)
30+
{
31+
$this->exporter =$exporter;
32+
}
33+
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
publicfunctionsupports($value)
38+
{
39+
return$valueinstanceof \Traversable;
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
publicfunctionformatToString($value)
46+
{
47+
$nested =array();
48+
foreach ($valueas$k =>$v) {
49+
$nested[] =sprintf('%s => %s',is_string($k) ?sprintf("'%s'",$k) :$k,$this->exporter->exportValue($v));
50+
}
51+
52+
returnsprintf("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+
namespaceSymfony\Component\ValueExporter\Tests\Fixtures;
13+
14+
/**
15+
* TraversableInstance.
16+
*
17+
* @author Jules Pietri <jules@heahprod.com>
18+
*/
19+
class TraversableInstanceimplements \IteratorAggregate
20+
{
21+
public$property1 ='value1';
22+
public$property2 ='value2';
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
publicfunctiongetIterator()
28+
{
29+
returnnew \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
namespaceSymfony\Component\ValueExporter\Tests;
1313

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

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

51+
publicfunctiontestExportTraversable()
52+
{
53+
ValueExporter::appendFormatter(newTraversableToStringFormatter());
54+
55+
$value =newTraversableInstance();
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
publicfunctionvalueProvider()
5067
{
5168
$foo =new \__PHP_Incomplete_Class();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp