Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HelperTrait.php « traits « src - github.com/HuasoFoundries/phpPgAdmin6.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0610572c705d0cee6edfa4a21c6a133826505249 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php

/**
 * PHPPgAdmin 6.1.3
 */

namespace PHPPgAdmin\Traits;

\defined('BASE_PATH') || \define('BASE_PATH', \dirname(__DIR__, 2));

/**
 * @file
 * A trait with helpers methods to debug, halt the app and format text to html
 */

/**
 * A trait with helpers methods to debug, halt the app and format text to html.
 */
trait HelperTrait
{
    /**
     * Halts the execution of the program. It's like calling exit() but using builtin Slim Exceptions.
     *
     * @param string $msg The message to show to the user
     *
     * @throws \Slim\Exception\SlimException (description)
     */
    public function halt($msg = 'An error has happened'): void
    {
        $body = \responseInstance()->getBody();
        $body->write($msg);

        throw new \Slim\Exception\SlimException(\requestInstance(), \responseInstance());
    }

    public static function getBackTrace($offset = 0)
    {
        $i0 = $offset;
        $i1 = $offset + 1;
        $backtrace = \debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, $offset + 3);

        return [
            'class' => 'Closure' === $backtrace[$i1]['class'] ?
            $backtrace[$i0]['file'] :
            $backtrace[$i1]['class'],

            'type' => $backtrace[$i1]['type'],

            'function' => '{closure}' === $backtrace[$i1]['function']
            ? $backtrace[$i0]['function'] :
            $backtrace[$i1]['function'],

            'spacer4' => ' ',
            'line' => $backtrace[$i0]['line'],
        ];
        //dump($backtrace);
    }

    /**
     * Converts an ADORecordSet to an array.
     *
     * @param \ADORecordSet $set   The set
     * @param string        $field optionally the field to query for
     *
     * @return array the parsed array
     */
    public static function recordSetToArray($set, $field = '')
    {
        $result = [];

        if (0 >= $set->RecordCount()) {
            return $result;
        }

        while (!$set->EOF) {
            $result[] = $field ? $set->fields[$field] : $set;
            $set->MoveNext();
        }

        return $result;
    }

    /**
     * Checks if a variable is defined, in which case assign its value to $var1
     * If it isn't and $set is true, assign the default value. Otherwise don't
     * assign anything to $var1.
     *
     * @param mixed $var1    The variable to manipulate if $set if true
     * @param mixed $var2    The value to assign to $var1 if it's defined
     * @param mixed $default The default value to set, it $set is true
     * @param bool  $set     True to set the default value if $var2 isn't defined
     *
     * @return mixed the value of $var2 is $var2 is set, or $default otherwise
     */
    public function setIfIsset(&$var1, $var2, $default = null, $set = true)
    {
        if (isset($var2)) {
            $var1 = $var2;

            return $var1;
        }

        if (true === $set) {
            $var1 = $default;

            return $var1;
        }

        return $default;
    }

    /**
     * Checks if the $key of an $array is set. If it isn't, optionally set it to
     * the default parameter.
     *
     * @param array      $array   The array to check
     * @param int|string $key     The key to check
     * @param mixed      $default The default value to set, it $set is true
     * @param bool       $set     True to set the default value if $key isn't
     *                            set
     *
     * @return array the original array
     */
    public function coalesceArr(&$array, $key, $default = null, $set = true)
    {
        if (!isset($array[$key]) && true === $set) {
            $array[$key] = $default;
        }

        return $array;
    }

    public static function formatSizeUnits($bytes, $lang)
    {
        if (-1 === $bytes) {
            $bytes = $lang['strnoaccess'];
        } elseif (1099511627776 <= $bytes) {
            $bytes = \sprintf('%s %s', \number_format($bytes / 1099511627776, 0), $lang['strtb']);
        } elseif (1073741824 <= $bytes) {
            $bytes = \sprintf('%s %s', \number_format($bytes / 1073741824, 0), $lang['strgb']);
        } elseif (1048576 <= $bytes) {
            $bytes = \sprintf('%s %s', \number_format($bytes / 1048576, 0), $lang['strmb']);
        } elseif (1024 <= $bytes) {
            $bytes = \sprintf('%s %s', \number_format($bytes / 1024, 0), $lang['strkb']);
        } else {
            $bytes = \sprintf('%s %s', $bytes, $lang['strbytes']);
        }

        return $bytes;
    }

    /**
     * Receives N parameters and sends them to the console adding where was it called from.
     *
     * @param array<int, mixed> $args
     */
    public function prtrace(...$args): void
    {
        if (\function_exists('\dump')) {
            \dump($args);
        }
    }

    /**
     * Just an alias of prtrace.
     *
     * @param array<int, mixed> $args The arguments
     */
    public function dump(...$args): void
    {
        if (\function_exists('\dump')) {
            \dump($args);
        }
    }

    /**
     * Just an alias of prtrace.
     *
     * @param array<int, mixed> $args The arguments
     */
    public function dumpAndDie(...$args): void
    {
        if (\function_exists('\dump')) {
            \dump($args);
        }

        exit();
    }

    /**
     * Receives N parameters and sends them to the console adding where was it
     * called from.
     *
     * @param array<int, mixed> $args
     */
    public static function staticTrace(
        ...$args
    ): void {
        if (\function_exists('\dump')) {
            \dump($args);
        }
    }
}