8000 [ValueExporter] added support for objects implementing __toString() · symfony/symfony@a83684c · GitHub
[go: up one dir, main page]

Skip to content

Commit a83684c

Browse files
committed
[ValueExporter] added support for objects implementing __toString()
1 parent 8f63b07 commit a83684c

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public function exportValue($value, $depth = 1, $expand = false)
7070
}
7171
// Fallback on default
7272
if (is_object($value)) {
73+
if (method_exists($value, '__toString')) {
74+
return sprintf('object(%s) "%s"', get_class($value), $value);
75+
}
76+
7377
return sprintf('object(%s)', get_class($value));
7478
}
7579
if (is_resource($value)) {

src/Symfony/Component/ValueExporter/Formatter/EntityToStringFormatter.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public function formatToString($value)
3939
{
4040
$id = isset($value->id) ? $value->id : (is_callable(array($value, 'id')) ? $value->id() : $value->getId());
4141

42+
if (method_exists($value, '__toString')) {
43+
return sprintf('entity:%s(%s) "%s"', $id, get_class($value), $value);
44+
}
45+
4246
return sprintf('entity:%s(%s)', $id, get_class($value));
4347
}
4448
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
* Entity with an id getter.
16+
*
17+
* @author Jules Pietri <jules@heahprod.com>
18+
*/
19+
class EntityImplementingToString
20+
{
21+
public $id;
22+
private $name;
23+
24+
public function __construct($id, $name)
25+
{
26+
$this->id = $id;
27+
$this->name = $name;
28+
}
29+
30+
public function __toString()
31+
{
32+
return (string) $this->name;
33+
}
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
8000 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+
* Entity with an id getter.
16+
*
17+
* @author Jules Pietri <jules@heahprod.com>
18+
*/
19+
class ObjectImplementingToString
20+
{
21+
private $name;
22+
23+
public function __construct($name)
24+
{
25+
$this->name = $name;
26+
}
27+
28+
public function __toString()
29+
{
30+
return (string) $this->name;
31+
}
32+
}

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

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

1414
use Symfony\Component\ValueExporter\Formatter\TraversableToStringFormatter;
1515
use Symfony\Component\ValueExporter\Tests\Fixtures\Entity;
16+
use Symfony\Component\ValueExporter\Tests\Fixtures\EntityImplementingToString;
17+
use Symfony\Component\ValueExporter\Tests\Fixtures\ObjectImplementingToString;
1618
use Symfony\Component\ValueExporter\Tests\Fixtures\PublicEntity;
1719
use Symfony\Component\ValueExporter\Tests\Fixtures\TraversableInstance;
1820
use Symfony\Component\ValueExporter\ValueExporter;
@@ -87,6 +89,10 @@ public function valueProvider()
8789
array(0 => 0, '1' => 'un', 'key' => 4.5),
8890
'array(0 => (int) 0, 1 => "un", \'key\' => (float) 4.5)',
8991
),
92+
'object implementing to string' => array(
93+
new ObjectImplementingToString('test'),
94+
'object(Symfony\Component\ValueExporter\Tests\Fixtures\ObjectImplementingToString) "test"',
95+
),
9096
'closure' => array(function() {}, 'object(Closure)'),
9197
'callable string' => array('strlen', '(function) "strlen"'),
9298
'callable array' => array(
@@ -105,6 +111,10 @@ public function valueProvider()
105111
'php incomplete class' => array($foo, '__PHP_Incomplete_Class(AppBundle/Foo)'),
106112
'entity' => array(new Entity(23), 'entity:23(Symfony\Component\ValueExporter\Tests\Fixtures\Entity)'),
107113
'public entity' => array(new PublicEntity(23), 'entity:23(Symfony\Component\ValueExporter\Tests\Fixtures\PublicEntity)'),
114+
'entity implementing to string' => array(
115+
new EntityImplementingToString(23, 'test'),
116+
'entity:23(Symfony\Component\ValueExporter\Tests\Fixtures\EntityImplementingToString) "test"',
117+
),
108118
);
109119
}
110120

0 commit comments

Comments
 (0)
0