Added the Porter Stemmer module to improve searches. This doesn't deal with some...
[yaffs-website] / vendor / sebastian / exporter / src / Exporter.php
1 <?php
2 /*
3  * This file is part of the Exporter package.
4  *
5  * (c) Sebastian Bergmann <sebastian@phpunit.de>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace SebastianBergmann\Exporter;
12
13 use SebastianBergmann\RecursionContext\Context;
14
15 /**
16  * A nifty utility for visualizing PHP variables.
17  *
18  * <code>
19  * <?php
20  * use SebastianBergmann\Exporter\Exporter;
21  *
22  * $exporter = new Exporter;
23  * print $exporter->export(new Exception);
24  * </code>
25  */
26 class Exporter
27 {
28     /**
29      * Exports a value as a string
30      *
31      * The output of this method is similar to the output of print_r(), but
32      * improved in various aspects:
33      *
34      *  - NULL is rendered as "null" (instead of "")
35      *  - TRUE is rendered as "true" (instead of "1")
36      *  - FALSE is rendered as "false" (instead of "")
37      *  - Strings are always quoted with single quotes
38      *  - Carriage returns and newlines are normalized to \n
39      *  - Recursion and repeated rendering is treated properly
40      *
41      * @param  mixed  $value
42      * @param  int    $indentation The indentation level of the 2nd+ line
43      * @return string
44      */
45     public function export($value, $indentation = 0)
46     {
47         return $this->recursiveExport($value, $indentation);
48     }
49
50     /**
51      * @param  mixed   $data
52      * @param  Context $context
53      * @return string
54      */
55     public function shortenedRecursiveExport(&$data, Context $context = null)
56     {
57         $result   = array();
58         $exporter = new self();
59
60         if (!$context) {
61             $context = new Context;
62         }
63
64         $context->add($data);
65
66         foreach ($data as $key => $value) {
67             if (is_array($value)) {
68                 if ($context->contains($data[$key]) !== false) {
69                     $result[] = '*RECURSION*';
70                 }
71
72                 else {
73                     $result[] = sprintf(
74                         'array(%s)',
75                         $this->shortenedRecursiveExport($data[$key], $context)
76                     );
77                 }
78             }
79
80             else {
81                 $result[] = $exporter->shortenedExport($value);
82             }
83         }
84
85         return implode(', ', $result);
86     }
87
88     /**
89      * Exports a value into a single-line string
90      *
91      * The output of this method is similar to the output of
92      * SebastianBergmann\Exporter\Exporter::export().
93      *
94      * Newlines are replaced by the visible string '\n'.
95      * Contents of arrays and objects (if any) are replaced by '...'.
96      *
97      * @param  mixed  $value
98      * @return string
99      * @see    SebastianBergmann\Exporter\Exporter::export
100      */
101     public function shortenedExport($value)
102     {
103         if (is_string($value)) {
104             $string = $this->export($value);
105
106             if (function_exists('mb_strlen')) {
107                 if (mb_strlen($string) > 40) {
108                     $string = mb_substr($string, 0, 30) . '...' . mb_substr($string, -7);
109                 }
110             } else {
111                 if (strlen($string) > 40) {
112                     $string = substr($string, 0, 30) . '...' . substr($string, -7);
113                 }
114             }
115
116             return str_replace("\n", '\n', $string);
117         }
118
119         if (is_object($value)) {
120             return sprintf(
121                 '%s Object (%s)',
122                 get_class($value),
123                 count($this->toArray($value)) > 0 ? '...' : ''
124             );
125         }
126
127         if (is_array($value)) {
128             return sprintf(
129                 'Array (%s)',
130                 count($value) > 0 ? '...' : ''
131             );
132         }
133
134         return $this->export($value);
135     }
136
137     /**
138      * Converts an object to an array containing all of its private, protected
139      * and public properties.
140      *
141      * @param  mixed $value
142      * @return array
143      */
144     public function toArray($value)
145     {
146         if (!is_object($value)) {
147             return (array) $value;
148         }
149
150         $array = array();
151
152         foreach ((array) $value as $key => $val) {
153             // properties are transformed to keys in the following way:
154             // private   $property => "\0Classname\0property"
155             // protected $property => "\0*\0property"
156             // public    $property => "property"
157             if (preg_match('/^\0.+\0(.+)$/', $key, $matches)) {
158                 $key = $matches[1];
159             }
160
161             // See https://github.com/php/php-src/commit/5721132
162             if ($key === "\0gcdata") {
163                 continue;
164             }
165
166             $array[$key] = $val;
167         }
168
169         // Some internal classes like SplObjectStorage don't work with the
170         // above (fast) mechanism nor with reflection in Zend.
171         // Format the output similarly to print_r() in this case
172         if ($value instanceof \SplObjectStorage) {
173             // However, the fast method does work in HHVM, and exposes the
174             // internal implementation. Hide it again.
175             if (property_exists('\SplObjectStorage', '__storage')) {
176                 unset($array['__storage']);
177             } elseif (property_exists('\SplObjectStorage', 'storage')) {
178                 unset($array['storage']);
179             }
180
181             if (property_exists('\SplObjectStorage', '__key')) {
182                 unset($array['__key']);
183             }
184
185             foreach ($value as $key => $val) {
186                 $array[spl_object_hash($val)] = array(
187                     'obj' => $val,
188                     'inf' => $value->getInfo(),
189                 );
190             }
191         }
192
193         return $array;
194     }
195
196     /**
197      * Recursive implementation of export
198      *
199      * @param  mixed                                       $value       The value to export
200      * @param  int                                         $indentation The indentation level of the 2nd+ line
201      * @param  \SebastianBergmann\RecursionContext\Context $processed   Previously processed objects
202      * @return string
203      * @see    SebastianBergmann\Exporter\Exporter::export
204      */
205     protected function recursiveExport(&$value, $indentation, $processed = null)
206     {
207         if ($value === null) {
208             return 'null';
209         }
210
211         if ($value === true) {
212             return 'true';
213         }
214
215         if ($value === false) {
216             return 'false';
217         }
218
219         if (is_float($value) && floatval(intval($value)) === $value) {
220             return "$value.0";
221         }
222
223         if (is_resource($value)) {
224             return sprintf(
225                 'resource(%d) of type (%s)',
226                 $value,
227                 get_resource_type($value)
228             );
229         }
230
231         if (is_string($value)) {
232             // Match for most non printable chars somewhat taking multibyte chars into account
233             if (preg_match('/[^\x09-\x0d\x1b\x20-\xff]/', $value)) {
234                 return 'Binary String: 0x' . bin2hex($value);
235             }
236
237             return "'" .
238             str_replace(array("\r\n", "\n\r", "\r"), array("\n", "\n", "\n"), $value) .
239             "'";
240         }
241
242         $whitespace = str_repeat(' ', 4 * $indentation);
243
244         if (!$processed) {
245             $processed = new Context;
246         }
247
248         if (is_array($value)) {
249             if (($key = $processed->contains($value)) !== false) {
250                 return 'Array &' . $key;
251             }
252
253             $key    = $processed->add($value);
254             $values = '';
255
256             if (count($value) > 0) {
257                 foreach ($value as $k => $v) {
258                     $values .= sprintf(
259                         '%s    %s => %s' . "\n",
260                         $whitespace,
261                         $this->recursiveExport($k, $indentation),
262                         $this->recursiveExport($value[$k], $indentation + 1, $processed)
263                     );
264                 }
265
266                 $values = "\n" . $values . $whitespace;
267             }
268
269             return sprintf('Array &%s (%s)', $key, $values);
270         }
271
272         if (is_object($value)) {
273             $class = get_class($value);
274
275             if ($hash = $processed->contains($value)) {
276                 return sprintf('%s Object &%s', $class, $hash);
277             }
278
279             $hash   = $processed->add($value);
280             $values = '';
281             $array  = $this->toArray($value);
282
283             if (count($array) > 0) {
284                 foreach ($array as $k => $v) {
285                     $values .= sprintf(
286                         '%s    %s => %s' . "\n",
287                         $whitespace,
288                         $this->recursiveExport($k, $indentation),
289                         $this->recursiveExport($v, $indentation + 1, $processed)
290                     );
291                 }
292
293                 $values = "\n" . $values . $whitespace;
294             }
295
296             return sprintf('%s Object &%s (%s)', $class, $hash, $values);
297         }
298
299         return var_export($value, true);
300     }
301 }