diff --git a/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php b/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php index 7d9c28943b..bf2c97f8e6 100644 --- a/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php +++ b/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php @@ -11,17 +11,26 @@ namespace Symfony\Component\HttpKernel\DataCollector; +use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporter; +use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporterInterface; + /** * DataCollector. * * Children of this class must store the collected data in the data property. * * @author Fabien Potencier + * @author Bernhard Schussek */ abstract class DataCollector implements DataCollectorInterface, \Serializable { protected $data; + /** + * @var ValueExporter + */ + private $valueExporter; + public function serialize() { return serialize($this->data); @@ -41,35 +50,10 @@ abstract class DataCollector implements DataCollectorInterface, \Serializable */ protected function varToString($var) { - if (is_object($var)) { - return sprintf('Object(%s)', get_class($var)); + if (null === $this->valueExporter) { + $this->valueExporter = new ValueExporter(); } - if (is_array($var)) { - $a = array(); - foreach ($var as $k => $v) { - $a[] = sprintf('%s => %s', $k, $this->varToString($v)); - } - - return sprintf("Array(%s)", implode(', ', $a)); - } - - if (is_resource($var)) { - return sprintf('Resource(%s)', get_resource_type($var)); - } - - if (null === $var) { - return 'null'; - } - - if (false === $var) { - return 'false'; - } - - if (true === $var) { - return 'true'; - } - - return (string) $var; + $this->valueExporter->exportValue($var); } } diff --git a/src/Symfony/Component/HttpKernel/DataCollector/Util/ValueExporter.php b/src/Symfony/Component/HttpKernel/DataCollector/Util/ValueExporter.php new file mode 100644 index 0000000000..f3aeb80cb2 --- /dev/null +++ b/src/Symfony/Component/HttpKernel/DataCollector/Util/ValueExporter.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\DataCollector\Util; + +/** + * @author Bernhard Schussek + */ +class ValueExporter +{ + /** + * Converts a PHP value to a string. + * + * @param mixed $value The PHP value + * + * @return string The string representation of the given value + */ + public function exportValue($value) + { + if (is_object($value)) { + return sprintf('Object(%s)', get_class($value)); + } + + if (is_array($value)) { + $a = array(); + foreach ($value as $k => $v) { + $a[] = sprintf('%s => %s', $k, $this->exportValue($v)); + } + + return sprintf("Array(%s)", implode(', ', $a)); + } + + if (is_resource($value)) { + return sprintf('Resource(%s)', get_resource_type($value)); + } + + if (null === $value) { + return 'null'; + } + + if (false === $value) { + return 'false'; + } + + if (true === $value) { + return 'true'; + } + + return (string) $value; + } +}