DoctrineDataCollector: taught sanitizeParam to support classes with __toString implemented.

This commit is contained in:
Vladislav Rastrusny 2016-11-29 14:10:58 +03:00 committed by Fabien Potencier
parent 0413e1832f
commit f2970f22ac
3 changed files with 41 additions and 2 deletions

View File

@ -159,7 +159,11 @@ class DoctrineDataCollector extends DataCollector
private function sanitizeParam($var)
{
if (is_object($var)) {
return array(sprintf('Object(%s)', get_class($var)), false);
$className = get_class($var);
return method_exists($var, '__toString') ?
array(sprintf('Object(%s): "%s"', $className, $var->__toString()), false) :
array(sprintf('Object(%s)', $className), false);
}
if (is_array($var)) {

View File

@ -127,7 +127,13 @@ class DoctrineDataCollectorTest extends TestCase
array(null, array(), null, true),
array(new \DateTime('2011-09-11'), array('date'), '2011-09-11', true),
array(fopen(__FILE__, 'r'), array(), 'Resource(stream)', false),
array(new \SplFileInfo(__FILE__), array(), 'Object(SplFileInfo)', false),
array(new \stdClass(), array(), 'Object(stdClass)', false),
array(
new StringRepresentableClass('presentation test'),
array(),
'Object(Symfony\Bridge\Doctrine\Tests\DataCollector\StringRepresentableClass): "presentation test"',
false,
),
);
}

View File

@ -0,0 +1,29 @@
<?php
namespace Symfony\Bridge\Doctrine\Tests\DataCollector;
/**
* A class for testing __toString method behaviour. It's __toString returns a value, that was passed into constructor.
*/
class StringRepresentableClass
{
/**
* @var string
*/
private $representation;
/**
* CustomStringableClass constructor.
*
* @param string $representation
*/
public function __construct($representation)
{
$this->representation = $representation;
}
public function __toString()
{
return $this->representation;
}
}