MiscHelper::dump_table()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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