71b090cf0b840be6b4ff860ce6f82c19482a1804
[yaffs-website] / vendor / symfony / http-kernel / DataCollector / Util / ValueExporter.php
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\HttpKernel\DataCollector\Util;
13
14 /**
15  * @author Bernhard Schussek <bschussek@gmail.com>
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
34         if (is_object($value)) {
35             if ($value instanceof \DateTime || $value instanceof \DateTimeInterface) {
36                 return sprintf('Object(%s) - %s', get_class($value), $value->format(\DateTime::ATOM));
37             }
38
39             return sprintf('Object(%s)', get_class($value));
40         }
41
42         if (is_array($value)) {
43             if (empty($value)) {
44                 return '[]';
45             }
46
47             $indent = str_repeat('  ', $depth);
48
49             $a = array();
50             foreach ($value as $k => $v) {
51                 if (is_array($v)) {
52                     $deep = true;
53                 }
54                 $a[] = sprintf('%s => %s', $k, $this->exportValue($v, $depth + 1, $deep));
55             }
56
57             if ($deep) {
58                 return sprintf("[\n%s%s\n%s]", $indent, implode(sprintf(", \n%s", $indent), $a), str_repeat('  ', $depth - 1));
59             }
60
61             return sprintf('[%s]', implode(', ', $a));
62         }
63
64         if (is_resource($value)) {
65             return sprintf('Resource(%s#%d)', get_resource_type($value), $value);
66         }
67
68         if (null === $value) {
69             return 'null';
70         }
71
72         if (false === $value) {
73             return 'false';
74         }
75
76         if (true === $value) {
77             return 'true';
78         }
79
80         return (string) $value;
81     }
82
83     private function getClassNameFromIncomplete(\__PHP_Incomplete_Class $value)
84     {
85         $array = new \ArrayObject($value);
86
87         return $array['__PHP_Incomplete_Class_Name'];
88     }
89 }