1<?php
2
3namespace PhpAmqpLib\Helper;
4
5class MiscHelper
6{
7    /**
8     * @param string|array $a
9     * @return string
10     */
11    public static function methodSig($a)
12    {
13        if (is_string($a)) {
14            return $a;
15        }
16
17        return sprintf('%d,%d', $a[0], $a[1]);
18    }
19
20    /**
21     * Gets a number (either int or float) and returns an array containing its integer part as first element and its
22     * decimal part mutliplied by 10^6. Useful for some PHP stream functions that need seconds and microseconds as
23     * different arguments
24     *
25     * @param int|float $number
26     * @return int[]
27     */
28    public static function splitSecondsMicroseconds($number)
29    {
30        return array((int)floor($number), (int)(fmod($number, 1) * 1000000));
31    }
32
33    /**
34     * View any string as a hexdump.
35     *
36     * This is most commonly used to view binary data from streams
37     * or sockets while debugging, but can be used to view any string
38     * with non-viewable characters.
39     *
40     * @version     1.3.2
41     * @author      Aidan Lister <aidan@php.net>
42     * @author      Peter Waller <iridum@php.net>
43     * @link        http://aidanlister.com/repos/v/function.hexdump.php
44     *
45     * @param string $data The string to be dumped
46     * @param bool $htmloutput Set to false for non-HTML output
47     * @param bool $uppercase Set to true for uppercase hex
48     * @param bool $return Set to true to return the dump
49     * @return string|null
50     */
51    public static function hexdump($data, $htmloutput = true, $uppercase = false, $return = false)
52    {
53        // Init
54        $hexi = '';
55        $ascii = '';
56        $dump = $htmloutput ? '<pre>' : '';
57        $offset = 0;
58        $len = mb_strlen($data, 'ASCII');
59
60        // Upper or lower case hexidecimal
61        $hexFormat = $uppercase ? 'X' : 'x';
62
63        // Iterate string
64        for ($i = $j = 0; $i < $len; $i++) {
65            // Convert to hexidecimal
66            // We must use concatenation here because the $hexFormat value
67            // is needed for sprintf() to parse the format
68            $hexi .= sprintf('%02' .  $hexFormat . ' ', ord($data[$i]));
69
70            // Replace non-viewable bytes with '.'
71            if (ord($data[$i]) >= 32) {
72                $ascii .= $htmloutput ? htmlentities($data[$i]) : $data[$i];
73            } else {
74                $ascii .= '.';
75            }
76
77            // Add extra column spacing
78            if ($j === 7) {
79                $hexi .= ' ';
80                $ascii .= ' ';
81            }
82
83            // Add row
84            if (++$j === 16 || $i === $len - 1) {
85                // Join the hexi / ascii output
86                // We must use concatenation here because the $hexFormat value
87                // is needed for sprintf() to parse the format
88                $dump .= sprintf('%04' . $hexFormat . '  %-49s  %s', $offset, $hexi, $ascii);
89
90                // Reset vars
91                $hexi = $ascii = '';
92                $offset += 16;
93                $j = 0;
94
95                // Add newline
96                if ($i !== $len - 1) {
97                    $dump .= PHP_EOL;
98                }
99            }
100        }
101
102        // Finish dump
103        $dump .= $htmloutput ? '</pre>' : '';
104        $dump .= PHP_EOL;
105
106        if ($return) {
107            return $dump;
108        }
109
110        echo $dump;
111
112        return null;
113    }
114
115    /**
116     * @param array $table
117     * @return string
118     */
119    public static function dump_table($table)
120    {
121        $tokens = array();
122        foreach ($table as $name => $value) {
123            switch ($value[0]) {
124                case 'D':
125                    $val = $value[1]->n . 'E' . $value[1]->e;
126                    break;
127                case 'F':
128                    $val = '(' . self::dump_table($value[1]) . ')';
129                    break;
130                case 'T':
131                    $val = date('Y-m-d H:i:s', $value[1]);
132                    break;
133                default:
134                    $val = $value[1];
135            }
136            $tokens[] = $name . '=' . $val;
137        }
138
139        return implode(', ', $tokens);
140    }
141}
142