8000 [PoC] [ValueExporter] extract HttpKernel/DataCollector util to its own component by HeahDude · Pull Request #18450 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PoC] [ValueExporter] extract HttpKernel/DataCollector util to its own component #18450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[ValueExporter] added TraversableToStringFormatter
  • Loading branch information
HeahDude committed Apr 23, 2016
commit 845aea1f65b20ab895e0aa61872dff3c08a02968
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\ValueExporter\Formatter;

/**
* Returns a string representation of an instance implementing \Traversable.
*
* @author Jules Pietri <jules@heahprod.com>
*/
class TraversableToStringFormatter extends ExpandedFormatter implements StringFormatterInterface
{
/**
* {@inheritdoc}
*/
public function supports($value)
{
return $value instanceof \Traversable;
}

/**
* {@inheritdoc}
*/
public function formatToString($value)
{
$nested = array();
foreach ($value as $k => $v) {
$nested[] = sprintf('%s => %s', is_string($k) ? sprintf("'%s'", $k) : $k, $this->export($v));
}

return sprintf("Traversable:\"%s\"(\n %s\n)", get_class($value), implode(",\n ", $nested));
}
}
Original file line number Diff line number 8000 Diff line change
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\ValueExporter\Tests\Fixtures;

/**
* TraversableInstance.
*
* @author Jules Pietri <jules@heahprod.com>
*/
class TraversableInstance implements \IteratorAggregate
{
public $property1 = 'value1';
public $property2 = 'value2';

/**
* {@inheritdoc}
*/
public function getIterator()
{
return new \ArrayIterator($this);
}
}
17 changes: 17 additions & 0 deletions src/Symfony/Component/ValueExporter/Tests/ValueExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\ValueExporter\Tests;

use Symfony\Component\ValueExporter\Formatter\TraversableToStringFormatter;
use Symfony\Component\ValueExporter\Tests\Fixtures\TraversableInstance;
use Symfony\Component\ValueExporter\ValueExporter;

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

public function testExportTraversable()
{
ValueExporter::appendFormatter(new TraversableToStringFormatter());

$value = new TraversableInstance();
$exportedValue = <<<EOT
Traversable:"Symfony\Component\ValueExporter\Tests\Fixtures\TraversableInstance"(
'property1' => "value1",
'property2' => "value2"
)
EOT;

$this->assertSame($exportedValue, ValueExporter::export($value));
}

public function valueProvider()
{
$foo = new \__PHP_Incomplete_Class();
Expand Down
0