Completed
Pull Request — master (#363)
by Anton
06:05
created

_functions.php ➔ debug()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 16
nc 4
nop 1
dl 0
loc 25
ccs 0
cts 0
cp 0
crap 20
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link https://github.com/bluzphp/framework
7
 */
8
9
use Bluz\Translator\Translator;
10
11
/**
12
 * Simple functions of framework
13
 * be careful with this way
14
 *
15
 * @author   Anton Shevchuk
16
 */
17
18
if (!function_exists('debug')) {
19
    /**
20
     * Debug variables
21
     *
22
     * Example of usage
23
     *     debug(123);
24
     *     debug(new stdClass());
25
     *     debug($_GET, $_POST, $_FILES);
26
     *
27
     * @codeCoverageIgnore
28
     *
29
     * @param $params
30
     */
31
    function debug(...$params)
32
    {
33
        // check definition
34
        if (!getenv('BLUZ_DEBUG')) {
35
            return;
36
        }
37
38
        ini_set('xdebug.var_display_max_children', 512);
39
40
        if ('cli' == PHP_SAPI) {
41
            if (extension_loaded('xdebug')) {
42
                // try to enable CLI colors
43
                ini_set('xdebug.cli_color', 1);
44
                xdebug_print_function_stack();
45
            } else {
46
                debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
47
            }
48
            var_dump($params);
49
        } else {
50
            echo '<div class="textleft clear"><pre>';
51
            debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
52
            var_dump($params);
53
            echo '</pre></div>';
54
        }
55
    }
56
}
57
58
if (!function_exists('esc')) {
59
    /**
60
     * Escape variable for use in View
61
     *
62
     * Example of usage
63
     *     esc($_GET['name']);
64
     *     esc($_GET['name'], ENT_QUOTES);
65
     *
66
     * @param string $variable
67
     * @param integer $flags
68
     * @return string
69
     */
70
    function esc($variable, $flags = ENT_HTML5)
71
    {
72 8
        return htmlentities($variable, $flags, "UTF-8");
73
    }
74
}
75
76
// @codingStandardsIgnoreStart
77
if (!function_exists('__')) {
78
    /**
79
     * Translate message
80
     *
81
     * Example of usage
82
     *     // simple
83
     *     // equal to gettext('Message')
84
     *     __('Message');
85
     *
86
     *     // simple replace of one or more argument(s)
87
     *     // equal to sprintf(gettext('Message to %s'), 'Username')
88
     *     __('Message to %s', 'Username');
89
     *
90
     * @param  string   $message
91
     * @param  string[] $text [optional]
92
     * @return string
93
     */
94
    function __($message, ...$text)
95
    {
96 74
        return Translator::translate($message, ...$text);
97
    }
98
}
99
100
if (!function_exists('_n')) {
101
    /**
102
     * Translate plural form
103
     *
104
     * Example of usage
105
     *     // plural form + sprintf
106
     *     // equal to sprintf(ngettext('%d comment', '%d comments', 4), 4)
107
     *     _n('%d comment', '%d comments', 4, 4)
108
     *
109
     *     // plural form + sprintf
110
     *     // equal to sprintf(ngettext('%d comment', '%d comments', 4), 4, 'Topic')
111
     *     _n('%d comment to %s', '%d comments to %s', 4, 'Topic')
112
     *
113
     * @param  string   $singular
114
     * @param  string   $plural
115
     * @param  integer      $number
116
     * @param  string[] $text      [optional]
117
     * @return string
118
     */
119
    function _n($singular, $plural, $number, ...$text)
120
    {
121
        return Translator::translatePlural($singular, $plural, $number, ...$text);
122
    }
123
}
124
// @codingStandardsIgnoreEnd
125