8000 [ValueExporter] added CallableToStringFormatter · symfony/symfony@608f750 · GitHub
[go: up one dir, main page]

Skip to content

Commit 608f750

Browse files
committed
[ValueExporter] added CallableToStringFormatter
1 parent 2f0461b commit 608f750

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

src/Symfony/Component/ValueExporter/Exporter/ValueToStringExporter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function exportValue($value, $depth = 1, $expand = false)
3030
// Therefor, \Traversable instances might be treated as objects unless
3131
// implementing a {@link StringFormatterInterface} and passing it to
3232
// the exporter in order to support them.
33-
if (is_array($value)) {
33+
if (is_array($value) && !is_callable($value)) {
3434
if (empty($value)) {
3535
return 'array()';
3636
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 a string or array callable.
16+
*
17+
* @author Jules Pietri <jules@heahprod.com>
18+
*/
19+
class CallableToStringFormatter implements StringFormatterInterface
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function supports($value)
25+
{
26+
return is_callable($value) && !$value instanceof \Closure;
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function formatToString($value)
33+
{
34+
if (is_array($value)) {
35+
$caller = is_object($value[0]) ? get_class($value[0]) : $value[0];
36+
37+
if (isset($value[1])) {
38+
$method = $value[1];
39+
40+
if (false !== $cut = strpos($method, $caller)) {
41+
$method = substr($method, $cut);
42+
}
43+
44+
return sprintf('(callable) "%s::%s"', $caller, $method);
45+
}
46+
47+
return sprintf('(invokable) "%s"', $caller);
48+
}
49+
50+
return sprintf('(callable) "%s"', $value);
51+
}
52+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ public function testArrays()
4747
$this->assertSame('array(0 => null, 1 => object(stdClass), "test" => array())', ValueExporter::export($array));
4848
}
4949

50+
public function testCallable()
51+
{
52+
$closure = function() {};
53+
$this->assertSame('object(Closure)', ValueExporter::export($closure));
54+
55+
$string = 'strlen';
56+
$this->assertSame('(callable) "strlen"', ValueExporter::export($string));
57+
58+
$array = array($this, 'testCallable');
59+
$this->assertSame('(callable) "Symfony\Component\ValueExporter\Tests\ValueExporterTest::testCallable"', ValueExporter::export($array));
60+
}
61+
5062
public function testDateTime()
5163
{
5264
$dateTime = new \DateTime('2014-06-10 07:35:40', new \DateTimeZone('UTC'));

src/Symfony/Component/ValueExporter/ValueExporter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\ValueExporter\Exporter\ValueExporterInterface;
1515
use Symfony\Component\ValueExporter\Exporter\ValueToStringExporter;
16+
use Symfony\Component\ValueExporter\Formatter\CallableToStringFormatter;
1617
use Symfony\Component\ValueExporter\Formatter\DateTimeToStringFormatter;
1718
use Symfony\Component\ValueExporter\Formatter\FormatterInterface;
1819
use Symfony\Component\ValueExporter\Formatter\PhpIncompleteClassToStringFormatter;
@@ -35,6 +36,7 @@ public static function export($value, $depth = 1, $expand = false)
3536
{
3637
if (null === self::$handler) {
3738
$exporter = self::$exporter ?: new ValueToStringExporter(
39+
new CallableToStringFormatter(),
3840
new DateTimeToStringFormatter(),
3941
new PhpIncompleteClassToStringFormatter()
4042
);

0 commit comments

Comments
 (0)
0