ValueExporter.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\DataCollector\Util;
  11. @trigger_error('The '.__NAMESPACE__.'\ValueExporter class is deprecated since version 3.2 and will be removed in 4.0. Use the VarDumper component instead.', E_USER_DEPRECATED);
  12. /**
  13. * @author Bernhard Schussek <bschussek@gmail.com>
  14. *
  15. * @deprecated since version 3.2, to be removed in 4.0. Use the VarDumper component instead.
  16. */
  17. class ValueExporter
  18. {
  19. /**
  20. * Converts a PHP value to a string.
  21. *
  22. * @param mixed $value The PHP value
  23. * @param int $depth only for internal usage
  24. * @param bool $deep only for internal usage
  25. *
  26. * @return string The string representation of the given value
  27. */
  28. public function exportValue($value, $depth = 1, $deep = false)
  29. {
  30. if ($value instanceof \__PHP_Incomplete_Class) {
  31. return sprintf('__PHP_Incomplete_Class(%s)', $this->getClassNameFromIncomplete($value));
  32. }
  33. if (is_object($value)) {
  34. if ($value instanceof \DateTimeInterface) {
  35. return sprintf('Object(%s) - %s', get_class($value), $value->format(\DateTime::ATOM));
  36. }
  37. return sprintf('Object(%s)', get_class($value));
  38. }
  39. if (is_array($value)) {
  40. if (empty($value)) {
  41. return '[]';
  42. }
  43. $indent = str_repeat(' ', $depth);
  44. $a = array();
  45. foreach ($value as $k => $v) {
  46. if (is_array($v)) {
  47. $deep = true;
  48. }
  49. $a[] = sprintf('%s => %s', $k, $this->exportValue($v, $depth + 1, $deep));
  50. }
  51. if ($deep) {
  52. return sprintf("[\n%s%s\n%s]", $indent, implode(sprintf(", \n%s", $indent), $a), str_repeat(' ', $depth - 1));
  53. }
  54. $s = sprintf('[%s]', implode(', ', $a));
  55. if (80 > strlen($s)) {
  56. return $s;
  57. }
  58. return sprintf("[\n%s%s\n]", $indent, implode(sprintf(",\n%s", $indent), $a));
  59. }
  60. if (is_resource($value)) {
  61. return sprintf('Resource(%s#%d)', get_resource_type($value), $value);
  62. }
  63. if (null === $value) {
  64. return 'null';
  65. }
  66. if (false === $value) {
  67. return 'false';
  68. }
  69. if (true === $value) {
  70. return 'true';
  71. }
  72. return (string) $value;
  73. }
  74. private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value)
  75. {
  76. $array = new \ArrayObject($value);
  77. return $array['__PHP_Incomplete_Class_Name'];
  78. }
  79. }