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

Commit5d237ae

Browse files
committed
[ValueExporter] added CallableToStringFormatter
1 parent95f2e99 commit5d237ae

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

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

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

‎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+
publicfunctiontestCallable()
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
publicfunctiontestDateTime()
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
useSymfony\Component\ValueExporter\Exporter\ValueExporterInterface;
1515
useSymfony\Component\ValueExporter\Exporter\ValueToStringExporter;
16+
useSymfony\Component\ValueExporter\Formatter\CallableToStringFormatter;
1617
useSymfony\Component\ValueExporter\Formatter\DateTimeToStringFormatter;
1718
useSymfony\Component\ValueExporter\Formatter\FormatterInterface;
1819
useSymfony\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 ?:newValueToStringExporter(
39+
newCallableToStringFormatter(),
3840
newDateTimeToStringFormatter(),
3941
newPhpIncompleteClassToStringFormatter()
4042
);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp