feature #20680 DoctrineDataCollector: taught sanitizeParam to support classes with __toString implemented. (FractalizeR)

This PR was squashed before being merged into the 3.3-dev branch (closes #20680).

Discussion
----------

DoctrineDataCollector: taught sanitizeParam to support classes with __toString implemented.

This PR teaches \Symfony\Bridge\Doctrine\DataCollector\DoctrineDataCollector::sanitizeParam support objects, which implement __toString and therefore can be represented as a string with more sense, than "(object) ClassName". It also includes test for the feature.

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | [20673](https://github.com/symfony/symfony/issues/20673)
| License       | MIT
| Doc PR        | no

Commits
-------

f2970f22ac DoctrineDataCollector: taught sanitizeParam to support classes with __toString implemented.
This commit is contained in:
Fabien Potencier 2017-03-02 10:22:47 -08:00
commit 3b4a8f3730
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;
}
}