Completed
Pull Request — master (#411)
by Anton
04:55
created

_functions.php ➔ value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 2
rs 10
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
declare(strict_types=1);
10
11
use Bluz\Common\Collection;
12
use Bluz\Translator\Translator;
13
14
/**
15
 * Simple functions of framework
16
 * be careful with this way
17
 *
18
 * @author   Anton Shevchuk
19
 */
20
if (!function_exists('array_get')) {
21
    /**
22
     * Get value of array by keys
23
     *
24
     * @param $array
25
     * @param array ...$keys
26
     * @return mixed|null
27
     */
28
    function array_get(array $array, ...$keys)
29
    {
30 1
        return Collection::get($array, ...$keys);
31
    }
32
}
33
34
if (!function_exists('array_has')) {
35
    /**
36
     * @param $array
37
     * @param array ...$keys
38
     * @return mixed|null
39
     */
40
    function array_has(array $array, ...$keys)
41
    {
42 1
        return Collection::has($array, ...$keys);
43
    }
44
}
45
46
if (!function_exists('array_add')) {
47
    /**
48
     * @param $array
49
     * @param array ...$keys
50
     * @return void
51
     */
52
    function array_add(array &$array, ...$keys)
53
    {
54 1
        Collection::add($array, ...$keys);
55 1
    }
56
}
57
58
if (!function_exists('array_set')) {
59
    /**
60
     * @param $array
61
     * @param array ...$keys
62
     * @return void
63
     */
64
    function array_set(array &$array, ...$keys)
65
    {
66 1
        Collection::set($array, ...$keys);
67 1
    }
68
}
69
70
if (!function_exists('debug')) {
71
    /**
72
     * Debug variables
73
     *
74
     * Example of usage
75
     *     debug(123);
76
     *     debug(new stdClass());
77
     *     debug($_GET, $_POST, $_FILES);
78
     *
79
     * @codeCoverageIgnore
80
     *
81
     * @param mixed ...$params
82
     */
83
    function debug(...$params)
84
    {
85
        // check definition
86
        if (!getenv('BLUZ_DEBUG') || !isset($_COOKIE['BLUZ_DEBUG'])) {
87
            return;
88
        }
89
90
        ini_set('xdebug.var_display_max_children', '512');
91
92
        if (PHP_SAPI === 'cli') {
93
            if (extension_loaded('xdebug')) {
94
                // try to enable CLI colors
95
                ini_set('xdebug.cli_color', '1');
96
                xdebug_print_function_stack();
97
            } else {
98
                debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
99
            }
100
            var_dump($params);
101
        } else {
102
            echo '<div class="textleft clear"><pre class="bluz-debug">';
103
            debug_print_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
104
            var_dump($params);
105
            echo '</pre></div>';
106
        }
107
    }
108
}
109
110
if (!function_exists('esc')) {
111
    /**
112
     * Escape variable for use in View
113
     *
114
     * Example of usage
115
     *     esc($_GET['name']);
116
     *     esc($_GET['name'], ENT_QUOTES);
117
     *
118
     * @param  string  $variable
119
     * @param  integer $flags
120
     * @return string
121
     */
122
    function esc($variable, int $flags = ENT_HTML5)
123
    {
124 9
        return htmlentities((string)$variable, $flags, 'UTF-8');
125
    }
126
}
127
128
if (!function_exists('value')) {
129
    /**
130
     * Return the value for callable
131
     *
132
     * @param  mixed  $value
133
     * @return mixed
134
     */
135
    function value($value)
136
    {
137 5
        return is_callable($value) ? $value() : $value;
138
    }
139
}
140
141
// @codingStandardsIgnoreStart
142
if (!function_exists('__')) {
143
    /**
144
     * Translate message
145
     *
146
     * Example of usage
147
     *
148
     *     // simple
149
     *     // equal to gettext('Message')
150
     *     __('Message');
151
     *
152
     *     // simple replace of one or more argument(s)
153
     *     // equal to sprintf(gettext('Message to %s'), 'Username')
154
     *     __('Message to %s', 'Username');
155
     *
156
     * @param  string   $message
157
     * @param  string[] $text [optional]
158
     * @return string
159
     */
160
    function __($message, ...$text)
161
    {
162 76
        return Translator::translate($message, ...$text);
163
    }
164
}
165
166
if (!function_exists('_n')) {
167
    /**
168
     * Translate plural form
169
     *
170
     * Example of usage
171
     *
172
     *     // plural form + sprintf
173
     *     // equal to sprintf(ngettext('%d comment', '%d comments', 4), 4)
174
     *     _n('%d comment', '%d comments', 4, 4)
175
     *
176
     *     // plural form + sprintf
177
     *     // equal to sprintf(ngettext('%d comment', '%d comments', 4), 4, 'Topic')
178
     *     _n('%d comment to %s', '%d comments to %s', 4, 'Topic')
179
     *
180
     * @param  string   $singular
181
     * @param  string   $plural
182
     * @param  integer  $number
183
     * @param  string[] $text      [optional]
184
     * @return string
185
     */
186
    function _n($singular, $plural, $number, ...$text)
187
    {
188
        return Translator::translatePlural($singular, $plural, $number, ...$text);
189
    }
190
}
191
// @codingStandardsIgnoreEnd
192